def comment(request, template="generic/comments.html"): """ Handle a ``ThreadedCommentForm`` submission and redirect back to its related object. """ response = initial_validation(request, "comment") if isinstance(response, HttpResponse): return response obj, post_data = response resource_mode = post_data.get('resource-mode', 'view') request.session['resource-mode'] = resource_mode form = ThreadedCommentForm(request, obj, post_data) if form.is_valid(): url = obj.get_absolute_url() if is_spam(request, form, url): return redirect(url) comment = form.save(request) response = redirect(add_cache_bypass(comment.get_absolute_url())) # Store commenter's details in a cookie for 90 days. # for field in ThreadedCommentForm.cookie_fields: # cookie_name = ThreadedCommentForm.cookie_prefix + field # cookie_value = post_data.get(field, "") # set_cookie(response, cookie_name, cookie_value) return response elif request.is_ajax() and form.errors: return HttpResponse(dumps({"errors": form.errors})) # Show errors with stand-alone comment form. context = {"obj": obj, "posted_comment_form": form} response = render(request, template, context) return response
def comment(request, template="generic/comments.html"): """ Handle a ``ThreadedCommentForm`` submission and redirect back to its related object. """ response = initial_validation(request, "comment") if isinstance(response, HttpResponse): return response obj, post_data = response form = ThreadedCommentForm(request, obj, post_data) if form.is_valid(): url = obj.get_absolute_url() if is_spam(request, form, url): return redirect(url) comment = form.save(request) response = redirect(add_cache_bypass(comment.get_absolute_url())) # Store commenter's details in a cookie for 90 days. # for field in ThreadedCommentForm.cookie_fields: # cookie_name = ThreadedCommentForm.cookie_prefix + field # cookie_value = post_data.get(field, "") # set_cookie(response, cookie_name, cookie_value) return response elif request.is_ajax() and form.errors: return HttpResponse(dumps({"errors": form.errors})) # Show errors with stand-alone comment form. context = {"obj": obj, "posted_comment_form": form} response = render(request, template, context) return response
def comments_for(context, obj): """ Provides a generic context variable name for the object that comments are being rendered for. """ form = ThreadedCommentForm(context["request"], obj) try: context["posted_comment_form"] except KeyError: context["posted_comment_form"] = form context["unposted_comment_form"] = form context["comment_url"] = reverse("comment") context["object_for_comments"] = obj return context