Mercurial > public > sg101
diff 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 |
line wrap: on
line diff
--- a/gpp/forums/views.py Sat Sep 26 03:55:50 2009 +0000 +++ b/gpp/forums/views.py Sat Sep 26 18:03:57 2009 +0000 @@ -112,9 +112,7 @@ # we do this for the template since it is rendered twice page_nav = render_to_string('forums/pagination.html', {'page': page}) - can_moderate = request.user.is_authenticated() and ( - request.user.is_superuser or \ - request.user in topic.forum.moderators.all()) + can_moderate = _can_moderate(topic.forum, request.user) can_reply = request.user.is_authenticated() and ( not topic.locked or can_moderate) @@ -248,9 +246,7 @@ """ post = get_object_or_404(Post.objects.select_related(), pk=id) - can_moderate = request.user.is_superuser or \ - request.user in post.topic.forum.moderators.all() - + can_moderate = _can_moderate(post.topic.forum, request.user) can_edit = can_moderate or request.user == post.user if not can_edit: @@ -391,6 +387,56 @@ context_instance=RequestContext(request)) +@login_required +def mod_topic_stick(request, id): + """ + This view function is for moderators to toggle the sticky status of a topic. + """ + topic = get_object_or_404(Topic.objects.select_related(), pk=id) + if _can_moderate(topic.forum, request.user): + topic.sticky = not topic.sticky + topic.save() + return HttpResponseRedirect(topic.get_absolute_url()) + + return HttpResponseForbidden() + + +@login_required +def mod_topic_lock(request, id): + """ + This view function is for moderators to toggle the locked status of a topic. + """ + topic = get_object_or_404(Topic.objects.select_related(), pk=id) + if _can_moderate(topic.forum, request.user): + topic.locked = not topic.locked + topic.save() + return HttpResponseRedirect(topic.get_absolute_url()) + + return HttpResponseForbidden() + + +@login_required +def mod_topic_delete(request, id): + """ + This view function is for moderators to delete an entire topic. + """ + topic = get_object_or_404(Topic.objects.select_related(), pk=id) + if _can_moderate(topic.forum, request.user): + forum_url = topic.forum.get_absolute_url() + _delete_topic(topic) + return HttpResponseRedirect(forum_url) + + return HttpResponseForbidden() + + +def _can_moderate(forum, user): + """ + Determines if a user has permission to moderate a given forum. + """ + return user.is_authenticated() and ( + user.is_superuser or user in forum.moderators.all()) + + def _can_post_in_topic(topic, user): """ This function returns true if the given user can post in the given topic