From f898e20b315f57117786b467314ef88d01015d44 Mon Sep 17 00:00:00 2001 From: dcz2 <dcz@ipipan.waw.pl> Date: Mon, 4 Jul 2022 21:47:24 +0200 Subject: [PATCH] Notes title and deleting --- .../js/components/UnificationRightPane.js | 1 - .../entries/js/unification_entries_list.js | 39 ++++++++++++++++++- users/forms.py | 2 +- users/models.py | 1 + users/templates/notes.html | 5 ++- users/urls.py | 1 + users/views.py | 9 +++++ 7 files changed, 53 insertions(+), 5 deletions(-) diff --git a/entries/static/entries/js/components/UnificationRightPane.js b/entries/static/entries/js/components/UnificationRightPane.js index 3e63504..6640ec3 100644 --- a/entries/static/entries/js/components/UnificationRightPane.js +++ b/entries/static/entries/js/components/UnificationRightPane.js @@ -29,7 +29,6 @@ export default { refresh () { this.key = null; setTimeout(() => { this.key = this.lexicalUnitId; }, 0); - update_entries(); // TODO }, swapFrames (previewedUnifiedFrameId) { this.previewedUnifiedFrameId = this.unifiedFrameId; diff --git a/entries/static/entries/js/unification_entries_list.js b/entries/static/entries/js/unification_entries_list.js index 710824d..5847bfa 100644 --- a/entries/static/entries/js/unification_entries_list.js +++ b/entries/static/entries/js/unification_entries_list.js @@ -88,7 +88,10 @@ function setup_notes($container, $template, pk, model, refreshFunction) { type : 'post', url : $('#note-form-template').data('url').replace('MODEL', model).replace('PK', pk), dataType : 'json', - data : { note: $('.note-form textarea[name=note]').val() }, + data : { + title: $('.note-form input[name=title]').val(), + note: $('.note-form textarea[name=note]').val() + }, timeout : 5000, success : function (response) { refreshFunction(); @@ -105,7 +108,39 @@ function setup_notes($container, $template, pk, model, refreshFunction) { url: $('.notes-table').data('url').replace('MODEL', model).replace('PK', pk), success: function (data) { data.notes.map(function (note) { - $('.notes-table tbody', $container).append(`<tr><td>${note.note}</td><td>${note.owner_label}</td></tr>`); + $('.notes-table tbody', $container).append( + ` + <tr role="button" data-toggle="collapse" data-target="#note-${note.pk}" class="cursor-pointer"> + <td>${note.title}</td> + <td>${note.owner_label}</td> + <td> + <a + href="#" + class="btn btn-xs btn-outline-dark float-right delete-note" + data-pk="${note.pk}" + >×</a> + </td> + </tr> + <tr id="note-${note.pk}" class="collapse v-hidden"><td colspan="3">${note.note}</td></tr> + ` + ); + $('.delete-note', $container).click(function (e) { + e.preventDefault(); + const pk = $(this).data("pk"); + if (!confirm(gettext("Czy na pewno chcesz usunąć notatkę?"))) return false; + $.ajax({ + type : 'delete', + url : `/${lang}/users/notes/${pk}/delete/`, + timeout : 5000, + success : function () { + refreshFunction(); + }, + error : function () { + alert(gettext('Nie udało się usunąć notatki.')); + } + }); + return false; + }); }); } }) diff --git a/users/forms.py b/users/forms.py index da41112..26bc2e4 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 = ["note"] + fields = ["title", "note"] diff --git a/users/models.py b/users/models.py index df17558..6d99a10 100644 --- a/users/models.py +++ b/users/models.py @@ -47,6 +47,7 @@ class Note(models.Model): subject_ct = models.ForeignKey(ContentType, on_delete=models.PROTECT) subject_id = models.PositiveIntegerField() subject = GenericForeignKey('subject_ct', 'subject_id') + title = models.CharField(max_length=100) note = models.TextField() created_at = models.DateTimeField(auto_now_add=True) diff --git a/users/templates/notes.html b/users/templates/notes.html index 75a7198..d9aa102 100644 --- a/users/templates/notes.html +++ b/users/templates/notes.html @@ -8,7 +8,10 @@ <a href="#" class="btn btn-xs btn-outline-dark float-right hide-note-form">×</a> </div> <div class="d-flex"> - <textarea name="note" class="form-control mr-3"></textarea> + <div class="flex-grow-1 mr-3"> + <input name="title" class="form-control mb-2" placeholder="{% trans 'Tytuł notatki' %}" /> + <textarea name="note" class="form-control" placeholder="{% trans 'Treść notatki' %}"></textarea> + </div> <a href="#" class="btn btn-sm btn-outline-dark ml-auto align-self-end add-note">{% trans 'Dodaj' %}</a> </div> </div> diff --git a/users/urls.py b/users/urls.py index 08c20de..911aa58 100644 --- a/users/urls.py +++ b/users/urls.py @@ -56,4 +56,5 @@ urlpatterns = [ ), re_path(r'^notes/(?P<model>\w+.\w+)/(?P<pk>\w+)/$', views.get_notes, name='get_notes'), re_path(r'^notes/(?P<model>\w+.\w+)/(?P<pk>\w+)/add/$', views.add_note, name='add_note'), + path('notes/<int:pk>/delete/', views.delete_note, name='delete_note'), ] diff --git a/users/views.py b/users/views.py index d12ab44..63b23cf 100644 --- a/users/views.py +++ b/users/views.py @@ -68,6 +68,7 @@ def get_notes(request, model, pk): "pk": note.pk, "owner_label": note.owner_label, "created_at": note.created_at, + "title": note.title, "note": note.note, } for note in notes], }) @@ -84,3 +85,11 @@ def add_note(request, model, pk): form.save() return JsonResponse({}) return JsonResponse(form.errors.get_json_data(), status=400) + + +@require_http_methods(["DELETE"]) +@login_required +def delete_note(request, pk): + note = get_object_or_404(Note.objects.for_user(request.user), pk=pk) + note.delete() + return JsonResponse({}) -- GitLab