fix(spec): enforce read-time budget and re-render invalid comment submissions
This commit is contained in:
@@ -5,7 +5,7 @@ from django.contrib import messages
|
||||
from django.core.cache import cache
|
||||
from django.core.exceptions import ValidationError
|
||||
from django.http import HttpResponse
|
||||
from django.shortcuts import get_object_or_404, redirect
|
||||
from django.shortcuts import get_object_or_404, redirect, render
|
||||
from django.views import View
|
||||
|
||||
from apps.blog.models import ArticlePage
|
||||
@@ -23,6 +23,12 @@ def client_ip_from_request(request) -> str:
|
||||
|
||||
|
||||
class CommentCreateView(View):
|
||||
def _render_article_with_errors(self, request, article, form):
|
||||
context = article.get_context(request)
|
||||
context["page"] = article
|
||||
context["comment_form"] = form
|
||||
return render(request, "blog/article_page.html", context, status=200)
|
||||
|
||||
def post(self, request):
|
||||
ip = client_ip_from_request(request)
|
||||
key = f"comment-rate:{ip}"
|
||||
@@ -48,11 +54,10 @@ class CommentCreateView(View):
|
||||
try:
|
||||
comment.full_clean()
|
||||
except ValidationError:
|
||||
messages.error(request, "Reply depth exceeds the allowed limit")
|
||||
return redirect(article.url)
|
||||
form.add_error(None, "Reply depth exceeds the allowed limit")
|
||||
return self._render_article_with_errors(request, article, form)
|
||||
comment.save()
|
||||
messages.success(request, "Your comment is awaiting moderation")
|
||||
return redirect(f"{article.url}?commented=1")
|
||||
|
||||
messages.error(request, "Please correct the form errors")
|
||||
return redirect(article.url)
|
||||
return self._render_article_with_errors(request, article, form)
|
||||
|
||||
Reference in New Issue
Block a user