Mercurial > public > sg101
comparison comments/views.py @ 581:ee87ea74d46b
For Django 1.4, rearranged project structure for new manage.py.
author | Brian Neal <bgneal@gmail.com> |
---|---|
date | Sat, 05 May 2012 17:10:48 -0500 |
parents | gpp/comments/views.py@8c1832b9d815 |
children | ad69236e8501 |
comparison
equal
deleted
inserted
replaced
580:c525f3e0b5d0 | 581:ee87ea74d46b |
---|---|
1 """ | |
2 Views for the comments application. | |
3 """ | |
4 from django.contrib.auth.decorators import login_required | |
5 from django.core.exceptions import ObjectDoesNotExist | |
6 from django.http import HttpResponse | |
7 from django.http import HttpResponseRedirect | |
8 from django.http import HttpResponseBadRequest | |
9 from django.http import HttpResponseForbidden | |
10 from django.db.models import get_model | |
11 from django.shortcuts import render_to_response | |
12 from django.template import RequestContext | |
13 from django.utils.html import escape | |
14 from django.views.decorators.http import require_POST | |
15 | |
16 from core.functions import email_admins | |
17 from core.markup import site_markup | |
18 from comments.forms import CommentForm | |
19 from comments.models import Comment | |
20 from comments.models import CommentFlag | |
21 import antispam | |
22 import antispam.utils | |
23 | |
24 | |
25 @login_required | |
26 @require_POST | |
27 def post_comment(request): | |
28 """ | |
29 This function handles the posting of comments. If successful, returns | |
30 the comment text as the response. This function is meant to be the target | |
31 of an AJAX post. | |
32 """ | |
33 # Look up the object we're trying to comment about | |
34 ctype = request.POST.get('content_type', None) | |
35 object_pk = request.POST.get('object_pk', None) | |
36 if ctype is None or object_pk is None: | |
37 return HttpResponseBadRequest('Missing content_type or object_pk field.') | |
38 | |
39 try: | |
40 model = get_model(*ctype.split('.', 1)) | |
41 target = model.objects.get(pk=object_pk) | |
42 except TypeError: | |
43 return HttpResponseBadRequest( | |
44 "Invalid content_type value: %r" % escape(ctype)) | |
45 except AttributeError: | |
46 return HttpResponseBadRequest( | |
47 "The given content-type %r does not resolve to a valid model." % \ | |
48 escape(ctype)) | |
49 except ObjectDoesNotExist: | |
50 return HttpResponseBadRequest( | |
51 "No object matching content-type %r and object PK %r exists." % \ | |
52 (escape(ctype), escape(object_pk))) | |
53 | |
54 # Can we comment on the target object? | |
55 if hasattr(target, 'can_comment_on'): | |
56 if callable(target.can_comment_on): | |
57 can_comment_on = target.can_comment_on() | |
58 else: | |
59 can_comment_on = target.can_comment_on | |
60 else: | |
61 can_comment_on = True | |
62 | |
63 if not can_comment_on: | |
64 return HttpResponseForbidden('Cannot comment on this item.') | |
65 | |
66 # Check form validity | |
67 | |
68 form = CommentForm(target, request.POST) | |
69 if not form.is_valid(): | |
70 return HttpResponseBadRequest('Invalid comment; missing parameters?') | |
71 | |
72 comment = form.get_comment_object(request.user, request.META.get("REMOTE_ADDR", None)) | |
73 | |
74 # Check for spam | |
75 | |
76 if antispam.utils.spam_check(request, comment.comment): | |
77 return HttpResponseForbidden(antispam.BUSTED_MESSAGE) | |
78 | |
79 comment.save() | |
80 | |
81 # return the rendered comment | |
82 return render_to_response('comments/comment.html', { | |
83 'comment': comment, | |
84 }, | |
85 context_instance = RequestContext(request)) | |
86 | |
87 | |
88 @require_POST | |
89 def flag_comment(request): | |
90 """ | |
91 This function handles the flagging of comments by users. This function should | |
92 be the target of an AJAX post. | |
93 """ | |
94 if not request.user.is_authenticated(): | |
95 return HttpResponse('Please login or register to flag a comment.') | |
96 | |
97 id = request.POST.get('id', None) | |
98 if id is None: | |
99 return HttpResponseBadRequest('No id') | |
100 | |
101 try: | |
102 comment = Comment.objects.get(pk=id) | |
103 except Comment.DoesNotExist: | |
104 return HttpResponseBadRequest('No comment with id %s' % id) | |
105 | |
106 flag = CommentFlag(user=request.user, comment=comment) | |
107 flag.save() | |
108 email_admins('A Comment Has Been Flagged', """Hello, | |
109 | |
110 A user has flagged a comment for review. | |
111 """) | |
112 return HttpResponse('The comment was flagged. A moderator will review the comment shortly. ' \ | |
113 'Thanks for helping to improve the discussions on this site.') | |
114 | |
115 | |
116 @require_POST | |
117 def markdown_preview(request): | |
118 """ | |
119 This function should be the target of an AJAX POST. It takes the 'data' parameter | |
120 from the POST parameters and returns a rendered HTML page from the data, which | |
121 is assumed to be in markdown format. The HTML page is suitable for the preview | |
122 function for a javascript editor such as markItUp. | |
123 """ | |
124 if not request.user.is_authenticated(): | |
125 return HttpResponseForbidden('This service is only available to logged in users.') | |
126 | |
127 data = request.POST.get('data', None) | |
128 if data is None: | |
129 return HttpResponseBadRequest('No data') | |
130 | |
131 return render_to_response('comments/markdown_preview.html', { | |
132 'data': site_markup(data), | |
133 }, | |
134 context_instance = RequestContext(request)) |