add remove feature comments

This commit is contained in:
Tuan-Dung Bui 2023-05-16 16:02:49 +07:00
parent d1f9c12c2d
commit c3d6b8ea41
5 changed files with 31 additions and 11 deletions

View file

@ -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

View file

@ -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,
}
)

View file

@ -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 %}
<li id="comment-{{ node.id }}" data-revision="{{ node.revisions - 1 }}" data-max-revision="{{ node.revisions - 1 }}"
data-revision-ajax="{{ url('comment_revision_ajax', node.id) }}" class="comment">
@ -120,14 +122,14 @@
{% set comment_more = comment_count - offset %}
{% if comment_more == 1 %}
<a href="javascript:comment_show_more({{ comment_root_id }}, {{ comment_parrent_none }}, {{ offset }} )" class="show_more_comment">
<a href="javascript:comment_show_more({{ comment_root_id }}, {{ comment_parrent_none }}, {{ offset }}, {{ comment_remove }})" class="show_more_comment">
<svg xmlns="http://www.w3.org/2000/svg" class="icon icon-tabler icon-tabler-chevron-down" width="24" height="24" viewBox="0 0 24 24" stroke-width="2" stroke="currentColor" fill="none" stroke-linecap="round" stroke-linejoin="round">
<path stroke="none" d="M0 0h24v24H0z" fill="none"></path>
<path d="M6 9l6 6l6 -6"></path>
</svg>
{{ comment_count - offset }} comment more</a>
{% elif comment_more > 1 %}
<a href="javascript:comment_show_more({{ comment_root_id }}, {{ comment_parrent_none }}, {{ offset }} )" class="show_more_comment">
<a href="javascript:comment_show_more({{ comment_root_id }}, {{ comment_parrent_none }}, {{ offset }}, {{ comment_remove }})" class="show_more_comment">
<svg xmlns="http://www.w3.org/2000/svg" class="icon icon-tabler icon-tabler-chevron-down" width="24" height="24" viewBox="0 0 24 24" stroke-width="2" stroke="currentColor" fill="none" stroke-linecap="round" stroke-linejoin="round">
<path stroke="none" d="M0 0h24v24H0z" fill="none"></path>
<path d="M6 9l6 6l6 -6"></path>

View file

@ -152,7 +152,7 @@
{% endfor %}
{% set comment_more = comment_count - offset %}
{% if comment_more == 1 %}
<a href="javascript:comment_show_more({{ comment_root_id }}, {{ comment_parrent_none }}, {{ offset }} )" class="show_more_comment">
<a href="javascript:comment_show_more({{ comment_root_id }}, {{ comment_parrent_none }}, {{ offset }}, {{ comment_remove }})" class="show_more_comment">
<svg xmlns="http://www.w3.org/2000/svg" class="icon icon-tabler icon-tabler-chevron-down" width="24" height="24" viewBox="0 0 24 24" stroke-width="2" stroke="currentColor" fill="none" stroke-linecap="round" stroke-linejoin="round">
<path stroke="none" d="M0 0h24v24H0z" fill="none"></path>
<path d="M6 9l6 6l6 -6"></path>
@ -160,7 +160,7 @@
{{ comment_count - offset }} comment more
</a>
{% elif comment_more > 1 %}
<a href="javascript:comment_show_more({{ comment_root_id }}, {{ comment_parrent_none }}, {{ offset }} )" class="show_more_comment">
<a href="javascript:comment_show_more({{ comment_root_id }}, {{ comment_parrent_none }}, {{ offset }}, {{ comment_remove }})" class="show_more_comment">
<svg xmlns="http://www.w3.org/2000/svg" class="icon icon-tabler icon-tabler-chevron-down" width="24" height="24" viewBox="0 0 24 24" stroke-width="2" stroke="currentColor" fill="none" stroke-linecap="round" stroke-linejoin="round">
<path stroke="none" d="M0 0h24v24H0z" fill="none"></path>
<path d="M6 9l6 6l6 -6"></path>

View file

@ -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("<p class='loading'> Loading... </p>");
}
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) {