From 3f7c2554ec09f1fe2cd0292409df129f9cf7525a Mon Sep 17 00:00:00 2001
From: dcz <dcz@ipipan.waw.pl>
Date: Thu, 24 Aug 2023 13:56:43 +0200
Subject: [PATCH] Check title uniqness of unified frame.

---
 .../Unification/LexicalUnitEdit.vue           | 78 +++++++++++++++----
 .../unification/shared/LexicalUnitDisplay.vue | 57 +++++++++-----
 unifier/urls.py                               |  1 +
 unifier/views.py                              | 42 ++++++----
 4 files changed, 128 insertions(+), 50 deletions(-)

diff --git a/frontend/src/components/unification/Unification/LexicalUnitEdit.vue b/frontend/src/components/unification/Unification/LexicalUnitEdit.vue
index 6b5f8f8..ebeff6a 100644
--- a/frontend/src/components/unification/Unification/LexicalUnitEdit.vue
+++ b/frontend/src/components/unification/Unification/LexicalUnitEdit.vue
@@ -235,19 +235,41 @@ Object.assign(LexicalUnitEdit, {
               if (this.hasWhiteSpace(title)) {
                 alert(gettext("Nazwa zunifikowanej ramy nie może zawierać białych znaków."));
               } else {
-                const data = {'unified_frame_id': this.unified_frame.id, 'unified_frame_title': title};
+
+                const dataCheckUniq = {'unified_frame_title': title};
                 $.ajax({
                   type: 'post',
-                  url: '/' + lang + '/unifier/save_unified_frame_title/',
+                  url: '/' + lang + '/unifier/check_unified_frame_title_uniq/',
                   dataType: 'json',
-                  data: data,
+                  data: dataCheckUniq,
                   timeout: 60000,
                   success: function (response) {
-                    show_info('Tytuł ramy zosał zapisany');
-                    $.prompt.close();
-                    this.loadFrame();
+
+                    if(response.exists === false) {
+                      const data = {'unified_frame_id': this.unified_frame.id, 'unified_frame_title': title};
+                      $.ajax({
+                        type: 'post',
+                        url: '/' + lang + '/unifier/save_unified_frame_title/',
+                        dataType: 'json',
+                        data: data,
+                        timeout: 60000,
+                        success: function (response) {
+                          alert(gettext("Nazwa ramy zosała zapisana."));
+                          show_info('Nazwa ramy zosała zapisana.');
+                          $.prompt.close();
+                          this.loadFrame();
+                        }.bind(this),
+                        error: function (request, errorType, errorMessage) {
+                          show_error(errorType + ' (' + errorMessage + ')');
+                          $.prompt.close();
+                        }
+                      });
+                    } else {
+                      alert(gettext("Nazwa zunifikowanej ramy nie jest unikalna. Proszę wybrać inną nazwę."));
+                    }
                   }.bind(this),
                   error: function (request, errorType, errorMessage) {
+                    alert(gettext("Wystapił błąd: " + errorType + ' (' + errorMessage + ')'));
                     show_error(errorType + ' (' + errorMessage + ')');
                     $.prompt.close();
                   }
@@ -504,23 +526,43 @@ Object.assign(LexicalUnitEdit, {
               if (this.hasWhiteSpace(title)) {
                 alert(gettext("Nazwa zunifikowanej ramy nie może zawierać białych znaków."));
               } else {
-                const data = {
-                  'unified_frame_id': this.unified_frame.id,
-                  'target_unified_frame_title': title
-                };
+                const dataCheckUniq = {'unified_frame_title': title};
                 $.ajax({
                   type: 'post',
-                  url: '/' + lang + '/unifier/duplicate_unified_frame/',
+                  url: '/' + lang + '/unifier/check_unified_frame_title_uniq/',
                   dataType: 'json',
-                  data: data,
+                  data: dataCheckUniq,
                   timeout: 60000,
                   success: function (response) {
-                    show_info('Zunifikowana rama została zduplikowana.');
-                    this.currentPreviewedUnifiedFrameId = response.unified_frame_id;
-                    this.$emit('refreshEntriesList');
-                    $.prompt.close();
+
+                    if(response.exists === false) {
+                      const data = {
+                        'unified_frame_id': this.unified_frame.id,
+                        'target_unified_frame_title': title
+                      };
+                      $.ajax({
+                        type: 'post',
+                        url: '/' + lang + '/unifier/duplicate_unified_frame/',
+                        dataType: 'json',
+                        data: data,
+                        timeout: 60000,
+                        success: function (response) {
+                          show_info('Zunifikowana rama została zduplikowana.');
+                          this.currentPreviewedUnifiedFrameId = response.unified_frame_id;
+                          this.$emit('refreshEntriesList');
+                          $.prompt.close();
+                        }.bind(this),
+                        error: function (request, errorType, errorMessage) {
+                          show_error(errorType + ' (' + errorMessage + ')');
+                          $.prompt.close();
+                        }
+                      });
+                    } else {
+                      alert(gettext("Nazwa zunifikowanej ramy nie jest unikalna. Proszę wybrać inną nazwę."));
+                    }
                   }.bind(this),
                   error: function (request, errorType, errorMessage) {
+                    alert(gettext("Wystapił błąd: " + errorType + ' (' + errorMessage + ')'));
                     show_error(errorType + ' (' + errorMessage + ')');
                     $.prompt.close();
                   }
@@ -565,10 +607,12 @@ Object.assign(LexicalUnitEdit, {
             data: data,
             timeout: 60000,
             success: function (response) {
+              alert(gettext("Status ramy zunifikowanej został zmieniony."));
               show_info('Status ramy został zmieniony');
               this.loadFrame();
             }.bind(this),
             error: function (request, errorType, errorMessage) {
+              alert(gettext("Wystąpił błąd: " + errorType + ' (' + errorMessage + ')'));
               show_error(errorType + ' (' + errorMessage + ')');
             }
           });
@@ -641,11 +685,13 @@ Object.assign(LexicalUnitEdit, {
           timeout: 60000,
           success: function (response) {
             show_info('Status ramy został zmieniony');
+            alert(gettext("Status ramy został zmieniony."));
             this.$emit('refreshEntriesList');
             this.loadFrame();
           }.bind(this),
           error: function (request, errorType, errorMessage) {
             show_error(errorType + ' (' + errorMessage + ')');
+            alert(gettext("Status ramy niew został zmieniony. Błąd: "+errorType + ' (' + errorMessage + ')'));
           }
         });
       });
diff --git a/frontend/src/components/unification/shared/LexicalUnitDisplay.vue b/frontend/src/components/unification/shared/LexicalUnitDisplay.vue
index 190fbd2..4c97b51 100644
--- a/frontend/src/components/unification/shared/LexicalUnitDisplay.vue
+++ b/frontend/src/components/unification/shared/LexicalUnitDisplay.vue
@@ -54,25 +54,44 @@ export default {
                 if (this.hasWhiteSpace(title) || title.length === 0) {
                   alert(gettext("Nazwa zunifikowanej ramy nie może być pusta oraz nie może zawierać białych znaków."));
                 } else {
-                  const data = {'lu_id': this.lexicalUnitId, 'unified_frame_title': title,
-                      'frame_id': this.frame.id};
-                  $("#overlay").fadeIn(300);
-                  $.ajax({
-                    type     : 'post',
-                    url      : `/${lang}/unifier/frame_assign/`,
-                    dataType : 'json',
-                    data: data,
-                    timeout  : 60000,
-                    success: function (response) {
-                      $("#overlay").fadeOut(300);
-                      show_info('Rama została stworzona oraz przypisana użytkownikowi.');
-                      this.goToEdit(response.unified_frame_id, this.entryId, this.lexicalUnitId);
-                    }.bind(this),
-                    error: function (request, errorType, errorMessage) {
-                        $("#overlay").fadeOut(300);
-                      show_error(errorType + ' (' + errorMessage + ')');
-                    }
-                  });
+                    const dataCheckUniq = {'unified_frame_title': title};
+                    $.ajax({
+                        type: 'post',
+                        url: '/' + lang + '/unifier/check_unified_frame_title_uniq/',
+                        dataType: 'json',
+                        data: dataCheckUniq,
+                        timeout: 60000,
+                        success: function (response) {
+                            if(response.exists === false) {
+                                const data = {'lu_id': this.lexicalUnitId, 'unified_frame_title': title,
+                                    'frame_id': this.frame.id};
+                                $("#overlay").fadeIn(300);
+                                $.ajax({
+                                    type     : 'post',
+                                    url      : `/${lang}/unifier/frame_assign/`,
+                                    dataType : 'json',
+                                    data: data,
+                                    timeout  : 60000,
+                                    success: function (response) {
+                                        $("#overlay").fadeOut(300);
+                                        show_info('Rama została stworzona oraz przypisana użytkownikowi.');
+                                        this.goToEdit(response.unified_frame_id, this.entryId, this.lexicalUnitId);
+                                    }.bind(this),
+                                    error: function (request, errorType, errorMessage) {
+                                        $("#overlay").fadeOut(300);
+                                        show_error(errorType + ' (' + errorMessage + ')');
+                                    }
+                                });
+                            } else {
+                                alert(gettext("Nazwa zunifikowanej ramy nie jest unikalna. Proszę wybrać inną nazwę."));
+                            }
+                        }.bind(this),
+                        error: function (request, errorType, errorMessage) {
+                            alert(gettext("Wystapił błąd: " + errorType + ' (' + errorMessage + ')'));
+                            show_error(errorType + ' (' + errorMessage + ')');
+                            $.prompt.close();
+                        }
+                    });
                 }
               }
             }.bind(this)
diff --git a/unifier/urls.py b/unifier/urls.py
index 9ba2f65..d0c7885 100644
--- a/unifier/urls.py
+++ b/unifier/urls.py
@@ -31,4 +31,5 @@ urlpatterns = [
     path('get_hierarchy_hyponyms/<int:unified_frame_id>/', views.get_hierarchy_hyponyms, name='get_hierarchy_hyponyms'),
     path('get_hierarchy_hyperonyms/<int:unified_frame_id>/', views.get_hierarchy_hyperonyms, name='get_hierarchy_hyperonyms'),
 
+    path('check_unified_frame_title_uniq/', views.check_unified_frame_title_uniq, name='check_unified_frame_title_uniq'),
 ]
diff --git a/unifier/views.py b/unifier/views.py
index 1a4bb73..9cd87d5 100644
--- a/unifier/views.py
+++ b/unifier/views.py
@@ -133,8 +133,9 @@ def get_unified_frames(request):
 
         res_processed = []
         for key, value in res.items():
-            if (exclude_status is None or value['status'] != exclude_status) and \
-                    (restrict_to_user is None or value['assignee_username'] == restrict_to_user):
+            if ((exclude_status is None or value['status'] != exclude_status) and
+                    (restrict_to_user is None or value['assignee_username'] == restrict_to_user)) or \
+                    value['status'] == choices.UnifiedFrameStatus.VERIFIED:
                 res_processed.append(value)
 
         ret = {
@@ -426,6 +427,17 @@ def save_unified_frame_title(request):
     return JsonResponse({})
 
 
+@ajax_required
+@transaction.atomic
+def check_unified_frame_title_uniq(request):
+    if request.method == 'POST':
+        unified_frame_title = request.POST['unified_frame_title']
+        unified_frames = UnifiedFrame.objects.filter(title=unified_frame_title)
+
+        return JsonResponse({"exists": len(unified_frames) > 0})
+    return JsonResponse({})
+
+
 @ajax_required
 @transaction.atomic
 def save_selected_role(request):
@@ -595,19 +607,19 @@ def build_unified_frame_xml(request):
     return HttpResponse()
 
 
-@ajax_required
-@transaction.atomic
-def save_unified_frame_title(request):
-    if request.method == 'POST':
-        unified_frame_id = request.POST['unified_frame_id']
-        unified_frame_title = request.POST['unified_frame_title']
-
-        unified_frame = UnifiedFrame.objects.get(id=unified_frame_id)
-
-        if unified_frame:
-            unified_frame.title = unified_frame_title
-            unified_frame.save()
-    return JsonResponse({})
+# @ajax_required
+# @transaction.atomic
+# def save_unified_frame_title(request):
+#     if request.method == 'POST':
+#         unified_frame_id = request.POST['unified_frame_id']
+#         unified_frame_title = request.POST['unified_frame_title']
+#
+#         unified_frame = UnifiedFrame.objects.get(id=unified_frame_id)
+#
+#         if unified_frame:
+#             unified_frame.title = unified_frame_title
+#             unified_frame.save()
+#     return JsonResponse({})
 
 
 @ajax_required
-- 
GitLab