From 2432f4560b46edf7a615bf48b558bbe80c360a82 Mon Sep 17 00:00:00 2001
From: dcz <dcz@ipipan.waw.pl>
Date: Fri, 3 Nov 2023 09:23:51 +0100
Subject: [PATCH] Schema to argument connection creation bugfix.

---
 .../static/entries/css/unification_frames.css |  2 +-
 entries/views.py                              |  6 ++-
 freelus/views.py                              | 54 ++++++++++++-------
 .../unification/free_lu/FreeLuEdit.vue        | 27 +++++++---
 .../SemanticsSchemataComponent.vue            |  8 +--
 5 files changed, 64 insertions(+), 33 deletions(-)

diff --git a/entries/static/entries/css/unification_frames.css b/entries/static/entries/css/unification_frames.css
index b4efb72..5b851fa 100644
--- a/entries/static/entries/css/unification_frames.css
+++ b/entries/static/entries/css/unification_frames.css
@@ -32,5 +32,5 @@ table.table-button-menu {
 }
 
 #free-lus-frame .argument.active {
-    background-color: #dee1e4;
+    background-color: black;
 }
diff --git a/entries/views.py b/entries/views.py
index 17a2ac5..2f0a797 100644
--- a/entries/views.py
+++ b/entries/views.py
@@ -99,6 +99,7 @@ def hierarchy(request):
     if "unified_frame_id" in request.GET:
         unified_frame_id = request.GET.get("unified_frame_id")
 
+    user = request.user
     return render(
         request,
         'hierarchy.html',
@@ -109,6 +110,7 @@ def hierarchy(request):
             'frames_form': FrameFormFactory.get_form(as_subform=False),
             'schemata_form': SchemaFormFactory.get_form(as_subform=False),
             'unified_frames_form': UnifiedFrameFormFactory.get_form(as_subform=False),
+            'is_superlexicograf': user.groups.filter(name=settings.SUPER_LEXICOGRAPHS_GROUP_NAME).exists()
         },
     )
 
@@ -120,6 +122,7 @@ def lu_free(request):
     if "unified_frame_id" in request.GET:
         unified_frame_id = request.GET.get("unified_frame_id")
 
+    user = request.user
     return render(
         request,
         'lu_free.html',
@@ -130,6 +133,7 @@ def lu_free(request):
             'frames_form': FrameFormFactory.get_form(as_subform=False),
             'schemata_form': SchemaFormFactory.get_form(as_subform=False),
             'unified_frames_form': UnifiedFrameFormFactory.get_form(as_subform=False),
+            'is_superlexicograf': user.groups.filter(name=settings.SUPER_LEXICOGRAPHS_GROUP_NAME).exists()
         },
     )
 
@@ -925,7 +929,7 @@ def get_entry(request):
                     schemata.append(schema2dict(schema, subentry.negativity, request.LANGUAGE_CODE))
                 if schemata:
                     all_schema_objects += list(schema_objects)
-                    subentries.append({ 'str' : subentry2str(subentry), 'schemata' : schemata })
+                    subentries.append({'str': subentry2str(subentry), 'id': subentry.id, 'schemata': schemata})
             # frame_objects = Frame.objects.filter(arguments__argument_connections__schema_connections__subentry__entry=entry).distinct()
             frame_objects = Frame.objects.filter(lexical_units__entry=entry).distinct()
             # filter out frames by frame properties
diff --git a/freelus/views.py b/freelus/views.py
index d492fe0..c47b6f4 100644
--- a/freelus/views.py
+++ b/freelus/views.py
@@ -7,6 +7,7 @@ from connections.models import Entry, Status, ExampleConnection, SchemaHook, Arg
 from meanings.models import LexicalUnit
 from semantics.models import Frame, FrameOpinion, Argument, ArgumentRole, SemanticRole, \
     RoleAttribute, RoleSubAttribute, RoleType
+from syntax.models import Position
 
 
 @ajax_required
