SciPost Code Repository

Skip to content
Snippets Groups Projects
Commit d53d7e88 authored by George Katsikas's avatar George Katsikas :goat:
Browse files

add delete button for notes

parent d5ca1c94
No related branches found
No related tags found
No related merge requests found
...@@ -5,6 +5,10 @@ ...@@ -5,6 +5,10 @@
<span class="badge text-bg-secondary">{{ note.get_visibility_display }}</span> <span class="badge text-bg-secondary">{{ note.get_visibility_display }}</span>
<span class="fw-bold">{{ note.author.profile.full_name }}</span> <span class="fw-bold">{{ note.author.profile.full_name }}</span>
<span>{{ note.created }}</span> <span>{{ note.created }}</span>
<span role="button"
class="text-danger"
hx-target="closest li"
hx-delete="{% url "pins:_hx_note_delete" note.id %}">{% include "bi/trash-fill.html" %}</span>
</span> </span>
</div> </div>
<div>{{ note.description }}</div> <div>{{ note.description }}</div>
......
...@@ -3,7 +3,7 @@ ...@@ -3,7 +3,7 @@
<!-- Loader / Update Wrapper --> <!-- Loader / Update Wrapper -->
<div class="row" <div class="row"
hx-get="{% url "pins:_hx_notes_list" object|content_type_id object.id %}" hx-get="{% url "pins:_hx_notes_list" object|content_type_id object.id %}"
hx-trigger="notes-updated from:body target:form delay:1s, {% if notes is None %}load{% endif %}" hx-trigger="notes-updated from:closest div delay:1s{% if notes is None %}, load{% endif %}"
hx-swap="outerHTML"> hx-swap="outerHTML">
<!-- Loader / Update Wrapper --> <!-- Loader / Update Wrapper -->
......
...@@ -9,20 +9,32 @@ from . import views ...@@ -9,20 +9,32 @@ from . import views
urlpatterns = [ urlpatterns = [
path( path(
"<int:regarding_content_type>/<int:regarding_object_id>", "notes/",
include( include(
[ [
path( path(
"_hx_create", "<int:pk>/_hx_delete",
views._hx_note_create_form, views._hx_note_delete,
name="_hx_note_create_form", name="_hx_note_delete",
), ),
path( path(
"_hx_list", "<int:regarding_content_type>/<int:regarding_object_id>",
views._hx_notes_list, include(
name="_hx_notes_list", [
path(
"_hx_create",
views._hx_note_create_form,
name="_hx_note_create_form",
),
path(
"_hx_list",
views._hx_notes_list,
name="_hx_notes_list",
),
]
),
), ),
] ]
), ),
), )
] ]
...@@ -3,6 +3,7 @@ __license__ = "AGPL v3" ...@@ -3,6 +3,7 @@ __license__ = "AGPL v3"
from django.contrib.contenttypes.models import ContentType from django.contrib.contenttypes.models import ContentType
from django.db.models import Q from django.db.models import Q
from django.shortcuts import HttpResponse
from django.template.response import TemplateResponse from django.template.response import TemplateResponse
from scipost.permissions import HTMXResponse from scipost.permissions import HTMXResponse
...@@ -32,6 +33,31 @@ def _hx_note_create_form(request, regarding_content_type, regarding_object_id): ...@@ -32,6 +33,31 @@ def _hx_note_create_form(request, regarding_content_type, regarding_object_id):
return TemplateResponse(request, "pins/_hx_note_create_form.html", context) return TemplateResponse(request, "pins/_hx_note_create_form.html", context)
def _hx_note_delete(request, pk):
if request.method != "DELETE":
return HTMXResponse("Invalid request method", tag="danger")
note = Note.objects.filter(pk=pk).first()
if note is None:
return HTMXResponse("Note not found", tag="danger")
if note.visibility == Note.VISIBILITY_PRIVATE:
if note.author == request.user.contributor:
note.delete()
return HttpResponse()
else:
response = HTMXResponse(
"You are not the author of this note.", tag="danger"
)
else:
response = HTMXResponse(
"Deletion of non-private notes is disabled.", tag="danger"
)
response["HX-Trigger"] = "notes-updated"
return response
def _hx_notes_list(request, regarding_content_type, regarding_object_id): def _hx_notes_list(request, regarding_content_type, regarding_object_id):
regarding_content_type = ContentType.objects.get_for_id(regarding_content_type) regarding_content_type = ContentType.objects.get_for_id(regarding_content_type)
object = regarding_content_type.get_object_for_this_type(id=regarding_object_id) object = regarding_content_type.get_object_for_this_type(id=regarding_object_id)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment