From de79440b66f2bb2d9cd44df6988877ca2573cf64 Mon Sep 17 00:00:00 2001
From: dcz <dcz@ipipan.waw.pl>
Date: Mon, 27 Jun 2022 23:06:52 +0200
Subject: [PATCH] Add/remove unified frame argument

---
 .../entries/js/components/LexicalUnitEdit.js  | 59 ++++++++++++++++++-
 .../management/commands/import_unification.py | 13 ++++
 unifier/models.py                             |  2 +-
 unifier/urls.py                               |  2 +
 unifier/views.py                              | 24 +++++++-
 5 files changed, 96 insertions(+), 4 deletions(-)

diff --git a/entries/static/entries/js/components/LexicalUnitEdit.js b/entries/static/entries/js/components/LexicalUnitEdit.js
index 3ff5ad3..5a95284 100644
--- a/entries/static/entries/js/components/LexicalUnitEdit.js
+++ b/entries/static/entries/js/components/LexicalUnitEdit.js
@@ -272,6 +272,61 @@ export default {
         };
         $.prompt(change_role_popup);
       }
+    },
+    addArgument() {
+        var data = { 'unified_frame_id' : this.unified_frame.id};
+        $.ajax({
+          type     : 'post',
+          url      : '/' + lang + '/unifier/add_argument/',
+          dataType : 'json',
+          data     : data,
+          timeout  : 60000,
+          success  : function(response) {
+            show_info('Nowy argument zosał dodany');
+            this.loadFrame();
+          }.bind(this),
+          error: function(request, errorType, errorMessage) {
+            show_error(errorType + ' (' + errorMessage + ')');
+            $.prompt.close();
+          }
+        });
+    },
+    removeArgument() {
+      if (!this.active_unified_frame_argument) {
+        alert(gettext("Zaznacz argument, który chcesz usunąć."));
+      } else {
+        let hasSlowalFrameArgumentMapping = false;
+        for (var i in this.unified_frame.slowal_frame_mapping) {
+          var slowal_frame_mapping = this.unified_frame.slowal_frame_mapping[i];
+          for (var j in slowal_frame_mapping.slowal_frame_argument_mapping) {
+            var slowal_frame_argument_mapping = slowal_frame_mapping.slowal_frame_argument_mapping[j];
+            if (slowal_frame_argument_mapping.unified_frame_agrument_id == this.active_unified_frame_argument.id) {
+              hasSlowalFrameArgumentMapping = true;
+              break;
+            }
+          }
+        }
+        if (hasSlowalFrameArgumentMapping) {
+          alert(gettext("Zaznaczony argument nie może zostać usunięty - podpięte ramy posiadają do niego dowiązania."));
+        } else {
+          var data = {'unified_frame_id': this.unified_frame.id, 'complement_id': this.active_unified_frame_argument.id};
+          $.ajax({
+            type: 'post',
+            url: '/' + lang + '/unifier/remove_argument/',
+            dataType: 'json',
+            data: data,
+            timeout: 60000,
+            success: function (response) {
+              show_info('Wybrany argument zosał usunięty');
+              this.loadFrame();
+            }.bind(this),
+            error: function (request, errorType, errorMessage) {
+              show_error(errorType + ' (' + errorMessage + ')');
+              $.prompt.close();
+            }
+          });
+        }
+      }
     }
   },
   mounted () {
@@ -292,14 +347,14 @@ export default {
       <table class="table-button-menu" cellspacing="1">
         <tr style="background-color: white;">
           <td id="change-title" @click="changeTitle" style="padding: 10px 15px 10px 15px; color: #000000;">Zmień nazwę</td>
-          <td id="add-arg" style="padding: 10px 15px 10px 15px; color: #000000;">Dodaj argum.</td>
+          <td id="add-arg" @click="addArgument" style="padding: 10px 15px 10px 15px; color: #000000;">Dodaj argum.</td>
           <td style="padding: 10px 15px 10px 15px; color: #000000;" @click="addSelectivePreference">Dodaj prefer.</td>
           <td id="merge" style="padding: 10px 15px 10px 15px; color: #000000;">Scal</td>
           <td style="padding: 10px 15px 10px 15px; color: #000000;" @click="$emit('goToDisplay')">Gotowe</td>
         </tr>
         <tr style="background-color: white;">
           <td id="change-role" @click="changeRole" style="padding: 10px 15px 10px 15px; color: #000000;">Zmień rolę</td>
-          <td id="remove-arg" style="padding: 10px 15px 10px 15px; color: #000000;">Usuń argum.</td>
+          <td id="remove-arg" @click="removeArgument" style="padding: 10px 15px 10px 15px; color: #000000;">Usuń argum.</td>
           <td id="change-windows" style="padding: 10px 15px 10px 15px; color: #000000;">Zamień okna</td>
           <td id="duplicates" style="padding: 10px 15px 10px 15px; color: #000000;">Duplikuj</td>
           <td id="save-changes" style="padding: 10px 15px 10px 15px; color: #000000;">Zapisz</td>
diff --git a/syntax/management/commands/import_unification.py b/syntax/management/commands/import_unification.py
index a842327..0283141 100644
--- a/syntax/management/commands/import_unification.py
+++ b/syntax/management/commands/import_unification.py
@@ -5,9 +5,16 @@ from django.core.management.base import BaseCommand
 
 import os, logging
 from xml.sax import handler, make_parser
+
+from django.db.models import Max
+
 from importer.unification.UnificationPreprocessXML import UnificationPreprocessHandler
 from shellvalier.settings import BASE_DIR
 from common.models import ImportInProgress
+from django.db import connection
+
+from unifier.models import UnifiedFrameArgument
+
 
 class Command(BaseCommand):
     args = 'none'
@@ -30,4 +37,10 @@ def import_unification():
     parser.setContentHandler(UnificationPreprocessHandler())
     parser.parse(xml_path)
 
+    max_id = UnifiedFrameArgument.objects.aggregate(Max('id'))['id__max']+1
+
+    trigger_sql = "ALTER SEQUENCE unifier_unifiedframeargument_id_seq RESTART "+str(max_id)+";"
+    cursor = connection.cursor()
+    cursor.execute(trigger_sql)
+
     ImportInProgress.objects.all().delete()
diff --git a/unifier/models.py b/unifier/models.py
index 4962b3e..e804f17 100644
--- a/unifier/models.py
+++ b/unifier/models.py
@@ -79,7 +79,7 @@ class UnifiedFrame(models.Model):
 
 
 class UnifiedFrameArgument(models.Model):
-    role_type = models.ForeignKey(RoleType, on_delete=models.PROTECT)
+    role_type = models.ForeignKey(RoleType, on_delete=models.PROTECT, default=None, blank=True, null=True)
     #rola - wybrana przez użytkownika
     role = models.ForeignKey(ArgumentRole, on_delete=models.PROTECT, default=None, blank=True, null=True)
     #role zaproponowane przez system unifikacyjny
diff --git a/unifier/urls.py b/unifier/urls.py
index 166cca0..ee3a35c 100644
--- a/unifier/urls.py
+++ b/unifier/urls.py
@@ -16,5 +16,7 @@ urlpatterns = [
     path('save_unified_frame_title/', views.save_unified_frame_title, name='save_unified_frame_title'),
     path('save_selected_role/', views.save_selected_role, name='save_selected_role'),
     path('save_new_role/', views.save_new_role, name='save_new_role'),
+    path('add_argument/', views.add_argument, name='add_argument'),
+    path('remove_argument/', views.remove_argument, name='remove_argument'),
 
 ]
diff --git a/unifier/views.py b/unifier/views.py
index 105092a..204c2d1 100644
--- a/unifier/views.py
+++ b/unifier/views.py
@@ -104,7 +104,7 @@ def unifiedFrame2dict(frame):
                     'str': '{}{}'.format(a.role.role.role.lower(), ' ' + a.role.attribute.attribute.lower() if a.role.attribute else '') if a.role is not None else None,
                     'id': str(a.role.id)
                 } if a.role is not None else None,
-                'role_type'   : a.role_type.type.lower(),
+                'role_type'   : a.role_type.type.lower() if a.role_type is not None else '',
                 'preferences' : get_prefs_list(a),
                 'proposed_roles': [{
                     'str': '{}{}'.format(r.role.role.lower(), ' ' + r.attribute.attribute.lower() if r.attribute else ''),
@@ -318,3 +318,25 @@ def save_new_role(request):
         unifiedFrameArgument.role = argumentRole
         unifiedFrameArgument.save()
     return JsonResponse({})
+
+@ajax_required
+@transaction.atomic
+def add_argument(request):
+    if request.method == 'POST':
+        unified_frame_id = request.POST['unified_frame_id']
+
+        unifiedFrame = UnifiedFrame.objects.get(pk=unified_frame_id)
+        newUnifiedFrameArgument = UnifiedFrameArgument.objects.create(unified_frame=unifiedFrame)
+        newUnifiedFrameArgument.save()
+    return JsonResponse({})
+
+@ajax_required
+@transaction.atomic
+def remove_argument(request):
+    if request.method == 'POST':
+        unified_frame_id = request.POST['unified_frame_id']
+        complement_id = request.POST['complement_id']
+
+        newUnifiedFrameArgument = UnifiedFrameArgument.objects.get(id=complement_id)
+        newUnifiedFrameArgument.delete()
+    return JsonResponse({})
-- 
GitLab