comparison gpp/forums/views.py @ 109:07be3e39e639

Forums: implemented topic level moderator controls.
author Brian Neal <bgneal@gmail.com>
date Sat, 26 Sep 2009 18:03:57 +0000
parents 80ab249d1adc
children c329bfaed4a7
comparison
equal deleted inserted replaced
108:80ab249d1adc 109:07be3e39e639
110 last_page = page_num == paginator.num_pages 110 last_page = page_num == paginator.num_pages
111 111
112 # we do this for the template since it is rendered twice 112 # we do this for the template since it is rendered twice
113 page_nav = render_to_string('forums/pagination.html', {'page': page}) 113 page_nav = render_to_string('forums/pagination.html', {'page': page})
114 114
115 can_moderate = request.user.is_authenticated() and ( 115 can_moderate = _can_moderate(topic.forum, request.user)
116 request.user.is_superuser or \
117 request.user in topic.forum.moderators.all())
118 116
119 can_reply = request.user.is_authenticated() and ( 117 can_reply = request.user.is_authenticated() and (
120 not topic.locked or can_moderate) 118 not topic.locked or can_moderate)
121 119
122 return render_to_response('forums/topic.html', { 120 return render_to_response('forums/topic.html', {
246 This view function allows authorized users to edit posts. 244 This view function allows authorized users to edit posts.
247 The superuser, forum moderators, and original author can edit posts. 245 The superuser, forum moderators, and original author can edit posts.
248 """ 246 """
249 post = get_object_or_404(Post.objects.select_related(), pk=id) 247 post = get_object_or_404(Post.objects.select_related(), pk=id)
250 248
251 can_moderate = request.user.is_superuser or \ 249 can_moderate = _can_moderate(post.topic.forum, request.user)
252 request.user in post.topic.forum.moderators.all()
253
254 can_edit = can_moderate or request.user == post.user 250 can_edit = can_moderate or request.user == post.user
255 251
256 if not can_edit: 252 if not can_edit:
257 return HttpResponseForbidden("You don't have permission to edit that post.") 253 return HttpResponseForbidden("You don't have permission to edit that post.")
258 254
389 'can_post': can_post, 385 'can_post': can_post,
390 }, 386 },
391 context_instance=RequestContext(request)) 387 context_instance=RequestContext(request))
392 388
393 389
390 @login_required
391 def mod_topic_stick(request, id):
392 """
393 This view function is for moderators to toggle the sticky status of a topic.
394 """
395 topic = get_object_or_404(Topic.objects.select_related(), pk=id)
396 if _can_moderate(topic.forum, request.user):
397 topic.sticky = not topic.sticky
398 topic.save()
399 return HttpResponseRedirect(topic.get_absolute_url())
400
401 return HttpResponseForbidden()
402
403
404 @login_required
405 def mod_topic_lock(request, id):
406 """
407 This view function is for moderators to toggle the locked status of a topic.
408 """
409 topic = get_object_or_404(Topic.objects.select_related(), pk=id)
410 if _can_moderate(topic.forum, request.user):
411 topic.locked = not topic.locked
412 topic.save()
413 return HttpResponseRedirect(topic.get_absolute_url())
414
415 return HttpResponseForbidden()
416
417
418 @login_required
419 def mod_topic_delete(request, id):
420 """
421 This view function is for moderators to delete an entire topic.
422 """
423 topic = get_object_or_404(Topic.objects.select_related(), pk=id)
424 if _can_moderate(topic.forum, request.user):
425 forum_url = topic.forum.get_absolute_url()
426 _delete_topic(topic)
427 return HttpResponseRedirect(forum_url)
428
429 return HttpResponseForbidden()
430
431
432 def _can_moderate(forum, user):
433 """
434 Determines if a user has permission to moderate a given forum.
435 """
436 return user.is_authenticated() and (
437 user.is_superuser or user in forum.moderators.all())
438
439
394 def _can_post_in_topic(topic, user): 440 def _can_post_in_topic(topic, user):
395 """ 441 """
396 This function returns true if the given user can post in the given topic 442 This function returns true if the given user can post in the given topic
397 and false otherwise. 443 and false otherwise.
398 """ 444 """