From 8bd534793b20706a50d151ea83ebe150ae41aca5 Mon Sep 17 00:00:00 2001
From: dcz <dcz@ipipan.waw.pl>
Date: Tue, 22 Aug 2023 11:21:37 +0200
Subject: [PATCH] Notes visible for superleksykografs

---
 .../entries/js/unification_entries_list.js       |  4 +++-
 semantics/views.py                               |  1 +
 users/forms.py                                   |  2 +-
 users/models.py                                  | 16 +++++++++++-----
 users/templates/notes.html                       | 10 +++++++++-
 users/views.py                                   |  3 ++-
 6 files changed, 27 insertions(+), 9 deletions(-)

diff --git a/entries/static/entries/js/unification_entries_list.js b/entries/static/entries/js/unification_entries_list.js
index 492c88b..8ef45fe 100644
--- a/entries/static/entries/js/unification_entries_list.js
+++ b/entries/static/entries/js/unification_entries_list.js
@@ -96,7 +96,8 @@ function show_notes_form($container, pk, model, refreshFunction, type, title, bo
             dataType: 'json',
             data: {
                 title: $('.note-form input[name=title]').val(),
-                note: $('.note-form textarea[name=note]').val()
+                note: $('.note-form textarea[name=note]').val(),
+                super: $('.note-form input[name=super]').val()
             },
             timeout: 5000,
             success: function (response) {
@@ -136,6 +137,7 @@ function setup_notes($container, $template, pk, model, refreshFunction, type, ti
                         <tr role="button" data-toggle="collapse" data-target="#note-${note.pk}" class="cursor-pointer">
                             <td>${note.title}</td>
                             <td>${note.owner_label}</td>
+                            <td>${note.super}</td>
                             <td>
                                 <a
                                     href="#"
diff --git a/semantics/views.py b/semantics/views.py
index 9b6f7ce..da49e6a 100644
--- a/semantics/views.py
+++ b/semantics/views.py
@@ -19,6 +19,7 @@ def frame_mark_as_invalid(request, frame_pk):
     frame.save()
     return {}
 
+
 @ajax(login_required=True, method='post')
 @transaction.atomic
 @permission_required('semantics.manage_invalid_lexical_units')
diff --git a/users/forms.py b/users/forms.py
index 26bc2e4..6400b8c 100644
--- a/users/forms.py
+++ b/users/forms.py
@@ -109,4 +109,4 @@ password_reset_set_password_form_helper.layout = Layout(
 class NoteForm(forms.ModelForm):
     class Meta:
         model = Note
-        fields = ["title", "note"]
+        fields = ["title", "note", "super"]
diff --git a/users/models.py b/users/models.py
index 2fe93d3..d289bd9 100644
--- a/users/models.py
+++ b/users/models.py
@@ -34,17 +34,22 @@ class Assignment(models.Model):
 class NoteQuerySet(models.QuerySet):
     def for_user(self, user):
         notes = (
-            self.filter(author=user).annotate(owner_label=models.Value(_('własne'), output_field=models.CharField()))
+            self.filter(author=user)
         )
-        if user.has_perm('user.view_all_notes'):
+        if user.has_perm('users.view_all_notes'):
             notes |= (
                 self.exclude(author=user).annotate(owner_label=models.F('author__username'))
             )
-        else:
+        elif user.groups.filter(name=settings.SUPER_LEXICOGRAPHS_GROUP_NAME).exists():
             notes |= (
-                self.exclude(author=user).filter(author__groups__name=settings.SUPER_LEXICOGRAPHS_GROUP_NAME)
-                .annotate(owner_label=models.Value(_('Super'), output_field=models.CharField()))
+                self.exclude(author=user)
+                    .filter(super=True)
             )
+            # notes |= (
+            #     self.exclude(author=user).filter(author__groups__name=settings.SUPER_LEXICOGRAPHS_GROUP_NAME)
+            #     .annotate(owner_label=models.Value(_('Super'), output_field=models.CharField()))
+            # )
+
         return notes
 
 
@@ -57,5 +62,6 @@ class Note(models.Model):
     note = models.TextField()
     created_at = models.DateTimeField(auto_now_add=True)
     type = models.CharField(max_length=100, default=None, blank=True, null=True)
+    super = models.BooleanField(default=False)
 
     objects = models.Manager.from_queryset(NoteQuerySet)()
diff --git a/users/templates/notes.html b/users/templates/notes.html
index 0b2ba24..be44cca 100644
--- a/users/templates/notes.html
+++ b/users/templates/notes.html
@@ -9,6 +9,7 @@
             </div>
             <div class="d-flex">
                 <div class="flex-grow-1 mr-3">
+                Superleksykograf <input id="super" name="super" type="checkbox" title="{% trans 'Notatka widoczna dla superleksykografa' %}" />
                 <input id="title" name="title" class="form-control mb-2" placeholder="{% trans 'Tytuł notatki' %}" />
                 <textarea id="note" name="note" class="form-control" placeholder="{% trans 'Treść notatki' %}"></textarea>
                 </div>
@@ -23,7 +24,14 @@
             <a href="#" class="show-note-form btn btn-xs btn-outline-dark ml-2">{% trans 'Dodaj' %}</a>
         </div>
         <table class="table table-sm table-striped border notes-table mb-0" data-url="{% url 'users:get_notes' model='MODEL' pk='PK' type='TYPE' %}">
-            <tbody></tbody>
+            <tbody>
+                <tr style="font-weight: bold">
+                    <td>Tytuł</td>
+                    <td>Właściciel</td>
+                    <td>Czy widoczna dla superleksykografa</td>
+                    <td></td>
+                </tr>
+            </tbody>
         </table>
     </div>
 </div>
diff --git a/users/views.py b/users/views.py
index 7be4819..374106b 100644
--- a/users/views.py
+++ b/users/views.py
@@ -68,11 +68,12 @@ def get_notes(request, model, pk, type):
     return JsonResponse({
         "notes": [{
             "pk": note.pk,
-            "owner_label": note.owner_label,
+            "owner_label": "własne" if note.author.pk is request.user.pk else note.author.username,
             "created_at": note.created_at,
             "title": note.title,
             "note": note.note,
             "type": note.type,
+            "super": "Tak" if note.super else "Nie"
         } for note in notes],
     })
 
-- 
GitLab