From addfd2469c465226c34e009d008b4cdd0a5e670d Mon Sep 17 00:00:00 2001
From: dcz <dcz@ipipan.waw.pl>
Date: Thu, 22 Dec 2022 15:07:15 +0100
Subject: [PATCH] Additional fitlers for unified frames

---
 entries/forms.py                           | 15 +++++++++++++++
 importer/unification/UnifiedFrameImport.py |  4 ++++
 unifier/models.py                          | 18 ++++++++++++++++++
 unifier/views.py                           |  5 +++++
 4 files changed, 42 insertions(+)

diff --git a/entries/forms.py b/entries/forms.py
index 2384da9..c0c3ec9 100644
--- a/entries/forms.py
+++ b/entries/forms.py
@@ -628,6 +628,13 @@ class UnifiedFrameFormFactory(FormFactory):
                 lookup='arguments_count',
             ), None
         ),
+        (
+            'num_frames',
+            lambda: RangeFilter(
+                label=_('Liczba ram semantycznych'),
+                lookup='slowal_frames_count',
+            ), None
+        ),
         (
             'name',
             lambda: RegexFilter(
@@ -636,6 +643,14 @@ class UnifiedFrameFormFactory(FormFactory):
                 lookup='title',
             ), None,
         ),
+        (
+            'lemmas',
+            lambda: RegexFilter(
+                label=_('Lematy jednostek leksykalnych'),
+                max_length=200,
+                lookup='unified_frame_2_slowal_frame__slowal_frame__lexical_units__base',
+            ), None,
+        ),
         (
             None, None,
             lambda n, cls: and_or_form_creator(_('UnifiedFrameArgument'), 'add-argument-{}'.format(n), data_add='argument'),
diff --git a/importer/unification/UnifiedFrameImport.py b/importer/unification/UnifiedFrameImport.py
index bdf8deb..e8c4dd4 100644
--- a/importer/unification/UnifiedFrameImport.py
+++ b/importer/unification/UnifiedFrameImport.py
@@ -86,5 +86,9 @@ class UnifiedFrameImport:
                     if subnode._name == 'slowal_frame':
                         UnifiedFrameImport.storeSlowalFrame(subnode, unifiedFrame, argumentIdsMapping)
 
+        unifiedFrame.arguments_count = len(argumentIdsMapping)
+        unifiedFrame.update_sloval_frame_count()
+        unifiedFrame.save()
+
         return unifiedFrame.pk
 
diff --git a/unifier/models.py b/unifier/models.py
index e31db9f..1e5fd1e 100644
--- a/unifier/models.py
+++ b/unifier/models.py
@@ -27,6 +27,10 @@ class UnifiedFrame(models.Model):
 
     assignments = GenericRelation("users.Assignment", content_type_field="subject_ct", object_id_field="subject_id")
 
+    arguments_count = models.PositiveIntegerField(null=False, default=0)
+
+    slowal_frames_count = models.PositiveIntegerField(null=False, default=0)
+
     objects = models.Manager.from_queryset(UnifiedFrameQueryset)()
 
     def sorted_arguments(self):  # TODO: zaimplementowac wlasciwe sortowanie
@@ -42,6 +46,7 @@ class UnifiedFrame(models.Model):
         new_unified_frame_arguments = None
         if not new_frame:
             new_frame = UnifiedFrame.objects.create()
+            new_frame.arguments_count = self.arguments_count
             new_frame.save()
             unified_frame_arguments = UnifiedFrameArgument.objects.filter(unified_frame=self)
             old_2_new_argument_mapping = {}
@@ -71,6 +76,10 @@ class UnifiedFrame(models.Model):
                     argument_mapping.unified_agrument_id = new_unified_frame_arguments[i]
                 argument_mapping.save()
 
+
+        self.update_sloval_frame_count()
+        new_frame.update_sloval_frame_count()
+
         # curr_mapping = UnifiedFrame2SlowalFrameMapping.objects.filter(unified_frame=self).all()
         # if len(curr_mapping) == 0:
         #     unified_frame_arguments = UnifiedFrameArgument.objects.filter(unified_frame=self).all()
@@ -79,11 +88,20 @@ class UnifiedFrame(models.Model):
 
         return new_frame
 
+    @transaction.atomic
+    def update_sloval_frame_count(
+            self, new_frame_title: Optional = None
+    ) -> "UnifiedFrame":
+        self.slowal_frames_count = self.unified_frame_2_slowal_frame.count()
+        self.save()
+
     @transaction.atomic
     def duplicate(
             self, new_frame_title: Optional = None
     ) -> "UnifiedFrame":
         new_frame = UnifiedFrame.objects.create(title=new_frame_title)
+        new_frame.arguments_count = self.arguments_count
+        new_frame.slowal_frames_count = self.slowal_frames_count
         new_frame.save()
         unified_frame_arguments = UnifiedFrameArgument.objects.filter(unified_frame=self)
         for unified_frame_argument in unified_frame_arguments:
diff --git a/unifier/views.py b/unifier/views.py
index 8de7272..733c626 100644
--- a/unifier/views.py
+++ b/unifier/views.py
@@ -375,6 +375,8 @@ def add_argument(request):
         unifiedFrame = UnifiedFrame.objects.get(pk=unified_frame_id)
         newUnifiedFrameArgument = UnifiedFrameArgument.objects.create(unified_frame=unifiedFrame)
         newUnifiedFrameArgument.save()
+        unifiedFrame.arguments_count = unifiedFrame.arguments_count + 1
+        unifiedFrame.save()
     return JsonResponse({})
 
 @ajax_required
@@ -386,6 +388,9 @@ def remove_argument(request):
 
         newUnifiedFrameArgument = UnifiedFrameArgument.objects.get(id=complement_id)
         newUnifiedFrameArgument.delete()
+        unifiedFrame = UnifiedFrame.objects.get(pk=unified_frame_id)
+        unifiedFrame.arguments_count = unifiedFrame.arguments_count - 1
+        unifiedFrame.save()
     return JsonResponse({})
 
 @ajax_required
-- 
GitLab