|
|
|
|
@ -6,12 +6,13 @@ from django.utils.decorators import method_decorator
|
|
|
|
|
from django.views.decorators.csrf import csrf_protect
|
|
|
|
|
from django.views.generic.edit import FormView
|
|
|
|
|
|
|
|
|
|
from accounts.models import BlogUser
|
|
|
|
|
from blog.models import Article
|
|
|
|
|
from ..accounts.models import BlogUser
|
|
|
|
|
from ..blog.models import Article
|
|
|
|
|
from .forms import CommentForm
|
|
|
|
|
from .models import Comment
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#ssj
|
|
|
|
|
# 定义一个基于类的视图,用于处理用户提交评论的请求
|
|
|
|
|
class CommentPostView(FormView):
|
|
|
|
|
# 指定该视图使用的表单类
|
|
|
|
|
@ -20,10 +21,12 @@ class CommentPostView(FormView):
|
|
|
|
|
# 指定表单验证失败时,或需要渲染响应时使用的模板
|
|
|
|
|
template_name = 'blog/article_detail.html'
|
|
|
|
|
|
|
|
|
|
#ssj
|
|
|
|
|
@method_decorator(csrf_protect)
|
|
|
|
|
def dispatch(self, *args, **kwargs):
|
|
|
|
|
return super(CommentPostView, self).dispatch(*args, **kwargs)
|
|
|
|
|
|
|
|
|
|
#ssj
|
|
|
|
|
def get(self, request, *args, **kwargs):
|
|
|
|
|
# 从 URL 参数中获取文章 ID
|
|
|
|
|
article_id = self.kwargs['article_id']
|
|
|
|
|
@ -37,6 +40,7 @@ class CommentPostView(FormView):
|
|
|
|
|
# 重定向到文章页面,并锚定到 id="comments" 的元素(通常是评论列表)
|
|
|
|
|
return HttpResponseRedirect(url + "#comments")
|
|
|
|
|
|
|
|
|
|
#ssj
|
|
|
|
|
def form_invalid(self, form):
|
|
|
|
|
# 获取文章 ID
|
|
|
|
|
article_id = self.kwargs['article_id']
|
|
|
|
|
@ -50,6 +54,7 @@ class CommentPostView(FormView):
|
|
|
|
|
'article': article
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
#ssj
|
|
|
|
|
def form_valid(self, form):
|
|
|
|
|
"""提交的数据验证合法后的逻辑"""
|
|
|
|
|
|
|
|
|
|
@ -65,6 +70,7 @@ class CommentPostView(FormView):
|
|
|
|
|
# 获取对应的文章对象(404 安全获取)
|
|
|
|
|
article = get_object_or_404(Article, pk=article_id)
|
|
|
|
|
|
|
|
|
|
#ssj
|
|
|
|
|
# 检查文章是否允许评论
|
|
|
|
|
# 如果文章的评论状态为 'c'(关闭)或文章状态为 'c'(草稿等),则禁止评论
|
|
|
|
|
if article.comment_status == 'c' or article.status == 'c':
|
|
|
|
|
@ -78,7 +84,7 @@ class CommentPostView(FormView):
|
|
|
|
|
comment.article = article
|
|
|
|
|
|
|
|
|
|
# 导入博客设置工具函数
|
|
|
|
|
from djangoblog.utils import get_blog_setting
|
|
|
|
|
from ..djangoblog.utils import get_blog_setting
|
|
|
|
|
|
|
|
|
|
# 获取当前博客的全局设置
|
|
|
|
|
settings = get_blog_setting()
|
|
|
|
|
@ -90,6 +96,7 @@ class CommentPostView(FormView):
|
|
|
|
|
# 设置评论作者
|
|
|
|
|
comment.author = author
|
|
|
|
|
|
|
|
|
|
#ssj
|
|
|
|
|
# 检查表单中是否包含父评论 ID(即是否为回复)
|
|
|
|
|
if form.cleaned_data['parent_comment_id']:
|
|
|
|
|
# 根据父评论 ID 获取父评论对象
|
|
|
|
|
@ -101,6 +108,7 @@ class CommentPostView(FormView):
|
|
|
|
|
# 将评论对象保存到数据库(此时所有字段已设置完毕)
|
|
|
|
|
comment.save(True)
|
|
|
|
|
|
|
|
|
|
#ssj
|
|
|
|
|
# 重定向到文章详情页,并定位到刚刚发表的评论
|
|
|
|
|
# 使用锚点 #div-comment-{id} 定位到具体评论
|
|
|
|
|
return HttpResponseRedirect(
|
|
|
|
|
|