Mercurial > public > sg101
comparison gpp/forums/views.py @ 98:d0d779dd0832
Forums: flag posts.
author | Brian Neal <bgneal@gmail.com> |
---|---|
date | Sun, 13 Sep 2009 21:45:35 +0000 |
parents | 96eec1ed0fd3 |
children | 10d6182b9f6e |
comparison
equal
deleted
inserted
replaced
97:96eec1ed0fd3 | 98:d0d779dd0832 |
---|---|
1 """ | 1 """ |
2 Views for the forums application. | 2 Views for the forums application. |
3 """ | 3 """ |
4 from django.contrib.auth.decorators import login_required | 4 from django.contrib.auth.decorators import login_required |
5 from django.http import Http404 | 5 from django.http import Http404 |
6 from django.http import HttpResponse | |
6 from django.http import HttpResponseBadRequest | 7 from django.http import HttpResponseBadRequest |
7 from django.http import HttpResponseForbidden | 8 from django.http import HttpResponseForbidden |
8 from django.http import HttpResponseRedirect | 9 from django.http import HttpResponseRedirect |
9 from django.core.urlresolvers import reverse | 10 from django.core.urlresolvers import reverse |
10 from django.core.paginator import InvalidPage | 11 from django.core.paginator import InvalidPage |
13 from django.template.loader import render_to_string | 14 from django.template.loader import render_to_string |
14 from django.template import RequestContext | 15 from django.template import RequestContext |
15 from django.views.decorators.http import require_POST | 16 from django.views.decorators.http import require_POST |
16 | 17 |
17 from core.paginator import DiggPaginator | 18 from core.paginator import DiggPaginator |
19 from core.functions import email_admins | |
18 from forums.models import Forum | 20 from forums.models import Forum |
19 from forums.models import Topic | 21 from forums.models import Topic |
20 from forums.models import Post | 22 from forums.models import Post |
23 from forums.models import FlaggedPost | |
21 from forums.forms import NewTopicForm | 24 from forums.forms import NewTopicForm |
22 from forums.forms import PostForm | 25 from forums.forms import PostForm |
23 | 26 |
24 ####################################################################### | 27 ####################################################################### |
25 | 28 |
180 page = count / POSTS_PER_PAGE + 1 | 183 page = count / POSTS_PER_PAGE + 1 |
181 url = reverse('forums-topic_index', kwargs={'id': post.topic.id}) + \ | 184 url = reverse('forums-topic_index', kwargs={'id': post.topic.id}) + \ |
182 '?page=%s#p%s' % (page, post.id) | 185 '?page=%s#p%s' % (page, post.id) |
183 return HttpResponseRedirect(url) | 186 return HttpResponseRedirect(url) |
184 | 187 |
188 | |
189 @require_POST | |
190 def flag_post(request): | |
191 """ | |
192 This function handles the flagging of posts by users. This function should | |
193 be the target of an AJAX post. | |
194 """ | |
195 if not request.user.is_authenticated(): | |
196 return HttpResponse('Please login or register to flag a post.') | |
197 | |
198 id = request.POST.get('id') | |
199 if id is None: | |
200 return HttpResponseBadRequest('No post id') | |
201 | |
202 try: | |
203 post = Post.objects.get(pk=id) | |
204 except Post.DoesNotExist: | |
205 return HttpResponseBadRequest('No post with id %s' % id) | |
206 | |
207 flag = FlaggedPost(user=request.user, post=post) | |
208 flag.save() | |
209 email_admins('A Post Has Been Flagged', """Hello, | |
210 | |
211 A user has flagged a forum post for review. | |
212 """) | |
213 return HttpResponse('The post was flagged. A moderator will review the post shortly. ' \ | |
214 'Thanks for helping to improve the discussions on this site.') |