| 1 |
from django import http |
| 2 |
from django.apps import apps |
| 3 |
from django.conf import settings |
| 4 |
from django.contrib.sites.shortcuts import get_current_site |
| 5 |
from django.core.exceptions import ObjectDoesNotExist, ValidationError |
| 6 |
from django.shortcuts import render |
| 7 |
from django.template.loader import render_to_string |
| 8 |
from django.utils.html import escape |
| 9 |
from django.views.decorators.csrf import csrf_protect |
| 10 |
from django.views.decorators.http import require_POST |
| 11 |
|
| 12 |
import django_comments |
| 13 |
from django_comments import signals |
| 14 |
from django_comments.views.utils import next_redirect, confirmation_view |
| 15 |
|
| 16 |
|
| 17 |
class CommentPostBadRequest(http.HttpResponseBadRequest): |
| 18 |
""" |
| 19 |
Response returned when a comment post is invalid. If ``DEBUG`` is on a |
| 20 |
nice-ish error message will be displayed (for debugging purposes), but in |
| 21 |
production mode a simple opaque 400 page will be displayed. |
| 22 |
""" |
| 23 |
|
| 24 |
def __init__(self, why): |
| 25 |
super().__init__() |
| 26 |
if settings.DEBUG: |
| 27 |
self.content = render_to_string("comments/400-debug.html", {"why": why}) |
| 28 |
|
| 29 |
|
| 30 |
@csrf_protect |
| 31 |
@require_POST |
| 32 |
def post_comment(request, next=None, using=None): |
| 33 |
""" |
| 34 |
Post a comment. |
| 35 |
|
| 36 |
HTTP POST is required. If ``POST['submit'] == "preview"`` or if there are |
| 37 |
errors a preview template, ``comments/preview.html``, will be rendered. |
| 38 |
""" |
| 39 |
|
| 40 |
data = request.POST.copy() |
| 41 |
if request.user.is_authenticated: |
| 42 |
if not data.get('name', ''): |
| 43 |
data["name"] = request.user.get_full_name() or request.user.get_username() |
| 44 |
if not data.get('email', ''): |
| 45 |
data["email"] = request.user.email |
| 46 |
|
| 47 |
|
| 48 |
ctype = data.get("content_type") |
| 49 |
object_pk = data.get("object_pk") |
| 50 |
if ctype is None or object_pk is None: |
| 51 |
return CommentPostBadRequest("Missing content_type or object_pk field.") |
| 52 |
try: |
| 53 |
model = apps.get_model(*ctype.split(".", 1)) |
| 54 |
target = model._default_manager.using(using).get(pk=object_pk) |
| 55 |
except (LookupError, TypeError): |
| 56 |
return CommentPostBadRequest( |
| 57 |
"Invalid content_type value: %r" % escape(ctype)) |
| 58 |
except AttributeError: |
| 59 |
return CommentPostBadRequest( |
| 60 |
"The given content-type %r does not resolve to a valid model." % escape(ctype)) |
| 61 |
except ObjectDoesNotExist: |
| 62 |
return CommentPostBadRequest( |
| 63 |
"No object matching content-type %r and object PK %r exists." % ( |
| 64 |
escape(ctype), escape(object_pk))) |
| 65 |
except (ValueError, ValidationError) as e: |
| 66 |
return CommentPostBadRequest( |
| 67 |
"Attempting to get content-type %r and object PK %r raised %s" % ( |
| 68 |
escape(ctype), escape(object_pk), e.__class__.__name__)) |
| 69 |
|
| 70 |
|
| 71 |
preview = "preview" in data |
| 72 |
|
| 73 |
|
| 74 |
form = django_comments.get_form()(target, data=data) |
| 75 |
|
| 76 |
|
| 77 |
if form.security_errors(): |
| 78 |
return CommentPostBadRequest( |
| 79 |
"The comment form failed security verification: %s" % escape(str(form.security_errors()))) |
| 80 |
|
| 81 |
|
| 82 |
if form.errors or preview: |
| 83 |
template_list = [ |
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
"comments/%s_%s_preview.html" % (model._meta.app_label, model._meta.model_name), |
| 88 |
"comments/%s_preview.html" % model._meta.app_label, |
| 89 |
|
| 90 |
"comments/%s/%s/preview.html" % (model._meta.app_label, model._meta.model_name), |
| 91 |
"comments/%s/preview.html" % model._meta.app_label, |
| 92 |
"comments/preview.html", |
| 93 |
] |
| 94 |
return render(request, template_list, { |
| 95 |
"comment": form.data.get("comment", ""), |
| 96 |
"form": form, |
| 97 |
"next": data.get("next", next), |
| 98 |
}, |
| 99 |
) |
| 100 |
|
| 101 |
|
| 102 |
comment = form.get_comment_object(site_id=get_current_site(request).id) |
| 103 |
comment.ip_address = request.META.get("REMOTE_ADDR", None) or None |
| 104 |
if request.user.is_authenticated: |
| 105 |
comment.user = request.user |
| 106 |
|
| 107 |
|
| 108 |
responses = signals.comment_will_be_posted.send( |
| 109 |
sender=comment.__class__, |
| 110 |
comment=comment, |
| 111 |
request=request |
| 112 |
) |
| 113 |
|
| 114 |
for (receiver, response) in responses: |
| 115 |
if response is False: |
| 116 |
return CommentPostBadRequest( |
| 117 |
"comment_will_be_posted receiver %r killed the comment" % receiver.__name__) |
| 118 |
|
| 119 |
|
| 120 |
comment.save() |
| 121 |
signals.comment_was_posted.send( |
| 122 |
sender=comment.__class__, |
| 123 |
comment=comment, |
| 124 |
request=request |
| 125 |
) |
| 126 |
|
| 127 |
return next_redirect(request, fallback=next or 'comments-comment-done', |
| 128 |
c=comment._get_pk_val()) |
| 129 |
|
| 130 |
|
| 131 |
comment_done = confirmation_view( |
| 132 |
template="comments/posted.html", |
| 133 |
doc="""Display a "comment was posted" success page.""" |
| 134 |
) |