Mercurial > public > sg101
comparison gpp/forums/views.py @ 107:e94398f5e027
Forums: implemented post delete feature.
author | Brian Neal <bgneal@gmail.com> |
---|---|
date | Tue, 22 Sep 2009 03:36:39 +0000 |
parents | cb72577785df |
children | 80ab249d1adc |
comparison
equal
deleted
inserted
replaced
106:cb72577785df | 107:e94398f5e027 |
---|---|
65 forum = get_object_or_404(Forum.objects.select_related(), slug=slug) | 65 forum = get_object_or_404(Forum.objects.select_related(), slug=slug) |
66 | 66 |
67 if not forum.category.can_access(request.user): | 67 if not forum.category.can_access(request.user): |
68 return HttpResponseForbidden() | 68 return HttpResponseForbidden() |
69 | 69 |
70 topics = forum.topics.select_related('last_post', 'last_post__user') | 70 topics = forum.topics.select_related('user', 'last_post', 'last_post__user') |
71 paginator = create_topic_paginator(topics) | 71 paginator = create_topic_paginator(topics) |
72 page_num = int(request.GET.get('page', 1)) | 72 page_num = int(request.GET.get('page', 1)) |
73 try: | 73 try: |
74 page = paginator.page(page_num) | 74 page = paginator.page(page_num) |
75 except InvalidPage: | 75 except InvalidPage: |
265 'post': post, | 265 'post': post, |
266 'form': form, | 266 'form': form, |
267 'can_moderate': True, | 267 'can_moderate': True, |
268 }, | 268 }, |
269 context_instance=RequestContext(request)) | 269 context_instance=RequestContext(request)) |
270 | |
271 | |
272 @require_POST | |
273 def delete_post(request): | |
274 """ | |
275 This view function allows superusers and forum moderators to delete posts. | |
276 This function is the target of AJAX calls from the client. | |
277 """ | |
278 if not request.user.is_authenticated(): | |
279 return HttpResponseForbidden('Please login to delete a post.') | |
280 | |
281 id = request.POST.get('id') | |
282 if id is None: | |
283 return HttpResponseBadRequest('No post id') | |
284 | |
285 post = get_object_or_404(Post.objects.select_related(), pk=id) | |
286 | |
287 can_delete = request.user.is_superuser or \ | |
288 request.user in post.topic.forum.moderators.all() | |
289 | |
290 if not can_delete: | |
291 return HttpResponseForbidden("You don't have permission to delete that post.") | |
292 | |
293 if post.topic.post_count == 1 and post == post.topic.last_post: | |
294 _delete_topic(post.topic) | |
295 else: | |
296 _delete_post(post) | |
297 | |
298 return HttpResponse("The post has been deleted.") | |
299 | |
300 | |
301 def _delete_post(post): | |
302 """ | |
303 Internal function to delete a single post object. | |
304 Decrements the post author's post count. | |
305 Adjusts the parent topic and forum's last_post as needed. | |
306 """ | |
307 # Adjust post creator's post count | |
308 profile = post.user.get_profile() | |
309 if profile.forum_post_count > 0: | |
310 profile.forum_post_count -= 1 | |
311 profile.save() | |
312 | |
313 # If this post is the last_post in a topic, we need to update | |
314 # both the topic and parent forum's last post fields. If we don't | |
315 # the cascading delete will delete them also! | |
316 | |
317 topic = post.topic | |
318 if topic.last_post == post: | |
319 topic.last_post_pre_delete() | |
320 topic.save() | |
321 | |
322 forum = topic.forum | |
323 if forum.last_post == post: | |
324 forum.last_post_pre_delete() | |
325 forum.save() | |
326 | |
327 # Should be safe to delete the post now: | |
328 post.delete() | |
329 | |
330 | |
331 def _delete_topic(topic): | |
332 """ | |
333 Internal function to delete an entire topic. | |
334 Deletes the topic and all posts contained within. | |
335 Adjusts the parent forum's last_post as needed. | |
336 Note that we don't bother adjusting all the users' | |
337 post counts as that doesn't seem to be worth the effort. | |
338 """ | |
339 if topic.forum.last_post.topic == topic: | |
340 topic.forum.last_post_pre_delete() | |
341 topic.forum.save() | |
342 | |
343 # It should be safe to just delete the topic now. This will | |
344 # automatically delete all posts in the topic. | |
345 topic.delete() |