Mercurial > public > sg101
comparison gpp/comments/views.py @ 1:dbd703f7d63a
Initial import of sg101 stuff from private repository.
author | gremmie |
---|---|
date | Mon, 06 Apr 2009 02:43:12 +0000 |
parents | |
children | d203a4a986d2 |
comparison
equal
deleted
inserted
replaced
0:900ba3c7b765 | 1:dbd703f7d63a |
---|---|
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 comments.forms import CommentForm | |
18 from comments.models import Comment | |
19 from comments.models import CommentFlag | |
20 | |
21 @login_required | |
22 @require_POST | |
23 def post_comment(request): | |
24 """ | |
25 This function handles the posting of comments. If successful, returns | |
26 the comment text as the response. This function is mean't to be the target | |
27 of an AJAX post. | |
28 """ | |
29 # Look up the object we're trying to comment about | |
30 ctype = request.POST.get('content_type', None) | |
31 object_pk = request.POST.get('object_pk', None) | |
32 if ctype is None or object_pk is None: | |
33 return HttpResponseBadRequest('Missing content_type or object_pk field.') | |
34 | |
35 try: | |
36 model = get_model(*ctype.split('.', 1)) | |
37 target = model.objects.get(pk=object_pk) | |
38 except TypeError: | |
39 return HttpResponseBadRequest( | |
40 "Invalid content_type value: %r" % escape(ctype)) | |
41 except AttributeError: | |
42 return HttpResponseBadRequest( | |
43 "The given content-type %r does not resolve to a valid model." % \ | |
44 escape(ctype)) | |
45 except ObjectDoesNotExist: | |
46 return HttpResponseBadRequest( | |
47 "No object matching content-type %r and object PK %r exists." % \ | |
48 (escape(ctype), escape(object_pk))) | |
49 | |
50 # Can we comment on the target object? | |
51 if hasattr(target, 'can_comment_on'): | |
52 if callable(target.can_comment_on): | |
53 can_comment_on = target.can_comment_on() | |
54 else: | |
55 can_comment_on = target.can_comment_on | |
56 else: | |
57 can_comment_on = True | |
58 | |
59 if not can_comment_on: | |
60 return HttpResponseForbidden('Cannot comment on this item.') | |
61 | |
62 # Check form validity | |
63 | |
64 form = CommentForm(target, request.POST) | |
65 if not form.is_valid(): | |
66 return HttpResponseBadRequest('Invalid comment; missing parameters?') | |
67 | |
68 # else, create and save the comment | |
69 | |
70 comment = form.get_comment_object(request.user, request.META.get("REMOTE_ADDR", None)) | |
71 comment.save() | |
72 | |
73 # return the rendered comment | |
74 return render_to_response('comments/comment.html', { | |
75 'comment': comment, | |
76 }, | |
77 context_instance = RequestContext(request)) | |
78 | |
79 | |
80 @require_POST | |
81 def flag_comment(request): | |
82 """ | |
83 This function handles the flagging of comments by users. This function should | |
84 be the target of an AJAX post. | |
85 """ | |
86 if not request.user.is_authenticated(): | |
87 return HttpResponse('Please login or register to flag a comment.') | |
88 | |
89 id = request.POST.get('id', None) | |
90 if id is None: | |
91 return HttpResponseBadRequest('No id') | |
92 | |
93 try: | |
94 comment = Comment.objects.get(pk=id) | |
95 except Comment.DoesNotExist: | |
96 return HttpResponseBadRequest('No comment with id %s' % id) | |
97 | |
98 flag = CommentFlag(user=request.user, comment=comment) | |
99 flag.save() | |
100 email_admins('A Comment Has Been Flagged', """Hello, | |
101 | |
102 A user has flagged a comment for review. | |
103 """) | |
104 return HttpResponse('The comment was flagged. A moderator will review the comment shortly. ' \ | |
105 'Thanks for helping to improve the discussions on this site.') | |
106 | |
107 | |
108 @require_POST | |
109 def markdown_preview(request): | |
110 """ | |
111 This function should be the target of an AJAX POST. It takes the 'data' parameter | |
112 from the POST parameters and returns a rendered HTML page from the data, which | |
113 is assumed to be in markdown format. The HTML page is suitable for the preview | |
114 function for a javascript editor such as markItUp. | |
115 """ | |
116 if not request.user.is_authenticated(): | |
117 return HttpResponseForbidden('This service is only available to logged in users.') | |
118 | |
119 data = request.POST.get('data', None) | |
120 if data is None: | |
121 return HttpResponseBadRequest('No data') | |
122 | |
123 return render_to_response('comments/markdown_preview.html', { | |
124 'data': data, | |
125 }, | |
126 context_instance = RequestContext(request)) |