From 0bddc382cce5a753cac1992d413f49aa2318ac65 Mon Sep 17 00:00:00 2001
From: dcz <dcz@ipipan.waw.pl>
Date: Tue, 10 Jan 2023 11:33:06 +0100
Subject: [PATCH] Additional fitlers for unified frames

---
 entries/forms.py          | 106 +++++++++++++++++++++++++++++++++++---
 entries/polish_strings.py |  23 +++++++++
 entries/views.py          |   4 +-
 unifier/models.py         |   3 ++
 unifier/views.py          |  17 ++++++
 5 files changed, 145 insertions(+), 8 deletions(-)

diff --git a/entries/forms.py b/entries/forms.py
index c0c3ec9..0ae5f8f 100644
--- a/entries/forms.py
+++ b/entries/forms.py
@@ -26,8 +26,8 @@ from semantics.models import (
     PredefinedSelectionalPreference, RelationalSelectionalPreference, SelectionalPreferenceRelation,
 )
 
-from meanings.models import Synset
-from unifier.models import UnifiedFrame
+from meanings.models import Synset, LexicalUnit
+from unifier.models import UnifiedFrame, UnifiedFrameArgument
 
 from .form_fields.generic_fields import (
     RangeFilter,
@@ -621,6 +621,10 @@ class UnifiedFrameFormFactory(FormFactory):
                 lookup='opinion',
             ), None,
         ),
+        (
+            None, None,
+            lambda n, cls: and_or_form_creator(_('Opnia podpiętych ram slowala'), 'add-slowal-frame-opinion-{}'.format(n), data_add='FrameOpinion'),
+        ),
         (
             'num_arguments',
             lambda: RangeFilter(
@@ -653,17 +657,40 @@ class UnifiedFrameFormFactory(FormFactory):
         ),
         (
             None, None,
-            lambda n, cls: and_or_form_creator(_('UnifiedFrameArgument'), 'add-argument-{}'.format(n), data_add='argument'),
+            lambda n, cls: and_or_form_creator(_('UnifiedFrameArgument'), 'add-unified-frame-argument-{}'.format(n), data_add='UnifiedArgument'),
         ),
     )
 
     @staticmethod
     def get_child_form_prefix(child_form):
-        if child_form.model_class == Argument:
-            return 'arguments__in'
+        if child_form.model_class == UnifiedFrameArgument:
+            return 'unified_arguments__in'
+        if child_form.model_class == Frame:
+            return 'unified_frame_2_slowal_frame__slowal_frame__in'
         raise KeyError(type(child_form))
 
 
+class FrameOpinionFormFactory(FormFactory):
+
+    form_class_name = 'FrameOpinionForm'
+    form_model = Frame
+    form_formtype = 'FrameOpinion'
+    form_header = _('Opinia ramy slowala')
+
+    field_makers = (
+        (
+            'opinion',
+            lambda: ModelMultipleChoiceFilter(
+                label=_('Opinia'),
+                queryset=FrameOpinion.objects.exclude(key='unk').filter(frame__isnull=False).distinct(),
+                key='key',
+                human_values=polish_strings.FRAME_OPINION(),
+                lookup='opinion',
+            ), None,
+        ),
+    )
+
+
 class ArgumentFormFactory(FormFactory):
     
     form_class_name = 'ArgumentForm'
@@ -726,6 +753,70 @@ class ArgumentFormFactory(FormFactory):
         raise KeyError(type(child_form))
 
 
