Make ue comments a fragment

This commit is contained in:
2026-08-26 21:56:39 +02:00
parent 13829c0945
commit c84cef8e55
6 changed files with 63 additions and 29 deletions
@@ -6,13 +6,8 @@
{% endif %}
<form
hx-post="{{ action }}"
{% if form.is_creation %}
hx-target="#comments"
hx-swap="afterbegin"
{% else %}
hx-target="closest .leave-comment"
hx-swap="outerHTML"
{% endif %}
hx-target="closest .leave-comment"
hx-swap="outerHTML"
hx-disabled-elt="find input[type='submit']"
>
{% csrf_token %}
@@ -0,0 +1,17 @@
<section
hx-get="{{ url("pedagogy:ue_comments", ue_id=object.id) }}"
hx-swap="outerHTML"
hx-target="this"
hx-trigger="NewComment from:body"
>
{% if comments %}
<h2>{% trans %}Comments{% endtrans %}</h2>
<br>
{% endif %}
{% for comment in comments %}
{% include "pedagogy/fragments/ue_comment.jinja" %}
{% endfor %}
</section>
@@ -40,11 +40,6 @@
<br>
{% endif %}
{% if comments %}
<h2>{% trans %}Comments{% endtrans %}</h2>
<br>
{% endif %}
{% if not object.has_user_already_commented(user) and user.has_perm("pedagogy.add_uecomment") %}
{{ add_comment_form }}
{% endif %}
+1 -5
View File
@@ -61,11 +61,7 @@
{% include "pedagogy/fragments/ue_detail.jinja" %}
{% for comment in comments %}
<section id="comments">
{% include "pedagogy/fragments/ue_comment.jinja" %}
</section>
{% endfor %}
{{ comments }}
</div>
</div>
+6
View File
@@ -31,6 +31,7 @@ from pedagogy.views import (
UECommentUpdateView,
UECreateView,
UEDeleteView,
UEDetailCommentsView,
UEDetailView,
UEGuideView,
UEModerationFormView,
@@ -41,6 +42,11 @@ urlpatterns = [
# Urls displaying the actual application for visitors
path("", UEGuideView.as_view(), name="guide"),
path("ue/<int:ue_id>/", UEDetailView.as_view(), name="ue_detail"),
path(
"ue/<int:ue_id>/comments",
UEDetailCommentsView.as_view(),
name="ue_comments",
),
path(
"ue/<int:ue_id>/comment",
UECommentCreateView.as_view(),
+37 -12
View File
@@ -79,8 +79,36 @@ class UECommentCreateView(PermissionRequiredMixin, FragmentMixin, CreateView):
"action": reverse("pedagogy:comment_create", kwargs={"ue_id": self.ue.id})
}
def get_success_url(self):
return reverse("pedagogy:comment_detail", kwargs={"comment_id": self.object.id})
def form_valid(self, form):
"""If the form is valid, save the associated model."""
self.object = form.save()
response = HttpResponse(status=200)
response.headers["HX-Trigger"] = "NewComment, CommentUpdate"
return response
class UEDetailCommentsView(
PermissionRequiredMixin,
FragmentMixin,
DetailView,
):
"""Fragment view that display all comments"""
model = UE
pk_url_kwarg = "ue_id"
template_name = "pedagogy/fragments/ue_comments.jinja"
permission_required = "pedagogy.view_ue"
def get_context_data(self, **kwargs):
self.object = self.get_object() # Needed if loaded with .as_fragment()
return super().get_context_data(**kwargs) | {
"comments": list(
self.object.comments.viewable_by(self.request.user)
.annotate_is_reported()
.select_related("author")
.order_by("-publish_date")
),
}
class UEDetailView(
@@ -94,19 +122,13 @@ class UEDetailView(
permission_required = "pedagogy.view_ue"
fragments = {
"add_comment_form": UECommentCreateView,
"comments": UEDetailCommentsView,
}
def get_fragment_data(self):
return {"add_comment_form": {"ue_id": self.object.id}}
def get_context_data(self, **kwargs):
return super().get_context_data(**kwargs) | {
"comments": list(
self.object.comments.viewable_by(self.request.user)
.annotate_is_reported()
.select_related("author")
.order_by("-publish_date")
),
return {
"add_comment_form": {"ue_id": self.object.id},
"comments": {"ue_id": self.object.id},
}
@@ -117,6 +139,9 @@ class UECommentDetailView(PermissionRequiredMixin, DetailView):
permission_required = "pedagogy.view_ue"
context_object_name = "comment"
def get_queryset(self):
return super().get_queryset().annotate_is_reported()
def dispatch(self, *args, **kwargs):
res: HttpResponse = super().dispatch(*args, **kwargs)
res.headers["HX-Trigger"] = "CommentUpdate"