From 27a015eab23992f25b88da0d5bf0dd20da3c7dec Mon Sep 17 00:00:00 2001 From: dcz <dcz@ipipan.waw.pl> Date: Fri, 3 Nov 2023 14:22:38 +0100 Subject: [PATCH] Free lus bugfix. --- entries/static/entries/js/entries.js | 15 +++ entries/urls.py | 3 +- entries/views.py | 16 ++- freelus/urls.py | 3 + freelus/views.py | 42 ++++++- .../unification/free_lu/FreeLuEdit.vue | 110 ++++++++++++------ 6 files changed, 146 insertions(+), 43 deletions(-) diff --git a/entries/static/entries/js/entries.js b/entries/static/entries/js/entries.js index 25ead31..6d83ac0 100644 --- a/entries/static/entries/js/entries.js +++ b/entries/static/entries/js/entries.js @@ -12,6 +12,7 @@ var curr_examples_by_id = null; var roles = [] var role_attributes = [] var role_sub_attributes = [] +var frame_opinions = [] function make_opinion_row(item, span, width) { const opinion_row = document.createElement('tr'); @@ -1133,6 +1134,18 @@ function getRoleSubAttributes() { }); } +function getFrameOpinions() { + $.ajax({ + dataType: "json", + url: '/' + lang + '/entries/frame_opinions', + success: function(data){ + frame_opinions = data.result; + }, + async: false + }); +} + + $(document).ready(function() { bind_last_visited(); @@ -1166,6 +1179,8 @@ $(document).ready(function() { getRoleSubAttributes(); + getFrameOpinions(); + // $.getJSON('relations', function(data){ // memorizeRelations(data.relations); // }); diff --git a/entries/urls.py b/entries/urls.py index 3ff50d6..f434ba6 100644 --- a/entries/urls.py +++ b/entries/urls.py @@ -2,7 +2,7 @@ from django.urls import path from . import autocompletes, views from .views import ajax_plWN_context_lookup, ajax_predefined_preferences, ajax_relations, ajax_synsets, ajax_roles, \ - ajax_role_attributes, ajax_role_sub_attributes, ajax_free_slowal_frame_lookup + ajax_role_attributes, ajax_role_sub_attributes, ajax_free_slowal_frame_lookup, ajax_frame_opinions app_name = 'entries' @@ -25,6 +25,7 @@ urlpatterns = [ path('plWN_context_lookup/', ajax_plWN_context_lookup, name='plWN_context_lookup'), path('predefined_preferences/', ajax_predefined_preferences, name='predefined_preferences'), path('roles/', ajax_roles, name='roles'), + path('frame_opinions/', ajax_frame_opinions, name='frame_opinions'), path('role_attributes/', ajax_role_attributes, name='role_attributes'), path('role_sub_attributes/', ajax_role_sub_attributes, name='role_sub_attributes'), path('relations/', ajax_relations, name='relations'), diff --git a/entries/views.py b/entries/views.py index f81b03f..013fc94 100644 --- a/entries/views.py +++ b/entries/views.py @@ -21,7 +21,7 @@ from connections.models import Entry, Subentry, ArgumentConnection, RealisationD from meanings.models import LexicalUnit from syntax.models import NaturalLanguageDescription, Schema from semantics.models import Frame, PredefinedSelectionalPreference, SelectivePreferenceRelations, SemanticRole, \ - RoleAttribute, RoleSubAttribute, SelectionalPreferenceRelation + RoleAttribute, RoleSubAttribute, SelectionalPreferenceRelation, FrameOpinion from common.decorators import ajax_required, ajax from unifier.models import UnifiedFrame @@ -452,8 +452,7 @@ def get_entries(request): #entries = entries.filter(subentries__schema_hooks__alternation=2) if without_frames: - entries = entries.filter(name__in=(LexicalUnit.objects.filter(frames__isnull=True).values("base"))) - print('entries.query: ', entries.query) + entries = entries.filter(name__in=(LexicalUnit.objects.filter(Q(frames__isnull=True) | Q(frames__in_building=True)).values("base"))) total = entries.count() if scroller_params['filter']: @@ -1080,6 +1079,15 @@ def ajax_roles(request): return context +@ajax(method='get', encode_result=True) +def ajax_frame_opinions(request): + opinions = [] + for opinion in FrameOpinion.objects.exclude(key='unk').filter(frame__isnull=False).distinct().order_by('priority'): + opinions.append({"id": opinion.id, "key": str(FRAME_OPINION()[opinion.key])}) + + return {'result': opinions} + + @ajax(method='get', encode_result=True) def ajax_role_attributes(request): roleAttributes = [] @@ -1164,3 +1172,5 @@ def ajax_free_slowal_frame_lookup(request, term): lexical_unit_str.append(str(lexical_unit)) return {'result': lexical_unit_str} + + diff --git a/freelus/urls.py b/freelus/urls.py index 81308e4..555efc7 100644 --- a/freelus/urls.py +++ b/freelus/urls.py @@ -14,5 +14,8 @@ urlpatterns = [ name='delete_schema_to_argument_connection'), path('finish_frame_processing/', views.finish_frame_processing, name='finish_frame_processing'), path('remove_argument_from_frame/', views.remove_argument_from_frame, name='remove_argument_from_frame'), + path('change_lus_in_slowal_frame/', views.change_lus_in_slowal_frame, name='change_lus_in_slowal_frame'), + path('change_frame_opinion/', views.change_frame_opinion, name='change_frame_opinion'), + ] diff --git a/freelus/views.py b/freelus/views.py index 509bc14..f404654 100644 --- a/freelus/views.py +++ b/freelus/views.py @@ -13,19 +13,51 @@ from syntax.models import Position @ajax_required @transaction.atomic -def change_status(request): +def change_frame_opinion(request): """ - Changing status of slowal frame in building process. - The request has to contain 'entry_id' and 'status'. + Changing opinion of slowal frame in building process. + The request has to contain 'frame_id' and 'opinion_id'. + :param request: http request + :return: Empty json response + """ + if request.method == 'POST': + frame_id = request.POST['frame_id'] + opinion_id = request.POST['opinion_id'] + + frame = Frame.objects.get(pk=frame_id) + frame.opinion_id = opinion_id + frame.save() + return JsonResponse({}) + + return JsonResponse({}) + + +@ajax_required +@transaction.atomic +def change_lus_in_slowal_frame(request): + """ + Change lus assigned to slowal frame. + The request has to contain 'entry_id' and 'lu_ids' list for witch slowal frame has to be created. :param request: http request :return: Empty json response """ if request.method == 'POST': entry_id = request.POST['entry_id'] - status = request.POST['status'] + frame_id = request.POST['frame_id'] + lu_ids = json.loads(request.POST['lu_ids']) + frame = Frame.objects.get(id=frame_id) + frame.lexical_units.clear() entry = Entry.objects.get(pk=entry_id) - entry.status = Status.objects.get(key=status) + entry.lexical_units.clear() + for lu_id in lu_ids: + lu = LexicalUnit.objects.get(pk=lu_id) + lu.entry = entry + lu.save() + frame.lexical_units.add(lu) + entry.lexical_units_count = entry.lexical_units_count + 1 + + frame.save() entry.save() return JsonResponse({}) diff --git a/frontend/src/components/unification/free_lu/FreeLuEdit.vue b/frontend/src/components/unification/free_lu/FreeLuEdit.vue index 11370d4..3ee5dd6 100644 --- a/frontend/src/components/unification/free_lu/FreeLuEdit.vue +++ b/frontend/src/components/unification/free_lu/FreeLuEdit.vue @@ -151,10 +151,16 @@ this.active_slowal_frame = frame; } }, - create_new_slowal_frame() { + connect_lus_to_slowal_frame(frame_in_progress) { + const frame_in_progress_lus = frame_in_progress ? frames2lexical_units([frame_in_progress]).map(l => { + l.pk = l.id; + l.display = l.str; + return l; + }) : []; + const frame_in_progress_lus_ids = new Set(frame_in_progress_lus.map(l => l.pk)); const lusSelect = function () { - return this.free_lexical_units.map(lu => { - return `<label><input type="checkbox" name="lus" value="${lu.pk}" /> ${lu.display}</label><br />`; + return this.lexical_units.concat(this.free_lexical_units).concat(frame_in_progress_lus).map(lu => { + return `<label><input type="checkbox" ${frame_in_progress_lus_ids.has(lu.pk) ? 'checked' : ''} name="lus" value="${lu.pk}" /> ${lu.display}</label><br />`; }).join(""); }.bind(this); @@ -174,16 +180,17 @@ let lu_ids = normalizeFormData(f.lus); const data = { 'lu_ids': JSON.stringify(lu_ids), - 'entry_id': this.entryId + 'entry_id': this.entryId, + 'frame_id': frame_in_progress ? frame_in_progress.id : null }; $.ajax({ type: 'post', - url: '/' + lang + '/freelus/create_new_slowal_frame/', + url: '/' + lang + '/freelus/' + (frame_in_progress ? 'change_lus_in_slowal_frame' : 'create_new_slowal_frame') + '/', dataType: 'json', data: data, timeout: 60000, success: function (response) { - show_info('Nowa rama została stworzona.'); + // show_info(frame_in_progress ? 'Nowa rama została stworzona.' : 'Lista podpiętych jednostek leksyklanych zostala zmieniona.'); this.loadEntry(); $.prompt.close(); }.bind(this), @@ -255,20 +262,47 @@ }, change_frame_opinion() { if (this.frame_in_progress) { - send_post_request('/freelus/change_frame_opinion/', - { - 'frame_id': this.frame_in_progress.id, - 'opinion': frame.id, - }, - (reponse) => { - show_info('Opinia została zmieniona.'); - alert(gettext("Opinia została zmieniona.")); - this.loadEntry(); - }, - (request, errorType, errorMessage) => { - show_error(errorType + ' (' + errorMessage + ')'); - alert(gettext("Zmiana opinii nie powiodła się. Błąd: " + errorType + ' (' + errorMessage + ')')); - }) + const frame_opinion_select = function () { + return frame_opinions.map(frame_opinion => { + return `<label><input type="radio" name="opinion" value="${frame_opinion.id}" /> ${frame_opinion.key}</label><br />`; + }).join(""); + }.bind(this); + + const change_frame_opinion_popup = { + state0: { + title: 'Wybierz opinię', + html: frame_opinion_select, + buttons: {Anuluj: 0, Wybierz: 1}, + focus: -1, + submit: function (e, v, m, f) { + if (v == 0) { + e.preventDefault(); + $.prompt.close(); + } + if (v === 1) { + e.preventDefault(); + let opinion_id = normalizeFormData(f.opinion)[0]; + send_post_request('/freelus/change_frame_opinion/', + { + 'frame_id': this.frame_in_progress.id, + 'opinion_id': opinion_id, + }, + (reponse) => { + // show_info('Opinia została zmieniona.'); + alert(gettext("Opinia została zmieniona.")); + this.loadEntry(); + $.prompt.close(); + }, + (request, errorType, errorMessage) => { + show_error(errorType + ' (' + errorMessage + ')'); + alert(gettext("Zmiana opinii nie powiodła się. Błąd: " + errorType + ' (' + errorMessage + ')')); + $.prompt.close(); + }) + } + }.bind(this) + } + } + $.prompt(change_frame_opinion_popup); } else { alert(gettext("Stwórz nową ramę.")); } @@ -296,7 +330,7 @@ data: data, timeout: 60000, success: function (response) { - alert(gettext("Argument został usunięty.")); + // alert(gettext("Argument został usunięty.")); show_info('Argument został usunięty.'); this.loadEntry(); $.prompt.close(); @@ -363,12 +397,13 @@ data: data, timeout: 60000, success: function (response) { - alert(gettext("Nowa rola zosała zapisana.")); + // alert(gettext("Nowa rola zosała zapisana.")); show_info('Nowa rola zosała zapisana.'); this.loadEntry(); $.prompt.close(); }.bind(this), error: function (request, errorType, errorMessage) { + alert(errorType + ' (' + errorMessage + ')'); show_error(errorType + ' (' + errorMessage + ')'); $.prompt.close(); } @@ -417,7 +452,7 @@ timeout: 60000, success: function (response) { if(response['succ'] == true) { - alert(gettext("Argument ramy został powiązany z tybranym argumentem schematu.")); + // alert(gettext("Argument ramy został powiązany z tybranym argumentem schematu.")); show_info('Argument ramy został powiązany z tybranym argumentem schematu.'); this.loadEntry(); } else { @@ -427,6 +462,7 @@ $.prompt.close(); }.bind(this), error: function (request, errorType, errorMessage) { + alert(errorType + ' (' + errorMessage + ')'); show_error(errorType + ' (' + errorMessage + ')'); $.prompt.close(); } @@ -452,12 +488,13 @@ data: data, timeout: 60000, success: function (response) { - alert(gettext("Powiązanie pomiędzy argumentem ramy a argumentem schematu zostało usunięte.")); + // alert(gettext("Powiązanie pomiędzy argumentem ramy a argumentem schematu zostało usunięte.")); show_info('Powiązanie pomiędzy argumentem ramy a argumentem schematu zostało usunięte.'); this.loadEntry(); $.prompt.close(); }.bind(this), error: function (request, errorType, errorMessage) { + alert(errorType + ' (' + errorMessage + ')'); show_error(errorType + ' (' + errorMessage + ')'); $.prompt.close(); } @@ -484,6 +521,7 @@ $.prompt.close(); }.bind(this), error: function (request, errorType, errorMessage) { + alert(errorType + ' (' + errorMessage + ')'); show_error(errorType + ' (' + errorMessage + ')'); $.prompt.close(); } @@ -522,32 +560,36 @@ <div id="free-lus-pane" class="col w-100 p-0 overflow-auto"> <table class="table-button-menu sticky-top" cellspacing="1"> <tr style="background-color: white;"> - <td id="create-new-frame" class="table-button-menu-td" @click="create_new_slowal_frame()" - style="padding: 10px 15px 10px 15px; color: #000000;">Nowa rama + <td id="create-new-frame" class="table-button-menu-td" @click="connect_lus_to_slowal_frame(this.frame_in_progress)" + style="padding: 10px 15px 10px 15px; color: #000000;">{{this.frame_in_progress ? 'Dodaj/Usuń jednostki' : 'Nowa rama'}} </td> <td id="add-argument" class="table-button-menu-td" @click="change_role(-1, 'add_argument_to_frame')" style="padding: 10px 15px 10px 15px; color: #000000;">Dodaj argument </td> - <td id="change-role" class="table-button-menu-td" - @click="change_role(selected_frame_argument_id ? selected_frame_argument_id.split('-')[1] : null, 'change_role')" - style="padding: 10px 15px 10px 15px; color: #000000;">Zmień rolę + + <td id="assign-schema" class="table-button-menu-td" @click="assign_schema()" + style="padding: 10px 15px 10px 15px; color: #000000;">Podłącz argument </td> <td id="assign-examples" class="table-button-menu-td" @click="attach_examples()" style="padding: 10px 15px 10px 15px; color: #000000;">Podłącz przykłady </td> + <td id="finish-frame-building" class="table-button-menu-td" @click="finish_frame_building()" + style="padding: 10px 15px 10px 15px; color: #000000;">Gotowe + </td> </tr> <tr style="background-color: white;"> - <td id="assign-schema" class="table-button-menu-td" @click="assign_schema()" - style="padding: 10px 15px 10px 15px; color: #000000;">Podłącz schemat + <td id="change-role" class="table-button-menu-td" + @click="change_role(selected_frame_argument_id ? selected_frame_argument_id.split('-')[1] : null, 'change_role')" + style="padding: 10px 15px 10px 15px; color: #000000;">Zmień rolę </td> <td id="remove-argument" class="table-button-menu-td" @click="remove_argument()" style="padding: 10px 15px 10px 15px; color: #000000;">Usuń argument </td> <td id="delete-schema" class="table-button-menu-td" @click="delete_schema_connections()" - style="padding: 10px 15px 10px 15px; color: #000000;">Rozłącz schemat + style="padding: 10px 15px 10px 15px; color: #000000;">Odłącz argument </td> - <td id="finish-frame-building" class="table-button-menu-td" @click="finish_frame_building()" - style="padding: 10px 15px 10px 15px; color: #000000;">Zakończ tworzenie nowej ramy + <td id="change-frame-opinion" class="table-button-menu-td" @click="change_frame_opinion()" + style="padding: 10px 15px 10px 15px; color: #000000;">Zmień opinię </td> </tr> </table> -- GitLab