Mercurial > public > sg101
comparison gpp/forums/views.py @ 106:cb72577785df
Forums: implemented the edit post function.
author | Brian Neal <bgneal@gmail.com> |
---|---|
date | Sat, 19 Sep 2009 21:49:56 +0000 |
parents | 59688577a8f1 |
children | e94398f5e027 |
comparison
equal
deleted
inserted
replaced
105:08ddfd835305 | 106:cb72577785df |
---|---|
19 from core.functions import email_admins | 19 from core.functions import email_admins |
20 from forums.models import Forum | 20 from forums.models import Forum |
21 from forums.models import Topic | 21 from forums.models import Topic |
22 from forums.models import Post | 22 from forums.models import Post |
23 from forums.models import FlaggedPost | 23 from forums.models import FlaggedPost |
24 from forums.forms import NewTopicForm | 24 from forums.forms import NewTopicForm, NewPostForm, PostForm |
25 from forums.forms import PostForm | |
26 | 25 |
27 ####################################################################### | 26 ####################################################################### |
28 | 27 |
29 TOPICS_PER_PAGE = 50 | 28 TOPICS_PER_PAGE = 50 |
30 POSTS_PER_PAGE = 2 | 29 POSTS_PER_PAGE = 2 |
125 'page': page, | 124 'page': page, |
126 'page_nav': page_nav, | 125 'page_nav': page_nav, |
127 'last_page': last_page, | 126 'last_page': last_page, |
128 'can_moderate': can_moderate, | 127 'can_moderate': can_moderate, |
129 'can_reply': can_reply, | 128 'can_reply': can_reply, |
130 'form': PostForm(initial={'topic_id': topic.id}), | 129 'form': NewPostForm(initial={'topic_id': topic.id}), |
131 }, | 130 }, |
132 context_instance=RequestContext(request)) | 131 context_instance=RequestContext(request)) |
133 | 132 |
134 | 133 |
135 @login_required | 134 @login_required |
180 to the document. | 179 to the document. |
181 """ | 180 """ |
182 if not request.user.is_authenticated(): | 181 if not request.user.is_authenticated(): |
183 return HttpResponseForbidden() | 182 return HttpResponseForbidden() |
184 | 183 |
185 form = PostForm(request.POST) | 184 form = NewPostForm(request.POST) |
186 if form.is_valid(): | 185 if form.is_valid(): |
187 if form.topic.locked or not form.topic.forum.category.can_access(request.user): | 186 if form.topic.locked or not form.topic.forum.category.can_access(request.user): |
188 return HttpResponseForbidden() | 187 return HttpResponseForbidden() |
189 | 188 |
190 post = form.save(request.user, request.META.get("REMOTE_ADDR")) | 189 post = form.save(request.user, request.META.get("REMOTE_ADDR")) |
234 | 233 |
235 A user has flagged a forum post for review. | 234 A user has flagged a forum post for review. |
236 """) | 235 """) |
237 return HttpResponse('The post was flagged. A moderator will review the post shortly. ' \ | 236 return HttpResponse('The post was flagged. A moderator will review the post shortly. ' \ |
238 'Thanks for helping to improve the discussions on this site.') | 237 'Thanks for helping to improve the discussions on this site.') |
238 | |
239 | |
240 @login_required | |
241 def edit_post(request, id): | |
242 """ | |
243 This view function allows authorized users to edit posts. | |
244 The superuser, forum moderators, and original author can edit posts. | |
245 """ | |
246 post = get_object_or_404(Post.objects.select_related(), pk=id) | |
247 can_edit = request.user == post.user or \ | |
248 request.user.is_superuser or \ | |
249 request.user in post.topic.forum.moderators.all() | |
250 | |
251 if not can_edit: | |
252 return HttpResponseForbidden("You don't have permission to edit that post.") | |
253 | |
254 if request.method == "POST": | |
255 form = PostForm(request.POST, instance=post) | |
256 if form.is_valid(): | |
257 form.save() | |
258 return HttpResponseRedirect(post.get_absolute_url()) | |
259 else: | |
260 form = PostForm(instance=post) | |
261 | |
262 return render_to_response('forums/edit_post.html', { | |
263 'forum': post.topic.forum, | |
264 'topic': post.topic, | |
265 'post': post, | |
266 'form': form, | |
267 'can_moderate': True, | |
268 }, | |
269 context_instance=RequestContext(request)) |