+
+class UnifiedArgumentFormFactory(FormFactory):
+
+    form_class_name = 'UnifiedArgumentForm'
+    form_model = UnifiedFrameArgument
+    form_formtype = 'UnifiedArgument'
+    form_header = _('Argument semantyczny ramy zunifkikoanej')
+
+    field_makers = (
+        (
+            'role',
+            lambda: RoleNameFilter(
+                label=_('Rola'),
+                lookup='role__role',
+            ), None,
+        ),
+        (
+            'role_attribute',
+            lambda: RoleAttributeFilter(
+                label=_('Atrybut roli'),
+                lookup='role__attribute',
+            ), None,
+        ),
+        (
+            'num_preferences',
+            lambda: RangeFilter(
+                label=_('Liczba preferencyj selekcyjnych argumentu'),
+                lookup='preferences_count',
+            ), None
+        ),
+        (
+            'preference_type',
+            lambda: ChoiceFilter(
+                label=_('Preferencja selekcyjna'),
+                choices=(('', _('wybierz')), ('predefined', _('Predefiniowana grupa znaczeń')), ('relational', _('Wyrażona przez relację')), ('synset', _('Wyrażona przez jednostkę leksykalną Słowosieci'))),
+            ),
+            lambda n, cls: and_or_form_creator(_('Preferencja selekcyjna'), 'add-preference-{}'.format(n), field=cls.make_field(FormFactory.unique_name('preference_type', n)))
+        ),
+        # removing until we discuss whether this field is needed & how it should work
+        #(
+        #    None, None,
+        #    lambda n, cls: and_or_form_creator(_('Pozycja'), 'add-position-{}'.format(n), data_add='position'),
+        #),
+        (
+            'phrase_type',
+            lambda: PhraseTypeFilter(help_text=_('Typ frazy, przez którą może być realizowany argument.')),
+            lambda n, cls: and_or_form_creator(_('Fraza'), 'add-phrase-{}'.format(n), field=cls.make_field(FormFactory.unique_name('phrase_type', n)), data_prefix='phrase_'),
+        ),
+    )
+
+    @staticmethod
+    def get_child_form_prefix(child_form):
+        if child_form.model_class == PredefinedSelectionalPreference:
+            return 'predefined__in'
+        if child_form.model_class == RelationalSelectionalPreference:
+            return 'relations__in'
+        if child_form.model_class == Synset:
+            return 'synsets__in'
+        if child_form.model_class == PhraseType:
+            return 'unified_agrument_mapping__slowal_agrument__argument_connections__schema_connections__phrase_type__in'
+            # return 'argument_connections__schema_connections__phrase_type__in'
+        raise KeyError(type(child_form))
+
+
 class PredefinedPreferenceFormFactory(FormFactory):
     
     form_class_name = 'PredefinedPreferenceForm'
@@ -739,8 +830,9 @@ class PredefinedPreferenceFormFactory(FormFactory):
             lambda: ModelMultipleChoiceFilter(
                 label=_('Predefiniowane'),
                 queryset=PredefinedSelectionalPreference.objects.all(),
-                key='key',
-                lookup='key',
+                key='name',
+                human_values=polish_strings.PREDEFINED_SELECTIONAL_PREFERENCE(),
+                lookup='name',
             ), None,
         ),
     )
diff --git a/entries/polish_strings.py b/entries/polish_strings.py
index bdb37ce..4c70466 100644
--- a/entries/polish_strings.py
+++ b/entries/polish_strings.py
@@ -228,3 +228,26 @@ def EXAMPLE_OPINION():
         'wÄ…tpliwy' : _('wÄ…tpliwy'),
         'dobry'    : _('dobry'),
     }
+
+
+def PREDEFINED_SELECTIONAL_PREFERENCE():
+    return  {
+        'ALL'      : _('ALL'),
+        'LUDZIE'      : _('LUDZIE'),
+        'ISTOTY'      : _('ISTOTY'),
+        'PODMIOTY'      : _('PODMIOTY'),
+        'DOBRA'      : _('DOBRA'),
+        'KOMUNIKAT'      : _('KOMUNIKAT'),
+        'KONCEPCJA'      : _('KONCEPCJA'),
+        'WYTWÓR'      : _('WYTWÓR'),
+        'JADŁO'      : _('JADŁO'),
+        'MIEJSCE'      : _('MIEJSCE'),
+        'CZAS'      : _('CZAS'),
+        'OBIEKTY'      : _('OBIEKTY'),
+        'CECHA'      : _('CECHA'),
+        'CZYNNOŚĆ'      : _('CZYNNOŚĆ'),
+        'SYTUACJA'      : _('SYTUACJA'),
+        'KIEDY'      : _('KIEDY'),
+        'CZEMU'      : _('CZEMU'),
+        'ILOŚĆ'      : _('ILOŚĆ')
+    }
diff --git a/entries/views.py b/entries/views.py
index 8cacf58..659cfe4 100644
--- a/entries/views.py
+++ b/entries/views.py
@@ -38,7 +38,7 @@ from .forms import (
     ArgumentFormFactory,
     PredefinedPreferenceFormFactory,
     RelationalPreferenceFormFactory,
-    SynsetPreferenceFormFactory, UnifiedFrameFormFactory,
+    SynsetPreferenceFormFactory, UnifiedFrameFormFactory, FrameOpinionFormFactory, UnifiedArgumentFormFactory,
 )
 
 from .polish_strings import STATUS, POS, SCHEMA_OPINION, FRAME_OPINION, EXAMPLE_SOURCE, EXAMPLE_OPINION, RELATION
