diff --git a/forum/templates/forum/comment_check_delete.html b/forum/templates/forum/comment_check_delete.html new file mode 100644 index 0000000..42bc55f --- /dev/null +++ b/forum/templates/forum/comment_check_delete.html @@ -0,0 +1,37 @@ +{% extends 'base.html' %} + +{% block title %}确认删除评论{% endblock %} + +{% block content %} +
+
+
+
+
+
删除评论
+

该评论将会永久消失!(真的很久!)

+

请计算 {{ a }} + {{ b }} = ?

+
+ {% csrf_token %} + + +
+
+ + 取消 +
+
+
+
+
+
+
+ + +{% endblock %} diff --git a/forum/templates/forum/post_detail.html b/forum/templates/forum/post_detail.html index 71fa666..b56e98f 100644 --- a/forum/templates/forum/post_detail.html +++ b/forum/templates/forum/post_detail.html @@ -43,7 +43,14 @@

#{{ forloop.counter }} {{ comment.author.username }} - {{ comment.created_at|date:"Y-m-d H:i" }} + + {{ comment.created_at|date:"Y-m-d H:i" }} + {% if comment.author == request.user %} + + + + {% endif %} +
{{ comment.content_html | safe }} diff --git a/forum/urls.py b/forum/urls.py index b87f495..0a64d34 100644 --- a/forum/urls.py +++ b/forum/urls.py @@ -8,6 +8,7 @@ path('posts/create/', views.post_create, name='post_create'), path('posts//', views.PostDetailView.as_view(), name='post_detail'), path('posts//delete/', views.PostDeleteView.as_view(), name='post_delete'), + path('comments//delete/', views.comment_delete_view, name='comment_delete'), path('login/', views.LoginView.as_view(), name='login'), path('settings/', views.user_settings_view, name='settings'), path('settings/user_delete/', views.user_delete_view, name='user_delete'), diff --git a/forum/views.py b/forum/views.py index 1a06458..6dc331f 100644 --- a/forum/views.py +++ b/forum/views.py @@ -1,4 +1,4 @@ -import re +import re, random from django.urls import reverse_lazy from django.utils import timezone from django.contrib.auth.models import User @@ -11,7 +11,7 @@ from .utils import send_group_notification from forum.form import MDEditorCommentForm, MDEditorModelForm -from forum.models import Item, Post, Rating +from forum.models import Comment, Item, Post, Rating from forum.bots_manager import manager # Create your views here. @@ -153,6 +153,21 @@ def get_queryset(self): qs = super().get_queryset() return qs.filter(author=self.request.user) +@login_required +def comment_delete_view(request, comment_id): + comment = get_object_or_404(Comment, id=comment_id, author=request.user) + if request.method == 'POST': + answer = request.POST.get('answer', '') + expected = request.POST.get('expected', '') + if answer == expected: + post_id = comment.post.id + comment.delete() + return redirect('post_detail', post_id=post_id) + a, b = random.randint(1, 9), random.randint(1, 9) + return render(request, 'forum/comment_check_delete.html', { + 'comment': comment, 'a': a, 'b': b, 'answer': a + b, + }) + @login_required def user_settings_view(request): webpush = {"group": "webpush_new_posts" }