From d8420c9f11a7832454687d113ce124ed6ae45b98 Mon Sep 17 00:00:00 2001 From: George Katsikas <giorgakis.katsikas@gmail.com> Date: Fri, 15 Mar 2024 18:29:09 +0100 Subject: [PATCH] add notes list view --- .../pins/templates/pins/_hx_note_item.html | 1 + .../pins/templates/pins/_hx_notes_list.html | 25 +++++++++++++++++++ scipost_django/pins/urls.py | 20 ++++++++++++--- scipost_django/pins/views.py | 19 ++++++++++++++ 4 files changed, 61 insertions(+), 4 deletions(-) create mode 100644 scipost_django/pins/templates/pins/_hx_note_item.html create mode 100644 scipost_django/pins/templates/pins/_hx_notes_list.html diff --git a/scipost_django/pins/templates/pins/_hx_note_item.html b/scipost_django/pins/templates/pins/_hx_note_item.html new file mode 100644 index 000000000..fb3d7b84f --- /dev/null +++ b/scipost_django/pins/templates/pins/_hx_note_item.html @@ -0,0 +1 @@ +{{ note.title }} diff --git a/scipost_django/pins/templates/pins/_hx_notes_list.html b/scipost_django/pins/templates/pins/_hx_notes_list.html new file mode 100644 index 000000000..81e183ba3 --- /dev/null +++ b/scipost_django/pins/templates/pins/_hx_notes_list.html @@ -0,0 +1,25 @@ +{% load scipost_extras %} + +<div class="row" + hx-get="{% url "pins:_hx_notes_list" object|content_type_id object.id %}" + hx-trigger="notes-updated from:body target:form delay:2s, {% if notes is None %}load{% endif %}" + hx-swap="outerHTML"> + <div class="col-12"> + <h3>Notes</h3> + + <div> + <button class="btn btn-light btn-sm" + hx-get="{% url "pins:_hx_note_create_form" object|content_type_id object.id %}" + hx-target="closest div">Add note</button> + </div> + + <ul> + + {% for note in notes %} + <li>{% include "pins/_hx_note_item.html" %}</li> + {% endfor %} + + </ul> + + </div> +</div> diff --git a/scipost_django/pins/urls.py b/scipost_django/pins/urls.py index f822ff429..3e61307f0 100644 --- a/scipost_django/pins/urls.py +++ b/scipost_django/pins/urls.py @@ -3,14 +3,26 @@ __license__ = "AGPL v3" app_name = "pins" -from django.urls import path +from django.urls import include, path from . import views urlpatterns = [ path( - "_hx_create/<int:regarding_content_type>/<int:regarding_object_id>", - views._hx_note_create_form, - name="hx_note_create_form", + "<int:regarding_content_type>/<int:regarding_object_id>", + include( + [ + path( + "_hx_create", + views._hx_note_create_form, + name="_hx_note_create_form", + ), + path( + "_hx_list", + views._hx_notes_list, + name="_hx_notes_list", + ), + ] + ), ), ] diff --git a/scipost_django/pins/views.py b/scipost_django/pins/views.py index 877c7c77f..483d3540f 100644 --- a/scipost_django/pins/views.py +++ b/scipost_django/pins/views.py @@ -6,6 +6,7 @@ from django.template.response import TemplateResponse from scipost.permissions import HTMXResponse +from .models import Note from .forms import NoteForm @@ -28,3 +29,21 @@ def _hx_note_create_form(request, regarding_content_type, regarding_object_id): context = {"form": form} return TemplateResponse(request, "pins/_hx_note_create_form.html", context) + + +def _hx_notes_list(request, regarding_content_type, regarding_object_id): + regarding_content_type = ContentType.objects.get_for_id(regarding_content_type) + object = regarding_content_type.get_object_for_this_type(id=regarding_object_id) + notes = Note.objects.filter( + regarding_content_type=regarding_content_type, + regarding_object_id=regarding_object_id, + ) + + # TODO: Filter to the notes that the user has permission to see + # ... + + context = { + "object": object, + "notes": notes, + } + return TemplateResponse(request, "pins/_hx_notes_list.html", context) -- GitLab