From c3d6b8ea41a179c394136cffa0c400f13f765c0b Mon Sep 17 00:00:00 2001 From: Tuan-Dung Bui Date: Tue, 16 May 2023 16:02:49 +0700 Subject: [PATCH] add remove feature comments --- judge/comments.py | 11 +++++++++-- judge/views/comment.py | 8 +++++++- templates/comments/content-list.html | 6 ++++-- templates/comments/list.html | 6 +++--- templates/comments/media-js.html | 11 ++++++++--- 5 files changed, 31 insertions(+), 11 deletions(-) diff --git a/judge/comments.py b/judge/comments.py index 405b2ce..2a2f515 100755 --- a/judge/comments.py +++ b/judge/comments.py @@ -11,6 +11,7 @@ from django.http import ( HttpResponseForbidden, HttpResponseNotFound, HttpResponseRedirect, + Http404, ) from django.urls import reverse_lazy from django.utils.decorators import method_decorator @@ -154,7 +155,10 @@ class CommentedDetailView(TemplateResponseMixin, SingleObjectMixin, View): pre_query = None if "comment-id" in request.GET: comment_id = int(request.GET["comment-id"]) - comment_obj = Comment.objects.get(pk=comment_id) + try: + comment_obj = Comment.objects.get(pk=comment_id) + except Comment.DoesNotExist: + raise Http404 pre_query = comment_obj while comment_obj is not None: pre_query = comment_obj @@ -174,7 +178,9 @@ class CommentedDetailView(TemplateResponseMixin, SingleObjectMixin, View): queryset = queryset.filter(parent=None, hidden=False) queryset_all = None comment_count = len(queryset) + context["comment_remove"] = -1 if (pre_query != None): + comment_remove = pre_query.id queryset_all = pre_query.get_descendants(include_self=True) queryset_all = ( queryset_all.select_related("author__user") @@ -182,6 +188,7 @@ class CommentedDetailView(TemplateResponseMixin, SingleObjectMixin, View): .defer("author__about") .annotate(revisions=Count("versions", distinct=True)) ) + context["comment_remove"] = comment_remove else: queryset = ( queryset.select_related("author__user") @@ -227,7 +234,7 @@ class CommentedDetailView(TemplateResponseMixin, SingleObjectMixin, View): context["comment_root_id"] = 0 context["comment_parrent_none"] = 1 if (pre_query != None): - context["offset"] = 0 + context["offset"] = 1 else: context["offset"] = 10 diff --git a/judge/views/comment.py b/judge/views/comment.py index 976abd2..abacb52 100755 --- a/judge/views/comment.py +++ b/judge/views/comment.py @@ -123,8 +123,11 @@ def get_comments(request, limit=10): if comment_id and not Comment.objects.filter(id=comment_id).exists(): raise Http404() offset = 0 - if "offset" in request.GET: + if "offset" in request.GET: offset = int(request.GET["offset"]) + comment_remove = -1 + if "comment_remove" in request.GET: + comment_remove = int(request.GET["comment_remove"]) comment_root_id = 0 if (comment_id): comment_obj = Comment.objects.get(pk=comment_id) @@ -134,6 +137,8 @@ def get_comments(request, limit=10): queryset = comment_obj.linked_object.comments if parrent_none: queryset = queryset.filter(parent=None, hidden=False) + if (comment_remove != -1): + queryset.get(pk=comment_remove).delete() else: queryset = queryset.filter(parent=comment_obj, hidden=False) comment_count = len(queryset) @@ -165,6 +170,7 @@ def get_comments(request, limit=10): "limit": limit, "comment_count": comment_count, "comment_parrent_none": parrent_none, + "comment_remove": comment_remove, } ) diff --git a/templates/comments/content-list.html b/templates/comments/content-list.html index 711e632..2ab123e 100755 --- a/templates/comments/content-list.html +++ b/templates/comments/content-list.html @@ -1,3 +1,5 @@ +{% set logged_in = request.user.is_authenticated %} +{% set profile = request.profile if logged_in else None %} {% for node in mptt_tree(comment_list) recursive %}
  • @@ -120,14 +122,14 @@ {% set comment_more = comment_count - offset %} {% if comment_more == 1 %} - + {{ comment_count - offset }} comment more {% elif comment_more > 1 %} - + diff --git a/templates/comments/list.html b/templates/comments/list.html index c0c5832..9a0f1cc 100755 --- a/templates/comments/list.html +++ b/templates/comments/list.html @@ -152,7 +152,7 @@ {% endfor %} {% set comment_more = comment_count - offset %} {% if comment_more == 1 %} - + @@ -160,7 +160,7 @@ {{ comment_count - offset }} comment more {% elif comment_more > 1 %} - + @@ -171,7 +171,7 @@ {% else %} {% include "comments/content-list.html" %} {% endif %} - + {% elif not comment_lock %}

    {{ _('There are no comments at the moment.') }}

    diff --git a/templates/comments/media-js.html b/templates/comments/media-js.html index bf70f95..131d040 100755 --- a/templates/comments/media-js.html +++ b/templates/comments/media-js.html @@ -119,6 +119,10 @@ } }); } + const queryString = window.location.search; + const urlParams = new URLSearchParams(queryString); + const comment_remove = urlParams.get('comment-id'); + console.log(comment_remove); window.comment_get_replies = function (id, parrent_none) { var $comment_show_btn = $("#comment-" + id + " .show_more_reply"); @@ -145,7 +149,7 @@ }) } - window.comment_show_more = function (id, parrent_none, offset) { + window.comment_show_more = function (id, parrent_none, offset, comment_remove) { console.log(parrent_none) if (parrent_none == 1) { var $comment_show_btn = $("#comment-0" + " .show_more_comment"); @@ -158,10 +162,10 @@ var $comment = $("#comment-" + id + "-children"); $comment.append("

    Loading...

    "); } - ajax_comment_show_more('{{ url('comment_show_more') }}', id, parrent_none, offset); + ajax_comment_show_more('{{ url('comment_show_more') }}', id, parrent_none, offset, comment_remove); } - function ajax_comment_show_more(url, id, parrent_none, offset) { + function ajax_comment_show_more(url, id, parrent_none, offset, comment_remove) { return $.ajax({ url: url, type: 'GET', @@ -169,6 +173,7 @@ id: id, parrent_none: parrent_none, offset: offset, + comment_remove: comment_remove, }, success: function(data) { if (parrent_none == 1) {