From 5b661f8008a3b52e5db77f5bceae65ba34ab25ed Mon Sep 17 00:00:00 2001
From: dcz <dcz@ipipan.waw.pl>
Date: Wed, 29 Jun 2022 15:06:54 +0200
Subject: [PATCH] Change slowal frame status

---
 entries/static/entries/css/entries.css        |  7 ++
 .../js/components/LexicalUnitDisplay.js       |  9 +-
 .../entries/js/components/LexicalUnitEdit.js  | 91 +++++++++++++------
 entries/static/entries/js/entries.js          |  4 +-
 unifier/models.py                             |  1 -
 unifier/urls.py                               |  2 +-
 unifier/views.py                              | 20 ++--
 7 files changed, 88 insertions(+), 46 deletions(-)

diff --git a/entries/static/entries/css/entries.css b/entries/static/entries/css/entries.css
index e28fb44..c275d00 100644
--- a/entries/static/entries/css/entries.css
+++ b/entries/static/entries/css/entries.css
@@ -174,6 +174,13 @@ legend {
     background-repeat: repeat;
 }
 
+.active-frame {
+    border-style: dashed;
+    border-width: 2px;
+    border-color: #564c4c;
+    padding: 3px;
+}
+
 .sticky-bottom {
     position: sticky;
     bottom: 0;
diff --git a/entries/static/entries/js/components/LexicalUnitDisplay.js b/entries/static/entries/js/components/LexicalUnitDisplay.js
index 541c634..1a9e216 100644
--- a/entries/static/entries/js/components/LexicalUnitDisplay.js
+++ b/entries/static/entries/js/components/LexicalUnitDisplay.js
@@ -44,7 +44,10 @@ export default {
       }).then(() => { this.$emit('refresh'); });
     },
     isReadyToProcess() {
-      return (this.frame?.status === 'O' || this.frame?.status === 'G') && this.unifiedFrame?.status === 'O';
+      return this.isLeksykograf() || (this.frame?.status === 'O' || this.frame?.status === 'G') && this.unifiedFrame?.status === 'O';
+    },
+    isLeksykograf() {
+      return !has_permission("users.view_assignment");
     }
   },
   created () {
@@ -77,10 +80,10 @@ export default {
           <a class="btn btn-sm btn-outline-dark mr-2" v-if="frame?.status === 'N'" @click="take">{{ gettext('Pobierz') }}</a>
           <a
             class="btn btn-sm btn-outline-dark mr-2"
-            v-if="isReadyToProcess()"
+            v-if="isReadyToProcess() || isLeksykograf"
             @click="$emit('goToEdit', this.unifiedFrame.pk)"
           >
-            {{ gettext('Obrabiaj') }}
+            {{ isLeksykograf() ? gettext('Obrabiaj') : gettext('Sprawdź') }}
           </a>
           <a
             class="btn btn-sm btn-outline-dark mr-2"
diff --git a/entries/static/entries/js/components/LexicalUnitEdit.js b/entries/static/entries/js/components/LexicalUnitEdit.js
index d70808f..5bf1de3 100644
--- a/entries/static/entries/js/components/LexicalUnitEdit.js
+++ b/entries/static/entries/js/components/LexicalUnitEdit.js
@@ -81,6 +81,8 @@ Object.assign(LexicalUnitEdit, {
       right_pane_tab: this.initialRightPaneTab || 'schemata',
       currentPreviewedUnifiedFrameId: this.previewedUnifiedFrameId,
       internalForceRefresh: this.forceRefresh,
+      statusButtonTitle: '',
+      active_slowal_frame: null,
     }
   },
   components: {InfoTooltip, Spinner, FramePreview},
@@ -148,6 +150,8 @@ Object.assign(LexicalUnitEdit, {
             // tooltips with meaning gloss
             activate_tooltips($('#semantics-frames-pane'));
             update_last_visited(response.last_visited);
+
+            this.changeStatusButtonTitleToDefault();
           }.bind(this),
           error: function (request, errorType, errorMessage) {
             show_error(errorType + ' (' + errorMessage + ')');
@@ -466,7 +470,7 @@ Object.assign(LexicalUnitEdit, {
       $.prompt(duplicate_popup);
     },
     changeUnifiedFrameStatusToReady() {
-      let foundNotVerifiedFrame = this.frames.find(frame => !frame.verified);
+      let foundNotVerifiedFrame = this.frames.find(frame => frame.status !== 'G' && frame.status !== 'S');
       if (foundNotVerifiedFrame) {
         alert(gettext("Wszystkie podpięte ramy powinny być zweryfikowane."));
       } else {
@@ -541,32 +545,64 @@ Object.assign(LexicalUnitEdit, {
         });
       }
     },
-    change_slowal2unified_mapping_verification(slowal_frame_id, verified) {
-      var data = {
-        'unified_frame_id': this.unified_frame.id,
-        'slowal_frame_id': slowal_frame_id,
-        'verified': verified
-      };
-      $.ajax({
-        type: 'post',
-        url: '/' + lang + '/unifier/change_slowal2unified_mapping_verification/',
-        dataType: 'json',
-        data: data,
-        timeout: 60000,
-        success: function (response) {
-          show_info('Status ramy został zmieniony');
-          this.loadFrame();
-        }.bind(this),
-        error: function (request, errorType, errorMessage) {
-          show_error(errorType + ' (' + errorMessage + ')');
+    isFrameVerified(frame) {
+      const isSuperLeksykograf = has_permission("users.view_assignment");
+      return (!isSuperLeksykograf && frame.status === 'G') || (isSuperLeksykograf && frame.status === 'S')
+    },
+    change_slowal_frame_status() {
+      if(this.active_slowal_frame) {
+        let frame = this.active_slowal_frame;
+        const isSuperLeksykograf = has_permission("users.view_assignment");
+        let status = null;
+        if (!isSuperLeksykograf && frame.status === 'G') {
+          //przywracamy O
+          status = 'O';
+        } else if (!isSuperLeksykograf && frame.status === 'O') {
+          //ustawiany na Gotowe
+          status = 'G';
+        } else if (isSuperLeksykograf && frame.status === 'S') {
+          //ustawiany Sprawdzone
+          status = 'G';
+        } else {
+          status = 'S';
         }
-      });
+
+        var data = {
+          'unified_frame_id': this.unified_frame.id,
+          'slowal_frame_id': frame.id,
+          'status': status
+        };
+        $.ajax({
+          type: 'post',
+          url: '/' + lang + '/unifier/change_slowal_frame_status/',
+          dataType: 'json',
+          data: data,
+          timeout: 60000,
+          success: function (response) {
+            show_info('Status ramy został zmieniony');
+            this.loadFrame();
+          }.bind(this),
+          error: function (request, errorType, errorMessage) {
+            show_error(errorType + ' (' + errorMessage + ')');
+          }
+        });
+      } else {
+        alert(gettext("Wybierz ramę, dla której chcesz zmienić status."));
+      }
+    },
+    changeStatusButtonTitleToDefault() {
+      const isSuperLeksykograf = has_permission("users.view_assignment");
+      this.statusButtonTitle = isSuperLeksykograf ? 'Sprawdź' : 'Gotowe';
     },
     slowalFrameSelected(frame) {
       if (this.active_slowal_frame === frame) {
         this.active_slowal_frame = null;
+        this.changeStatusButtonTitleToDefault();
       } else {
         this.active_slowal_frame = frame;
+        if(this.isFrameVerified(frame)) {
+          this.statusButtonTitle = 'Przywróć';
+        }
       }
     },
     extract_frames_to_new_frame() {
@@ -654,6 +690,7 @@ Object.assign(LexicalUnitEdit, {
     },
   },
   mounted() {
+    this.changeStatusButtonTitleToDefault();
     !this.readOnly && Split(['#semantics-frames-pane', '#semantics-schemata-pane'], {
       sizes: [40, 60],
       minSize: 400,
@@ -782,7 +819,7 @@ Object.assign(LexicalUnitEdit, {
             Rozdziel
           </td>
           <td id="hide-slowal-frame" style="padding: 10px 15px 10px 15px; color: #000000;">Ukryj</td>
-          <td id="ready-slowal-frame" style="padding: 10px 15px 10px 15px; color: #000000;">Gotowe</td>
+          <td id="ready-slowal-frame" style="padding: 10px 15px 10px 15px; color: #000000;" @click="change_slowal_frame_status">{{ statusButtonTitle }}</td>
           <td id="filter-slowal-frames" style="padding: 10px 15px 10px 15px; color: #000000;">Filtruj</td>
         </tr>
         <tr style="background-color: white;">
@@ -810,21 +847,21 @@ Object.assign(LexicalUnitEdit, {
           <tbody>
           <tr>
             <th scope="row">
-              <div v-html="slowal_frame_html(frame)" @click="slowalFrameSelected(frame)"></div>
+              <div 
+                v-html="slowal_frame_html(frame)" 
+                @click="slowalFrameSelected(frame)"
+                :class="frame === active_slowal_frame ? 'active-frame' : ''"
+              ></div>
             </th>
               <th scope="row">
                 <div v-if="!readOnly">
                   <label v-bind:for="frame.id">
                     <div>
                       <input
-                        :id="frame.id"
                         type="checkbox"
-                        v-model="frame.verified"
-                        :checked="frame.verified"
+                        :checked="isFrameVerified(frame)"
                         class="custom-control custom-checkbox"
-                        @change="change_slowal2unified_mapping_verification(frame.id, frame.verified)"
                       >
-                      <span>Zweryfikowane</span>
                     </div>
                   </label>
                   <br><br>
diff --git a/entries/static/entries/js/entries.js b/entries/static/entries/js/entries.js
index ef204bb..71b1304 100644
--- a/entries/static/entries/js/entries.js
+++ b/entries/static/entries/js/entries.js
@@ -156,7 +156,9 @@ function frame2dom(frame) {
       var frame = $(this); \
       if (!frame.hasClass('active')) { \
         select_frame(frame); \
-      } \
+      } else { \
+            unselect_frame(frame); \
+        } \
       clear_info(); \
     ");
 
diff --git a/unifier/models.py b/unifier/models.py
index d617f6e..f6496d8 100644
--- a/unifier/models.py
+++ b/unifier/models.py
@@ -120,7 +120,6 @@ class UnifiedRelationalSelectionalPreference(models.Model):
 
 
 class UnifiedFrame2SlowalFrameMapping(models.Model):
-    verified = models.BooleanField(default=False)
     removed = models.BooleanField(default=False)
     unified_frame = models.ForeignKey(UnifiedFrame, related_name='unified_frame_2_slowal_frame', on_delete=models.PROTECT)
     slowal_frame = models.OneToOneField(Frame, related_name='slowal_frame_2_unified_frame', on_delete=models.PROTECT)
diff --git a/unifier/urls.py b/unifier/urls.py
index ba1b905..21fc47c 100644
--- a/unifier/urls.py
+++ b/unifier/urls.py
@@ -12,7 +12,7 @@ urlpatterns = [
     path('get_unified_frame/', views.get_unified_frame, name='get_unified_frame'),
     path('extract_frames_to_new_frame/', views.extract_frames_to_new_frame, name='extract_frames_to_new_frame'),
     path('change_slowal2unified_fram_argument_mapping/', views.change_slowal2unified_fram_argument_mapping, name='change_slowal2unified_fram_argument_mapping'),
-    path('change_slowal2unified_mapping_verification/', views.change_slowal2unified_mapping_verification, name='change_slowal2unified_mapping_verification'),
+    path('change_slowal_frame_status/', views.change_slowal_frame_status, name='change_slowal_frame_status'),
     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'),
diff --git a/unifier/views.py b/unifier/views.py
index 69696dc..cae2193 100644
--- a/unifier/views.py
+++ b/unifier/views.py
@@ -169,18 +169,13 @@ def get_examples(frames):
 
 def get_unified_frame_json(unifiedFrame, request):
 
-    slowal_frames_and_verified = [(connection.slowal_frame, connection.verified) for connection in unifiedFrame.unified_frame_2_slowal_frame.all()]
-
-    slowal_frames = [tuple[0] for tuple in slowal_frames_and_verified]
+    slowal_frames = [connection.slowal_frame for connection in unifiedFrame.unified_frame_2_slowal_frame.all()]
 
     all_schema_objects = Schema.objects.filter(schema_hooks__argument_connections__argument__frame__in=slowal_frames).distinct()
 
     slowal_frames_dict = []
-    for slowal_frame_and_verified in slowal_frames_and_verified:
-        slowal_frame = slowal_frame_and_verified[0]
-        verified = slowal_frame_and_verified[1]
+    for slowal_frame in slowal_frames:
         dict = frame2dict(slowal_frame, slowal_frame.lexical_units.all())
-        dict['verified'] = verified
         slowal_frames_dict.append(dict)
 
     alternations, realisation_phrases, realisation_descriptions = get_alternations(all_schema_objects, slowal_frames)
@@ -276,16 +271,15 @@ def change_slowal2unified_fram_argument_mapping(request):
     return JsonResponse({})
 
 @ajax_required
-def change_slowal2unified_mapping_verification(request):
+def change_slowal_frame_status(request):
     if request.method == 'POST':
         unified_frame_id = request.POST['unified_frame_id']
         slowal_frame_id = request.POST['slowal_frame_id']
-        verified = request.POST['verified']
-
-        unifiedFrame2SlowalFrameMapping = UnifiedFrame2SlowalFrameMapping.objects.get(unified_frame_id=unified_frame_id, slowal_frame=slowal_frame_id)
+        status = request.POST['status']
 
-        unifiedFrame2SlowalFrameMapping.verified = True if verified == 'true' else False
-        unifiedFrame2SlowalFrameMapping.save()
+        frame = Frame.objects.get(pk=slowal_frame_id)
+        frame.status = status
+        frame.save()
         return JsonResponse({})
     return JsonResponse({})
 
-- 
GitLab