@@ -159,28 +160,43 @@ def attach_schema_to_argument(request):
     :return: Empty json response
     """
     if request.method == 'POST':
-        frame_id = request.POST['frame_id']
-        argument_id = request.POST['argument_id']
-        schema_id = request.POST['schema_id']
-        schema_position_id = request.POST['schema_position_id']
+        frame_id = int(request.POST['frame_id'])
+        subentry_id = int(request.POST['subentry_id'])
+        argument_id = int(request.POST['argument_id'])
+        schema_id = int(request.POST['schema_id'])
+        schema_position_id = int(request.POST['schema_position_id'])
+        schema_alternation_id = int(request.POST['schema_alternation_id'])
 
         argument = Argument.objects.get(id=argument_id)
 
-        schema_hooks = SchemaHook.objects.filter(schema_id=schema_id, position_id=schema_position_id)
-        if len(schema_hooks) > 0:
-            schema_hook = schema_hooks[0]
-            argument_connection, xxx = ArgumentConnection.objects.get_or_create(argument=argument,
-                                                                                defaults={'argument': argument})
-            argument_connection.save()
-            argument_connection.schema_connections.add(schema_hook)
-            argument_connection.save()
-
-            RealisationDescription.objects.get_or_create(frame_id=frame_id,
-                                                         schema_id=schema_id,
-                                                         defaults={'alternation': 1,
-                                                                   'description': ''})
-
-    return JsonResponse({})
+        if not ArgumentConnection.objects.filter(argument=argument, schema_connections__schema_id=schema_id).exists():
+            position = Position.objects.get(id=schema_position_id)
+            for phrase_type in position.phrase_types.all():
+                schema_hooks = SchemaHook.objects.filter(subentry_id=subentry_id, schema_id=schema_id,
+                                                             position_id=schema_position_id, phrase_type=phrase_type,
+                                                             alternation=schema_alternation_id)
+                if schema_hooks.count() > 0:
+                    schema_hook = schema_hooks[0]
+                else:
+                    schema_hook = SchemaHook.objects.create(subentry_id=subentry_id, schema_id=schema_id,
+                                                             position_id=schema_position_id, phrase_type=phrase_type,
+                                                             alternation=schema_alternation_id)
+
+                argument_connection, _ = ArgumentConnection.objects.get_or_create(argument=argument,
+                                                                                    defaults={'argument': argument})
+                argument_connection.save()
+                argument_connection.schema_connections.add(schema_hook)
+                argument_connection.save()
+
+                RealisationDescription.objects.get_or_create(frame_id=frame_id,
+                                                             schema_id=schema_id,
+                                                             defaults={'alternation': schema_alternation_id,
+                                                                       'description': ''})
+            return JsonResponse({'succ': True})
+        else:
+            return JsonResponse({'succ': False, 'error': 'Odrzucono próbę połączenia. Argument jest już podłączony do innej pozycji w wybranym schemacie.'})
+    else:
+        return JsonResponse({'succ': False, 'error': 'Not a Post request.'})
 
 
 @ajax_required
diff --git a/frontend/src/components/unification/free_lu/FreeLuEdit.vue b/frontend/src/components/unification/free_lu/FreeLuEdit.vue
index dc58af9..e47ee85 100644
--- a/frontend/src/components/unification/free_lu/FreeLuEdit.vue
+++ b/frontend/src/components/unification/free_lu/FreeLuEdit.vue
@@ -38,6 +38,8 @@
                 lexicalUnitsVisible: true,
                 selected_frame_argument_id: null,
                 selected_schemata_position_id: null,
+                selected_schemata_position_alternation_id: null,
+                selected_schemata_subentry_id: null,
             }
         },
         components: {
@@ -127,8 +129,10 @@
             schemataSelected(schemas) {
                 this.selectedSchemas = schemas;
             },
-            selectSchemaPositionSelected(selected_schemata_position_id) {
+            selectSchemaPositionSelected(selected_schemata_position_id, subentry, alternation_id) {
                 this.selected_schemata_position_id = selected_schemata_position_id;
+                this.selected_schemata_position_alternation_id = alternation_id;
+                this.selected_schemata_subentry_id = subentry.id;
             },
             exampleSelected(selectedExamples) {
                 this.selectedExamples = selectedExamples;
@@ -373,10 +377,12 @@
             assign_schema() {
                 if (this.frame_in_progress && this.selected_schemata_position_id && this.selected_frame_argument_id) {
                     const data = {
-                        'frame_id': this.frame_in_progress.id,
-                        'argument_id': this.selected_frame_argument_id.split('-')[1],
-                        'schema_id': this.selectedSchemas[0].id,
-                        'schema_position_id': this.selected_schemata_position_id.split('-')[1]
+                        'frame_id': parseInt(this.frame_in_progress.id),
+                        'subentry_id': parseInt(this.selected_schemata_subentry_id),
+                        'argument_id': parseInt(this.selected_frame_argument_id.split('-')[1]),
+                        'schema_id': parseInt(this.selectedSchemas[0].id),
+                        'schema_position_id': parseInt(this.selected_schemata_position_id.split('-')[1]),
+                        'schema_alternation_id': (parseInt(this.selected_schemata_position_alternation_id)+1)
                     };
                     $.ajax({
                         type: 'post',
@@ -385,9 +391,14 @@
                         data: data,
                         timeout: 60000,
                         success: function (response) {
-                            alert(gettext("Argument ramy został powiązany z tybranym argumentem schematu."));
-                            show_info('Argument ramy został powiązany z tybranym argumentem schematu.');
-                            this.loadEntry();
+                            if(response['succ'] == true) {
+                                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 {
+                                alert(gettext(response['error']));
+                                show_info(response['error']);
+                            }
                             $.prompt.close();
                         }.bind(this),
                         error: function (request, errorType, errorMessage) {
diff --git a/frontend/src/components/unification/shared/frame-components/SemanticsSchemataComponent.vue b/frontend/src/components/unification/shared/frame-components/SemanticsSchemataComponent.vue
index 3f89bcb..92ce2d5 100644
--- a/frontend/src/components/unification/shared/frame-components/SemanticsSchemataComponent.vue
+++ b/frontend/src/components/unification/shared/frame-components/SemanticsSchemataComponent.vue
@@ -103,7 +103,7 @@
                 });
                 this.$emit('schemataSelected', selected);
             },
-            selectSchemaPosition(schema, position) {
+            selectSchemaPosition(schema, position, subentry, alternation_id) {
                 this.subentries.forEach(subentry => {
                     subentry.schemata.forEach(s => {
                         s.selected = false;
@@ -111,7 +111,7 @@
                 });
                 if(this.selected_schemata_position_id != position.id) {
                     this.selected_schemata_position_id = position.id;
-                    this.$emit('schemaPositionSelected', this.selected_schemata_position_id);
+                    this.$emit('schemaPositionSelected', this.selected_schemata_position_id, subentry, alternation_id);
                     this.$emit('schemataSelected', [schema]);
                 } else {
                     this.selected_schemata_position_id = null;
@@ -172,7 +172,7 @@
                                     <tr class="phrase-types alt-0">
                                         <th scope="row" class="py-0 px-1 text-secondary">Typy fraz</th>
                                         <td v-for="position in schema.positions" class="px-0 py-0 border-top border-left border-secondary"
-                                            @click.stop="selectSchemaPosition(schema, position)"
+                                            @click.stop="selectSchemaPosition(schema, position, subentry, index)"
                                             @mouseenter="position.hover=true"
                                             @mouseleave="position.hover=false"
                                             :class="computePositionCSS(position)">
@@ -194,7 +194,7 @@
                                 <tr class="phrase-types alt-0">
                                     <th scope="row" class="py-0 px-1 text-secondary">Typy fraz</th>
                                     <td v-for="position in schema.positions" class="px-0 py-0 border-top border-left border-secondary"
-                                        @click.stop="selectSchemaPosition(schema, position)"
+                                        @click.stop="selectSchemaPosition(schema, position, subentry, 0)"
                                         @mouseenter="position.hover=true"
                                         @mouseleave="position.hover=false"
                                         :class="computePositionCSS(position)">
-- 
GitLab