bgneal@81: """ bgneal@81: Views for the forums application. bgneal@81: """ bgneal@113: import datetime bgneal@113: bgneal@83: from django.contrib.auth.decorators import login_required bgneal@82: from django.http import Http404 bgneal@98: from django.http import HttpResponse bgneal@89: from django.http import HttpResponseBadRequest bgneal@90: from django.http import HttpResponseForbidden bgneal@83: from django.http import HttpResponseRedirect bgneal@83: from django.core.urlresolvers import reverse bgneal@91: from django.core.paginator import InvalidPage bgneal@82: from django.shortcuts import get_object_or_404 bgneal@81: from django.shortcuts import render_to_response bgneal@97: from django.template.loader import render_to_string bgneal@81: from django.template import RequestContext bgneal@89: from django.views.decorators.http import require_POST bgneal@108: from django.utils.text import wrap bgneal@81: bgneal@90: from core.paginator import DiggPaginator bgneal@98: from core.functions import email_admins bgneal@113: from forums.models import Forum, Topic, Post, FlaggedPost, TopicLastVisit, \ bgneal@113: ForumLastVisit bgneal@115: from forums.forms import NewTopicForm, NewPostForm, PostForm, MoveTopicForm, \ bgneal@115: SplitTopicForm bgneal@114: from forums.unread import get_forum_unread_status, get_topic_unread_status, \ bgneal@114: get_post_unread_status bgneal@81: bgneal@117: from bio.models import UserProfile bgneal@90: ####################################################################### bgneal@90: bgneal@93: TOPICS_PER_PAGE = 50 bgneal@113: POSTS_PER_PAGE = 20 bgneal@90: bgneal@93: def create_topic_paginator(topics): bgneal@93: return DiggPaginator(topics, TOPICS_PER_PAGE, body=5, tail=2, margin=3, padding=2) bgneal@93: bgneal@93: def create_post_paginator(posts): bgneal@93: return DiggPaginator(posts, POSTS_PER_PAGE, body=5, tail=2, margin=3, padding=2) bgneal@90: bgneal@90: ####################################################################### bgneal@81: bgneal@81: def index(request): bgneal@82: """ bgneal@82: This view displays all the forums available, ordered in each category. bgneal@82: """ bgneal@100: forums = Forum.objects.forums_for_user(request.user) bgneal@113: get_forum_unread_status(forums, request.user) bgneal@81: cats = {} bgneal@81: for forum in forums: bgneal@81: cat = cats.setdefault(forum.category.id, { bgneal@81: 'cat': forum.category, bgneal@81: 'forums': [], bgneal@81: }) bgneal@81: cat['forums'].append(forum) bgneal@81: bgneal@81: cmpdef = lambda a, b: cmp(a['cat'].position, b['cat'].position) bgneal@81: cats = sorted(cats.values(), cmpdef) bgneal@81: bgneal@81: return render_to_response('forums/index.html', { bgneal@81: 'cats': cats, bgneal@81: }, bgneal@81: context_instance=RequestContext(request)) bgneal@81: bgneal@82: bgneal@81: def forum_index(request, slug): bgneal@82: """ bgneal@82: Displays all the topics in a forum. bgneal@82: """ bgneal@101: forum = get_object_or_404(Forum.objects.select_related(), slug=slug) bgneal@100: bgneal@100: if not forum.category.can_access(request.user): bgneal@100: return HttpResponseForbidden() bgneal@100: bgneal@107: topics = forum.topics.select_related('user', 'last_post', 'last_post__user') bgneal@114: get_topic_unread_status(forum, topics, request.user) bgneal@114: bgneal@93: paginator = create_topic_paginator(topics) bgneal@93: page_num = int(request.GET.get('page', 1)) bgneal@93: try: bgneal@93: page = paginator.page(page_num) bgneal@93: except InvalidPage: bgneal@93: raise Http404 bgneal@97: bgneal@97: # we do this for the template since it is rendered twice bgneal@97: page_nav = render_to_string('forums/pagination.html', {'page': page}) bgneal@111: bgneal@111: can_moderate = _can_moderate(forum, request.user) bgneal@82: bgneal@82: return render_to_response('forums/forum_index.html', { bgneal@82: 'forum': forum, bgneal@93: 'page': page, bgneal@97: 'page_nav': page_nav, bgneal@111: 'can_moderate': can_moderate, bgneal@82: }, bgneal@82: context_instance=RequestContext(request)) bgneal@82: bgneal@82: bgneal@82: def topic_index(request, id): bgneal@82: """ bgneal@82: Displays all the posts in a topic. bgneal@82: """ bgneal@101: topic = get_object_or_404(Topic.objects.select_related(), pk=id) bgneal@100: bgneal@100: if not topic.forum.category.can_access(request.user): bgneal@100: return HttpResponseForbidden() bgneal@100: bgneal@86: topic.view_count += 1 bgneal@86: topic.save() bgneal@86: bgneal@86: posts = topic.posts.select_related() bgneal@114: bgneal@93: paginator = create_post_paginator(posts) bgneal@93: page_num = int(request.GET.get('page', 1)) bgneal@90: try: bgneal@90: page = paginator.page(page_num) bgneal@90: except InvalidPage: bgneal@90: raise Http404 bgneal@117: get_post_unread_status(topic, page.object_list, request.user) bgneal@117: bgneal@117: # Attach user profiles to each post to avoid using get_user_profile() in bgneal@117: # the template. bgneal@117: users = set(post.user.id for post in page.object_list) bgneal@117: bgneal@117: profiles = UserProfile.objects.filter(user__id__in=users).select_related() bgneal@117: user_profiles = dict((profile.user.id, profile) for profile in profiles) bgneal@117: bgneal@117: for post in page.object_list: bgneal@117: post.user_profile = user_profiles[post.user.id] bgneal@90: bgneal@90: last_page = page_num == paginator.num_pages bgneal@86: bgneal@113: if request.user.is_authenticated() and last_page: bgneal@113: _update_last_visit(request.user, topic) bgneal@113: bgneal@97: # we do this for the template since it is rendered twice bgneal@97: page_nav = render_to_string('forums/pagination.html', {'page': page}) bgneal@97: bgneal@109: can_moderate = _can_moderate(topic.forum, request.user) bgneal@104: bgneal@104: can_reply = request.user.is_authenticated() and ( bgneal@104: not topic.locked or can_moderate) bgneal@104: bgneal@86: return render_to_response('forums/topic.html', { bgneal@86: 'forum': topic.forum, bgneal@86: 'topic': topic, bgneal@90: 'page': page, bgneal@97: 'page_nav': page_nav, bgneal@87: 'last_page': last_page, bgneal@104: 'can_moderate': can_moderate, bgneal@104: 'can_reply': can_reply, bgneal@106: 'form': NewPostForm(initial={'topic_id': topic.id}), bgneal@86: }, bgneal@86: context_instance=RequestContext(request)) bgneal@83: bgneal@83: bgneal@83: @login_required bgneal@83: def new_topic(request, slug): bgneal@83: """ bgneal@83: This view handles the creation of new topics. bgneal@83: """ bgneal@101: forum = get_object_or_404(Forum.objects.select_related(), slug=slug) bgneal@100: bgneal@100: if not forum.category.can_access(request.user): bgneal@100: return HttpResponseForbidden() bgneal@100: bgneal@83: if request.method == 'POST': bgneal@102: form = NewTopicForm(request.user, forum, request.POST) bgneal@83: if form.is_valid(): bgneal@102: topic = form.save(request.META.get("REMOTE_ADDR")) bgneal@108: _bump_post_count(request.user) bgneal@83: return HttpResponseRedirect(reverse('forums-new_topic_thanks', bgneal@83: kwargs={'tid': topic.pk})) bgneal@83: else: bgneal@102: form = NewTopicForm(request.user, forum) bgneal@83: bgneal@83: return render_to_response('forums/new_topic.html', { bgneal@83: 'forum': forum, bgneal@83: 'form': form, bgneal@83: }, bgneal@83: context_instance=RequestContext(request)) bgneal@83: bgneal@83: bgneal@83: @login_required bgneal@83: def new_topic_thanks(request, tid): bgneal@83: """ bgneal@83: This view displays the success page for a newly created topic. bgneal@83: """ bgneal@101: topic = get_object_or_404(Topic.objects.select_related(), pk=tid) bgneal@83: return render_to_response('forums/new_topic_thanks.html', { bgneal@83: 'forum': topic.forum, bgneal@83: 'topic': topic, bgneal@83: }, bgneal@83: context_instance=RequestContext(request)) bgneal@89: bgneal@89: bgneal@89: @require_POST bgneal@89: def quick_reply_ajax(request): bgneal@89: """ bgneal@89: This function handles the quick reply to a thread function. This bgneal@89: function is meant to be the target of an AJAX post, and returns bgneal@89: the HTML for the new post, which the client-side script appends bgneal@89: to the document. bgneal@89: """ bgneal@90: if not request.user.is_authenticated(): bgneal@108: return HttpResponseForbidden('Please login or register to post.') bgneal@90: bgneal@106: form = NewPostForm(request.POST) bgneal@89: if form.is_valid(): bgneal@108: if not _can_post_in_topic(form.topic, request.user): bgneal@108: return HttpResponseForbidden("You don't have permission to post in this topic.") bgneal@100: bgneal@108: post = form.save(request.user, request.META.get("REMOTE_ADDR", "")) bgneal@114: post.unread = True bgneal@122: post.user_profile = request.user.get_profile() bgneal@108: _bump_post_count(request.user) bgneal@113: _update_last_visit(request.user, form.topic) bgneal@89: return render_to_response('forums/display_post.html', { bgneal@89: 'post': post, bgneal@113: 'can_moderate': _can_moderate(form.topic.forum, request.user), bgneal@120: 'can_reply': True, bgneal@89: }, bgneal@89: context_instance=RequestContext(request)) bgneal@89: bgneal@108: return HttpResponseBadRequest("Invalid post."); bgneal@89: bgneal@91: bgneal@91: def goto_post(request, post_id): bgneal@91: """ bgneal@91: This function calculates what page a given post is on, then redirects bgneal@91: to that URL. This function is the target of get_absolute_url() for bgneal@91: Post objects. bgneal@91: """ bgneal@101: post = get_object_or_404(Post.objects.select_related(), pk=post_id) bgneal@91: count = post.topic.posts.filter(creation_date__lt=post.creation_date).count() bgneal@91: page = count / POSTS_PER_PAGE + 1 bgneal@91: url = reverse('forums-topic_index', kwargs={'id': post.topic.id}) + \ bgneal@91: '?page=%s#p%s' % (page, post.id) bgneal@91: return HttpResponseRedirect(url) bgneal@91: bgneal@98: bgneal@98: @require_POST bgneal@98: def flag_post(request): bgneal@98: """ bgneal@98: This function handles the flagging of posts by users. This function should bgneal@98: be the target of an AJAX post. bgneal@98: """ bgneal@98: if not request.user.is_authenticated(): bgneal@99: return HttpResponseForbidden('Please login or register to flag a post.') bgneal@98: bgneal@98: id = request.POST.get('id') bgneal@98: if id is None: bgneal@98: return HttpResponseBadRequest('No post id') bgneal@98: bgneal@98: try: bgneal@98: post = Post.objects.get(pk=id) bgneal@98: except Post.DoesNotExist: bgneal@98: return HttpResponseBadRequest('No post with id %s' % id) bgneal@98: bgneal@98: flag = FlaggedPost(user=request.user, post=post) bgneal@98: flag.save() bgneal@98: email_admins('A Post Has Been Flagged', """Hello, bgneal@98: bgneal@98: A user has flagged a forum post for review. bgneal@98: """) bgneal@98: return HttpResponse('The post was flagged. A moderator will review the post shortly. ' \ bgneal@98: 'Thanks for helping to improve the discussions on this site.') bgneal@106: bgneal@106: bgneal@106: @login_required bgneal@106: def edit_post(request, id): bgneal@106: """ bgneal@106: This view function allows authorized users to edit posts. bgneal@106: The superuser, forum moderators, and original author can edit posts. bgneal@106: """ bgneal@106: post = get_object_or_404(Post.objects.select_related(), pk=id) bgneal@108: bgneal@109: can_moderate = _can_moderate(post.topic.forum, request.user) bgneal@108: can_edit = can_moderate or request.user == post.user bgneal@106: bgneal@106: if not can_edit: bgneal@106: return HttpResponseForbidden("You don't have permission to edit that post.") bgneal@106: bgneal@106: if request.method == "POST": bgneal@106: form = PostForm(request.POST, instance=post) bgneal@106: if form.is_valid(): bgneal@115: post = form.save(commit=False) bgneal@115: post.touch() bgneal@115: post.save() bgneal@106: return HttpResponseRedirect(post.get_absolute_url()) bgneal@106: else: bgneal@106: form = PostForm(instance=post) bgneal@106: bgneal@123: post.user_profile = request.user.get_profile() bgneal@123: bgneal@106: return render_to_response('forums/edit_post.html', { bgneal@106: 'forum': post.topic.forum, bgneal@106: 'topic': post.topic, bgneal@106: 'post': post, bgneal@106: 'form': form, bgneal@108: 'can_moderate': can_moderate, bgneal@106: }, bgneal@106: context_instance=RequestContext(request)) bgneal@107: bgneal@107: bgneal@107: @require_POST bgneal@107: def delete_post(request): bgneal@107: """ bgneal@107: This view function allows superusers and forum moderators to delete posts. bgneal@107: This function is the target of AJAX calls from the client. bgneal@107: """ bgneal@107: if not request.user.is_authenticated(): bgneal@107: return HttpResponseForbidden('Please login to delete a post.') bgneal@107: bgneal@107: id = request.POST.get('id') bgneal@107: if id is None: bgneal@107: return HttpResponseBadRequest('No post id') bgneal@107: bgneal@107: post = get_object_or_404(Post.objects.select_related(), pk=id) bgneal@107: bgneal@107: can_delete = request.user.is_superuser or \ bgneal@107: request.user in post.topic.forum.moderators.all() bgneal@107: bgneal@107: if not can_delete: bgneal@107: return HttpResponseForbidden("You don't have permission to delete that post.") bgneal@107: bgneal@147: delete_single_post(post) bgneal@147: return HttpResponse("The post has been deleted.") bgneal@147: bgneal@147: bgneal@147: def delete_single_post(post): bgneal@147: """ bgneal@147: This function deletes a single post. It handles the case of where bgneal@147: a post is the sole post in a topic by deleting the topic also. It bgneal@147: adjusts any foreign keys in Topic or Forum objects that might be pointing bgneal@147: to this post before deleting the post to avoid a cascading delete. bgneal@147: """ bgneal@107: if post.topic.post_count == 1 and post == post.topic.last_post: bgneal@107: _delete_topic(post.topic) bgneal@107: else: bgneal@107: _delete_post(post) bgneal@107: bgneal@107: bgneal@107: def _delete_post(post): bgneal@107: """ bgneal@107: Internal function to delete a single post object. bgneal@107: Decrements the post author's post count. bgneal@107: Adjusts the parent topic and forum's last_post as needed. bgneal@107: """ bgneal@107: # Adjust post creator's post count bgneal@107: profile = post.user.get_profile() bgneal@107: if profile.forum_post_count > 0: bgneal@107: profile.forum_post_count -= 1 bgneal@107: profile.save() bgneal@107: bgneal@107: # If this post is the last_post in a topic, we need to update bgneal@107: # both the topic and parent forum's last post fields. If we don't bgneal@107: # the cascading delete will delete them also! bgneal@107: bgneal@107: topic = post.topic bgneal@107: if topic.last_post == post: bgneal@107: topic.last_post_pre_delete() bgneal@107: topic.save() bgneal@107: bgneal@107: forum = topic.forum bgneal@107: if forum.last_post == post: bgneal@107: forum.last_post_pre_delete() bgneal@107: forum.save() bgneal@107: bgneal@107: # Should be safe to delete the post now: bgneal@107: post.delete() bgneal@107: bgneal@107: bgneal@107: def _delete_topic(topic): bgneal@107: """ bgneal@107: Internal function to delete an entire topic. bgneal@107: Deletes the topic and all posts contained within. bgneal@107: Adjusts the parent forum's last_post as needed. bgneal@107: Note that we don't bother adjusting all the users' bgneal@107: post counts as that doesn't seem to be worth the effort. bgneal@107: """ bgneal@147: if topic.forum.last_post and topic.forum.last_post.topic == topic: bgneal@107: topic.forum.last_post_pre_delete() bgneal@107: topic.forum.save() bgneal@107: bgneal@107: # It should be safe to just delete the topic now. This will bgneal@107: # automatically delete all posts in the topic. bgneal@107: topic.delete() bgneal@108: bgneal@108: bgneal@108: @login_required bgneal@108: def new_post(request, topic_id): bgneal@108: """ bgneal@108: This function is the view for creating a normal, non-quick reply bgneal@108: to a topic. bgneal@108: """ bgneal@108: topic = get_object_or_404(Topic.objects.select_related(), pk=topic_id) bgneal@108: can_post = _can_post_in_topic(topic, request.user) bgneal@108: bgneal@108: if can_post: bgneal@108: if request.method == 'POST': bgneal@108: form = PostForm(request.POST) bgneal@108: if form.is_valid(): bgneal@108: post = form.save(commit=False) bgneal@108: post.topic = topic bgneal@108: post.user = request.user bgneal@108: post.user_ip = request.META.get("REMOTE_ADDR", "") bgneal@108: post.save() bgneal@108: _bump_post_count(request.user) bgneal@113: _update_last_visit(request.user, topic) bgneal@108: return HttpResponseRedirect(post.get_absolute_url()) bgneal@108: else: bgneal@108: quote_id = request.GET.get('quote') bgneal@108: if quote_id: bgneal@108: quote_post = get_object_or_404(Post.objects.select_related(), bgneal@108: pk=quote_id) bgneal@108: form = PostForm(initial={'body': _quote_message(quote_post.user.username, bgneal@108: quote_post.body)}) bgneal@108: else: bgneal@108: form = PostForm() bgneal@108: else: bgneal@108: form = None bgneal@108: bgneal@108: return render_to_response('forums/new_post.html', { bgneal@108: 'forum': topic.forum, bgneal@108: 'topic': topic, bgneal@108: 'form': form, bgneal@108: 'can_post': can_post, bgneal@108: }, bgneal@108: context_instance=RequestContext(request)) bgneal@108: bgneal@108: bgneal@109: @login_required bgneal@109: def mod_topic_stick(request, id): bgneal@109: """ bgneal@109: This view function is for moderators to toggle the sticky status of a topic. bgneal@109: """ bgneal@109: topic = get_object_or_404(Topic.objects.select_related(), pk=id) bgneal@109: if _can_moderate(topic.forum, request.user): bgneal@109: topic.sticky = not topic.sticky bgneal@109: topic.save() bgneal@109: return HttpResponseRedirect(topic.get_absolute_url()) bgneal@109: bgneal@110: return HttpResponseForbidden() bgneal@109: bgneal@109: bgneal@109: @login_required bgneal@109: def mod_topic_lock(request, id): bgneal@109: """ bgneal@109: This view function is for moderators to toggle the locked status of a topic. bgneal@109: """ bgneal@109: topic = get_object_or_404(Topic.objects.select_related(), pk=id) bgneal@109: if _can_moderate(topic.forum, request.user): bgneal@109: topic.locked = not topic.locked bgneal@109: topic.save() bgneal@109: return HttpResponseRedirect(topic.get_absolute_url()) bgneal@109: bgneal@110: return HttpResponseForbidden() bgneal@109: bgneal@109: bgneal@109: @login_required bgneal@109: def mod_topic_delete(request, id): bgneal@109: """ bgneal@109: This view function is for moderators to delete an entire topic. bgneal@109: """ bgneal@109: topic = get_object_or_404(Topic.objects.select_related(), pk=id) bgneal@109: if _can_moderate(topic.forum, request.user): bgneal@109: forum_url = topic.forum.get_absolute_url() bgneal@109: _delete_topic(topic) bgneal@109: return HttpResponseRedirect(forum_url) bgneal@109: bgneal@110: return HttpResponseForbidden() bgneal@110: bgneal@110: bgneal@110: @login_required bgneal@110: def mod_topic_move(request, id): bgneal@110: """ bgneal@110: This view function is for moderators to move a topic to a different forum. bgneal@110: """ bgneal@110: topic = get_object_or_404(Topic.objects.select_related(), pk=id) bgneal@110: if not _can_moderate(topic.forum, request.user): bgneal@110: return HttpResponseForbidden() bgneal@110: bgneal@110: if request.method == 'POST': bgneal@110: form = MoveTopicForm(request.user, request.POST) bgneal@110: if form.is_valid(): bgneal@110: new_forum = form.cleaned_data['forums'] bgneal@110: old_forum = topic.forum bgneal@111: _move_topic(topic, old_forum, new_forum) bgneal@110: return HttpResponseRedirect(topic.get_absolute_url()) bgneal@110: else: bgneal@110: form = MoveTopicForm(request.user) bgneal@110: bgneal@110: return render_to_response('forums/move_topic.html', { bgneal@110: 'forum': topic.forum, bgneal@110: 'topic': topic, bgneal@110: 'form': form, bgneal@110: }, bgneal@110: context_instance=RequestContext(request)) bgneal@109: bgneal@109: bgneal@111: @login_required bgneal@111: def mod_forum(request, slug): bgneal@111: """ bgneal@111: Displays a view to allow moderators to perform various operations bgneal@111: on topics in a forum in bulk. We currently support mass locking/unlocking, bgneal@111: stickying and unstickying, moving, and deleting topics. bgneal@111: """ bgneal@111: forum = get_object_or_404(Forum.objects.select_related(), slug=slug) bgneal@111: if not _can_moderate(forum, request.user): bgneal@111: return HttpResponseForbidden() bgneal@111: bgneal@111: topics = forum.topics.select_related('user', 'last_post', 'last_post__user') bgneal@111: paginator = create_topic_paginator(topics) bgneal@111: page_num = int(request.REQUEST.get('page', 1)) bgneal@111: try: bgneal@111: page = paginator.page(page_num) bgneal@111: except InvalidPage: bgneal@111: raise Http404 bgneal@111: bgneal@111: # we do this for the template since it is rendered twice bgneal@111: page_nav = render_to_string('forums/pagination.html', {'page': page}) bgneal@111: form = None bgneal@111: bgneal@111: if request.method == 'POST': bgneal@111: topic_ids = request.POST.getlist('topic_ids') bgneal@111: url = reverse('forums-mod_forum', kwargs={'slug':forum.slug}) bgneal@111: url += '?page=%s' % page_num bgneal@111: bgneal@111: if len(topic_ids): bgneal@111: if request.POST.get('sticky'): bgneal@111: _bulk_sticky(forum, topic_ids) bgneal@111: return HttpResponseRedirect(url) bgneal@111: elif request.POST.get('lock'): bgneal@111: _bulk_lock(forum, topic_ids) bgneal@111: return HttpResponseRedirect(url) bgneal@111: elif request.POST.get('delete'): bgneal@111: _bulk_delete(forum, topic_ids) bgneal@111: return HttpResponseRedirect(url) bgneal@111: elif request.POST.get('move'): bgneal@111: form = MoveTopicForm(request.user, request.POST, hide_label=True) bgneal@111: if form.is_valid(): bgneal@111: _bulk_move(topic_ids, forum, form.cleaned_data['forums']) bgneal@111: return HttpResponseRedirect(url) bgneal@111: bgneal@111: if form is None: bgneal@111: form = MoveTopicForm(request.user, hide_label=True) bgneal@111: bgneal@111: return render_to_response('forums/mod_forum.html', { bgneal@111: 'forum': forum, bgneal@111: 'page': page, bgneal@111: 'page_nav': page_nav, bgneal@111: 'form': form, bgneal@111: }, bgneal@111: context_instance=RequestContext(request)) bgneal@111: bgneal@111: bgneal@113: @login_required bgneal@113: @require_POST bgneal@113: def forum_catchup(request, slug): bgneal@113: """ bgneal@113: This view marks all the topics in the forum as being read. bgneal@113: """ bgneal@113: forum = get_object_or_404(Forum.objects.select_related(), slug=slug) bgneal@113: bgneal@113: if not forum.category.can_access(request.user): bgneal@113: return HttpResponseForbidden() bgneal@113: bgneal@113: forum.catchup(request.user) bgneal@113: return HttpResponseRedirect(forum.get_absolute_url()) bgneal@113: bgneal@113: bgneal@115: @login_required bgneal@115: def mod_topic_split(request, id): bgneal@115: """ bgneal@115: This view function allows moderators to split posts off to a new topic. bgneal@115: """ bgneal@115: topic = get_object_or_404(Topic.objects.select_related(), pk=id) bgneal@115: if not _can_moderate(topic.forum, request.user): bgneal@115: return HttpResponseRedirect(topic.get_absolute_url()) bgneal@115: bgneal@115: if request.method == "POST": bgneal@115: form = SplitTopicForm(request.user, request.POST) bgneal@115: if form.is_valid(): bgneal@115: if form.split_at: bgneal@115: _split_topic_at(topic, form.post_ids[0], bgneal@115: form.cleaned_data['forums'], bgneal@115: form.cleaned_data['name']) bgneal@115: else: bgneal@115: _split_topic(topic, form.post_ids, bgneal@115: form.cleaned_data['forums'], bgneal@115: form.cleaned_data['name']) bgneal@115: bgneal@115: return HttpResponseRedirect(topic.get_absolute_url()) bgneal@115: else: bgneal@115: form = SplitTopicForm(request.user) bgneal@115: bgneal@115: posts = topic.posts.select_related() bgneal@115: bgneal@115: return render_to_response('forums/mod_split_topic.html', { bgneal@115: 'forum': topic.forum, bgneal@115: 'topic': topic, bgneal@115: 'posts': posts, bgneal@115: 'form': form, bgneal@115: }, bgneal@115: context_instance=RequestContext(request)) bgneal@115: bgneal@115: bgneal@109: def _can_moderate(forum, user): bgneal@109: """ bgneal@109: Determines if a user has permission to moderate a given forum. bgneal@109: """ bgneal@109: return user.is_authenticated() and ( bgneal@109: user.is_superuser or user in forum.moderators.all()) bgneal@109: bgneal@109: bgneal@108: def _can_post_in_topic(topic, user): bgneal@108: """ bgneal@108: This function returns true if the given user can post in the given topic bgneal@108: and false otherwise. bgneal@108: """ bgneal@108: return (not topic.locked and topic.forum.category.can_access(user)) or \ bgneal@108: (user.is_superuser or user in topic.forum.moderators.all()) bgneal@108: bgneal@108: bgneal@108: def _bump_post_count(user): bgneal@108: """ bgneal@108: Increments the forum_post_count for the given user. bgneal@108: """ bgneal@108: profile = user.get_profile() bgneal@108: profile.forum_post_count += 1 bgneal@108: profile.save() bgneal@108: bgneal@108: bgneal@108: def _quote_message(who, message): bgneal@111: """ bgneal@111: Builds a message reply by quoting the existing message in a bgneal@111: typical email-like fashion. The quoting is compatible with Markdown. bgneal@111: """ bgneal@111: header = '*%s wrote:*\n\n' % (who, ) bgneal@111: lines = wrap(message, 55).split('\n') bgneal@111: for i, line in enumerate(lines): bgneal@111: lines[i] = '> ' + line bgneal@111: return header + '\n'.join(lines) bgneal@111: bgneal@111: bgneal@111: def _move_topic(topic, old_forum, new_forum): bgneal@111: if new_forum != old_forum: bgneal@111: topic.forum = new_forum bgneal@111: topic.save() bgneal@111: # Have to adjust foreign keys to last_post, denormalized counts, etc.: bgneal@112: old_forum.sync() bgneal@111: old_forum.save() bgneal@112: new_forum.sync() bgneal@111: new_forum.save() bgneal@111: bgneal@111: bgneal@111: def _bulk_sticky(forum, topic_ids): bgneal@111: """ bgneal@111: Performs a toggle on the sticky status for a given list of topic ids. bgneal@111: """ bgneal@111: topics = Topic.objects.filter(pk__in=topic_ids) bgneal@111: for topic in topics: bgneal@111: if topic.forum == forum: bgneal@111: topic.sticky = not topic.sticky bgneal@111: topic.save() bgneal@111: bgneal@111: bgneal@111: def _bulk_lock(forum, topic_ids): bgneal@111: """ bgneal@111: Performs a toggle on the locked status for a given list of topic ids. bgneal@111: """ bgneal@111: topics = Topic.objects.filter(pk__in=topic_ids) bgneal@111: for topic in topics: bgneal@111: if topic.forum == forum: bgneal@111: topic.locked = not topic.locked bgneal@111: topic.save() bgneal@111: bgneal@111: bgneal@111: def _bulk_delete(forum, topic_ids): bgneal@111: """ bgneal@111: Deletes the list of topics. bgneal@111: """ bgneal@111: topics = Topic.objects.filter(pk__in=topic_ids).select_related() bgneal@111: for topic in topics: bgneal@111: if topic.forum == forum: bgneal@111: _delete_topic(topic) bgneal@111: bgneal@111: bgneal@111: def _bulk_move(topic_ids, old_forum, new_forum): bgneal@111: """ bgneal@111: Moves the list of topics to a new forum. bgneal@111: """ bgneal@111: topics = Topic.objects.filter(pk__in=topic_ids).select_related() bgneal@111: for topic in topics: bgneal@111: if topic.forum == old_forum: bgneal@111: _move_topic(topic, old_forum, new_forum) bgneal@111: bgneal@113: bgneal@113: def _update_last_visit(user, topic): bgneal@113: """ bgneal@113: Does the bookkeeping for the last visit status for the user to the bgneal@113: topic/forum. bgneal@113: """ bgneal@113: now = datetime.datetime.now() bgneal@113: try: bgneal@113: flv = ForumLastVisit.objects.get(user=user, forum=topic.forum) bgneal@113: except ForumLastVisit.DoesNotExist: bgneal@113: flv = ForumLastVisit(user=user, forum=topic.forum) bgneal@113: flv.begin_date = now bgneal@113: bgneal@113: flv.end_date = now bgneal@113: flv.save() bgneal@113: bgneal@113: if topic.update_date > flv.begin_date: bgneal@113: try: bgneal@113: tlv = TopicLastVisit.objects.get(user=user, topic=topic) bgneal@113: except TopicLastVisit.DoesNotExist: bgneal@113: tlv = TopicLastVisit(user=user, topic=topic) bgneal@113: bgneal@113: tlv.touch() bgneal@113: tlv.save() bgneal@113: bgneal@115: bgneal@115: def _split_topic_at(topic, post_id, new_forum, new_name): bgneal@115: """ bgneal@115: This function splits the post given by post_id and all posts that come bgneal@115: after it in the given topic to a new topic in a new forum. bgneal@115: It is assumed the caller has been checked for moderator rights. bgneal@115: """ bgneal@115: post = get_object_or_404(Post, id=post_id) bgneal@115: if post.topic == topic: bgneal@115: post_ids = Post.objects.filter(topic=topic, bgneal@115: creation_date__gte=post.creation_date).values_list('id', flat=True) bgneal@115: _split_topic(topic, post_ids, new_forum, new_name) bgneal@115: bgneal@115: bgneal@115: def _split_topic(topic, post_ids, new_forum, new_name): bgneal@115: """ bgneal@115: This function splits the posts given by the post_ids list in the bgneal@115: given topic to a new topic in a new forum. bgneal@115: It is assumed the caller has been checked for moderator rights. bgneal@115: """ bgneal@115: posts = Post.objects.filter(topic=topic, id__in=post_ids) bgneal@115: if len(posts) > 0: bgneal@115: new_topic = Topic(forum=new_forum, name=new_name, user=posts[0].user) bgneal@115: new_topic.save() bgneal@115: for post in posts: bgneal@115: post.topic = new_topic bgneal@115: post.save() bgneal@115: bgneal@115: topic.post_count_update() bgneal@115: topic.save() bgneal@115: new_topic.post_count_update() bgneal@115: new_topic.save() bgneal@115: topic.forum.sync() bgneal@115: topic.forum.save() bgneal@115: new_forum.sync() bgneal@115: new_forum.save()