@@ -98,7 +98,9 @@ FORM_FACTORY_TYPES = {
     'lemma'      : LemmaFormFactory,
     'frame'      : FrameFormFactory,
     'unifiedframe'      : UnifiedFrameFormFactory,
+    'FrameOpinion'      : FrameOpinionFormFactory,
     'argument'   : ArgumentFormFactory,
+    'UnifiedArgument'   : UnifiedArgumentFormFactory,
     'predefined' : PredefinedPreferenceFormFactory,
     'relational' : RelationalPreferenceFormFactory,
     'synset'     : SynsetPreferenceFormFactory,
diff --git a/unifier/models.py b/unifier/models.py
index 1e5fd1e..a15ba60 100644
--- a/unifier/models.py
+++ b/unifier/models.py
@@ -130,6 +130,9 @@ class UnifiedFrameArgument(models.Model):
     #odwołanie do ramy
     unified_frame = models.ForeignKey(UnifiedFrame, related_name='unified_arguments', default=None, blank=True, null=True, on_delete=models.PROTECT)
 
+    #do wyszukiwania
+    preferences_count = models.PositiveIntegerField(null=False, default=0)
+
     def __str__(self):
         return str(self.role)
 
diff --git a/unifier/views.py b/unifier/views.py
index 733c626..9a12996 100644
--- a/unifier/views.py
+++ b/unifier/views.py
@@ -34,6 +34,9 @@ def save_synset_preference(request):
         unifiedFrameArgument = UnifiedFrameArgument.objects.get(unified_frame_id=int(frame_id), id=int(complement_id))
         unifiedFrameArgument.synsets.add(int(synset_preference_id))
         unifiedFrameArgument.save()
+
+        updateArgumentPreferencesCount(unifiedFrameArgument)
+
     return JsonResponse({})
 
 @ajax_required
@@ -47,6 +50,9 @@ def save_predefined_preference(request):
         unifiedFrameArgument = UnifiedFrameArgument.objects.get(unified_frame_id=int(frame_id), id=int(complement_id))
         unifiedFrameArgument.predefined.add(int(predefined_preference_id))
         unifiedFrameArgument.save()
+
+        updateArgumentPreferencesCount(unifiedFrameArgument)
+
     return JsonResponse({})
 
 @ajax_required
@@ -63,6 +69,9 @@ def save_relational_selectional_preference(request):
         relationalSelectionalPreference.save()
         unifiedFrameArgument.relations.add(relationalSelectionalPreference)
         unifiedFrameArgument.save()
+
+        updateArgumentPreferencesCount(unifiedFrameArgument)
+
     return JsonResponse({})
 
 
@@ -252,9 +261,17 @@ def remove_selectional_preference(request):
         unified_frame_argument.relations.set(unified_frame_argument.relations.exclude(id__in=preference_ids))
         unified_frame_argument.save()
 
+        updateArgumentPreferencesCount(unified_frame_argument)
+
         return JsonResponse({})
     return JsonResponse({})
 
+
+def updateArgumentPreferencesCount(unified_frame_argument):
+    unified_frame_argument.preferences_count = unified_frame_argument.predefined.count() + unified_frame_argument.synsets.count() + unified_frame_argument.relations.count();
+    unified_frame_argument.save()
+
+
 @ajax_required
 @transaction.atomic
 def duplicate_unified_frame(request):
-- 
GitLab