bgneal@232: """ bgneal@232: Views for the forums application. bgneal@680: bgneal@232: """ bgneal@329: import collections bgneal@232: import datetime bgneal@232: bgneal@232: from django.contrib.auth.decorators import login_required bgneal@232: from django.contrib.auth.models import User bgneal@232: from django.http import Http404 bgneal@232: from django.http import HttpResponse bgneal@232: from django.http import HttpResponseBadRequest bgneal@232: from django.http import HttpResponseForbidden bgneal@232: from django.http import HttpResponseRedirect bgneal@232: from django.core.urlresolvers import reverse bgneal@232: from django.core.paginator import InvalidPage bgneal@232: from django.shortcuts import get_object_or_404 bgneal@1031: from django.shortcuts import render bgneal@232: from django.template.loader import render_to_string bgneal@232: from django.views.decorators.http import require_POST bgneal@232: from django.db.models import F bgneal@232: bgneal@459: import antispam bgneal@459: import antispam.utils bgneal@791: from bio.models import BadgeOwnership bgneal@232: from core.paginator import DiggPaginator bgneal@566: from core.functions import email_admins, quote_message bgneal@232: bgneal@459: from forums.models import (Forum, Topic, Post, FlaggedPost, TopicLastVisit, bgneal@791: ForumLastVisit) bgneal@459: from forums.forms import (NewTopicForm, NewPostForm, PostForm, MoveTopicForm, bgneal@459: SplitTopicForm) bgneal@459: from forums.unread import (get_forum_unread_status, get_topic_unread_status, bgneal@459: get_post_unread_status, get_unread_topics) bgneal@459: bgneal@459: import forums.permissions as perms bgneal@469: from forums.signals import (notify_new_topic, notify_updated_topic, bgneal@469: notify_new_post, notify_updated_post) bgneal@522: from forums.latest import get_latest_topic_ids bgneal@232: bgneal@232: ####################################################################### bgneal@232: bgneal@232: TOPICS_PER_PAGE = 50 bgneal@232: POSTS_PER_PAGE = 20 bgneal@263: FEED_BASE = '/feeds/forums/' bgneal@263: FORUM_FEED = FEED_BASE + '%s/' bgneal@232: bgneal@232: bgneal@232: def get_page_num(request): bgneal@232: """Returns the value of the 'page' variable in GET if it exists, or 1 bgneal@232: if it does not.""" bgneal@232: bgneal@232: try: bgneal@232: page_num = int(request.GET.get('page', 1)) bgneal@232: except ValueError: bgneal@232: page_num = 1 bgneal@232: bgneal@232: return page_num bgneal@232: bgneal@232: bgneal@232: def create_topic_paginator(topics): bgneal@232: return DiggPaginator(topics, TOPICS_PER_PAGE, body=5, tail=2, margin=3, padding=2) bgneal@232: bgneal@232: def create_post_paginator(posts): bgneal@232: return DiggPaginator(posts, POSTS_PER_PAGE, body=5, tail=2, margin=3, padding=2) bgneal@232: bgneal@232: bgneal@232: def attach_topic_page_ranges(topics): bgneal@232: """Attaches a page_range attribute to each topic in the supplied list. bgneal@232: This attribute will be None if it is a single page topic. This is used bgneal@232: by the templates to generate "goto page x" links. bgneal@232: """ bgneal@232: for topic in topics: bgneal@232: if topic.post_count > POSTS_PER_PAGE: bgneal@286: pp = DiggPaginator(range(topic.post_count), POSTS_PER_PAGE, bgneal@232: body=2, tail=3, margin=1) bgneal@232: topic.page_range = pp.page(1).page_range bgneal@232: else: bgneal@232: topic.page_range = None bgneal@232: bgneal@232: ####################################################################### bgneal@232: bgneal@232: def index(request): bgneal@232: """ bgneal@232: This view displays all the forums available, ordered in each category. bgneal@232: """ bgneal@232: public_forums = Forum.objects.public_forums() bgneal@263: feeds = [{'name': 'All Forums', 'feed': FEED_BASE}] bgneal@232: bgneal@232: forums = Forum.objects.forums_for_user(request.user) bgneal@232: get_forum_unread_status(forums, request.user) bgneal@232: cats = {} bgneal@232: for forum in forums: bgneal@232: forum.has_feed = forum in public_forums bgneal@232: if forum.has_feed: bgneal@232: feeds.append({ bgneal@232: 'name': '%s Forum' % forum.name, bgneal@263: 'feed': FORUM_FEED % forum.slug, bgneal@232: }) bgneal@232: bgneal@232: cat = cats.setdefault(forum.category.id, { bgneal@232: 'cat': forum.category, bgneal@232: 'forums': [], bgneal@232: }) bgneal@232: cat['forums'].append(forum) bgneal@232: bgneal@232: cmpdef = lambda a, b: cmp(a['cat'].position, b['cat'].position) bgneal@232: cats = sorted(cats.values(), cmpdef) bgneal@232: bgneal@1031: return render(request, 'forums/index.html', { bgneal@232: 'cats': cats, bgneal@232: 'feeds': feeds, bgneal@1161: 'V3_DESIGN': True, bgneal@1031: }) bgneal@232: bgneal@232: bgneal@232: def forum_index(request, slug): bgneal@232: """ bgneal@232: Displays all the topics in a forum. bgneal@232: """ bgneal@232: forum = get_object_or_404(Forum.objects.select_related(), slug=slug) bgneal@232: bgneal@459: if not perms.can_access(forum.category, request.user): bgneal@232: return HttpResponseForbidden() bgneal@232: bgneal@263: feed = None bgneal@263: if not forum.category.groups.all(): bgneal@263: feed = { bgneal@263: 'name': '%s Forum' % forum.name, bgneal@263: 'feed': FORUM_FEED % forum.slug, bgneal@263: } bgneal@263: bgneal@232: topics = forum.topics.select_related('user', 'last_post', 'last_post__user') bgneal@232: paginator = create_topic_paginator(topics) bgneal@232: page_num = get_page_num(request) bgneal@232: try: bgneal@232: page = paginator.page(page_num) bgneal@232: except InvalidPage: bgneal@232: raise Http404 bgneal@232: bgneal@328: get_topic_unread_status(forum, page.object_list, request.user) bgneal@232: attach_topic_page_ranges(page.object_list) bgneal@232: bgneal@459: can_moderate = perms.can_moderate(forum, request.user) bgneal@286: bgneal@1031: return render(request, 'forums/forum_index.html', { bgneal@232: 'forum': forum, bgneal@263: 'feed': feed, bgneal@232: 'page': page, bgneal@232: 'can_moderate': can_moderate, bgneal@1161: 'V3_DESIGN': True, bgneal@1031: }) bgneal@232: bgneal@232: bgneal@232: def topic_index(request, id): bgneal@232: """ bgneal@232: Displays all the posts in a topic. bgneal@232: """ bgneal@232: topic = get_object_or_404(Topic.objects.select_related( bgneal@232: 'forum', 'forum__category', 'last_post'), pk=id) bgneal@232: bgneal@459: if not perms.can_access(topic.forum.category, request.user): bgneal@232: return HttpResponseForbidden() bgneal@232: bgneal@232: topic.view_count = F('view_count') + 1 bgneal@232: topic.save(force_update=True) bgneal@232: bgneal@791: posts = topic.posts.\ bgneal@791: select_related('user', 'user__profile').\ bgneal@791: prefetch_related('attachments') bgneal@232: bgneal@232: paginator = create_post_paginator(posts) bgneal@232: page_num = get_page_num(request) bgneal@232: try: bgneal@232: page = paginator.page(page_num) bgneal@232: except InvalidPage: bgneal@232: raise Http404 bgneal@232: get_post_unread_status(topic, page.object_list, request.user) bgneal@232: bgneal@791: # Get the BadgeOwnership & Badges for each user who has posted in the bgneal@791: # thread. This is done to save SQL queries in the template. bgneal@232: bgneal@445: last_post_on_page = None bgneal@791: profile_ids = [] bgneal@232: for post in page.object_list: bgneal@445: last_post_on_page = post bgneal@791: profile_ids.append(post.user.profile.pk) bgneal@285: bgneal@791: bo_qs = BadgeOwnership.objects.filter(profile__in=profile_ids).\ bgneal@791: select_related() bgneal@329: bos = collections.defaultdict(list) bgneal@791: for bo in bo_qs: bgneal@791: bos[bo.profile.pk].append(bo) bgneal@329: bgneal@791: for post in page.object_list: bgneal@791: post.user.profile.badge_ownership = bos[post.user.profile.pk] bgneal@232: bgneal@232: last_page = page_num == paginator.num_pages bgneal@232: bgneal@445: if request.user.is_authenticated(): bgneal@445: if last_page or last_post_on_page is None: bgneal@445: visit_time = datetime.datetime.now() bgneal@445: else: bgneal@445: visit_time = last_post_on_page.creation_date bgneal@445: _update_last_visit(request.user, topic, visit_time) bgneal@232: bgneal@232: # we do this for the template since it is rendered twice bgneal@232: page_nav = render_to_string('forums/pagination.html', {'page': page}) bgneal@232: bgneal@459: can_moderate = perms.can_moderate(topic.forum, request.user) bgneal@232: bgneal@232: can_reply = request.user.is_authenticated() and ( bgneal@232: not topic.locked or can_moderate) bgneal@232: bgneal@232: is_favorite = request.user.is_authenticated() and ( bgneal@232: topic in request.user.favorite_topics.all()) bgneal@232: bgneal@232: is_subscribed = request.user.is_authenticated() and ( bgneal@232: topic in request.user.subscriptions.all()) bgneal@232: bgneal@1031: return render(request, 'forums/topic.html', { bgneal@232: 'forum': topic.forum, bgneal@232: 'topic': topic, bgneal@232: 'page': page, bgneal@232: 'page_nav': page_nav, bgneal@232: 'last_page': last_page, bgneal@232: 'can_moderate': can_moderate, bgneal@232: 'can_reply': can_reply, bgneal@232: 'form': NewPostForm(initial={'topic_id': topic.id}), bgneal@232: 'is_favorite': is_favorite, bgneal@232: 'is_subscribed': is_subscribed, bgneal@1031: }) bgneal@232: bgneal@232: bgneal@374: def topic_unread(request, id): bgneal@374: """ bgneal@374: This view redirects to the first post the user hasn't read, if we can bgneal@374: figure that out. Otherwise we redirect to the topic. bgneal@374: bgneal@374: """ bgneal@374: topic_url = reverse('forums-topic_index', kwargs={'id': id}) bgneal@374: bgneal@374: if request.user.is_authenticated(): bgneal@680: topic = get_object_or_404(Topic.objects.select_related('forum', 'user'), pk=id) bgneal@374: try: bgneal@376: tlv = TopicLastVisit.objects.get(user=request.user, topic=topic) bgneal@374: except TopicLastVisit.DoesNotExist: bgneal@376: try: bgneal@376: flv = ForumLastVisit.objects.get(user=request.user, bgneal@376: forum=topic.forum) bgneal@376: except ForumLastVisit.DoesNotExist: bgneal@376: return HttpResponseRedirect(topic_url) bgneal@376: else: bgneal@376: last_visit = flv.begin_date bgneal@376: else: bgneal@376: last_visit = tlv.last_visit bgneal@374: bgneal@376: posts = Post.objects.filter(topic=topic, creation_date__gt=last_visit) bgneal@374: if posts: bgneal@374: return _goto_post(posts[0]) bgneal@374: else: bgneal@374: # just go to the last post in the topic bgneal@374: return _goto_post(topic.last_post) bgneal@374: bgneal@374: # user isn't authenticated, just go to the topic bgneal@374: return HttpResponseRedirect(topic_url) bgneal@374: bgneal@374: bgneal@529: def topic_latest(request, id): bgneal@529: """ bgneal@529: This view shows the latest (last) post in a given topic. bgneal@529: bgneal@529: """ bgneal@680: topic = get_object_or_404(Topic.objects.select_related('forum', 'user'), pk=id) bgneal@529: bgneal@529: if topic.last_post: bgneal@529: return _goto_post(topic.last_post) bgneal@529: bgneal@529: raise Http404 bgneal@529: bgneal@529: bgneal@232: @login_required bgneal@232: def new_topic(request, slug): bgneal@232: """ bgneal@232: This view handles the creation of new topics. bgneal@232: """ bgneal@232: forum = get_object_or_404(Forum.objects.select_related(), slug=slug) bgneal@232: bgneal@459: if not perms.can_access(forum.category, request.user): bgneal@232: return HttpResponseForbidden() bgneal@232: bgneal@232: if request.method == 'POST': bgneal@232: form = NewTopicForm(request.user, forum, request.POST) bgneal@232: if form.is_valid(): bgneal@232: if antispam.utils.spam_check(request, form.cleaned_data['body']): bgneal@232: return HttpResponseRedirect(reverse('antispam-suspended')) bgneal@232: bgneal@232: topic = form.save(request.META.get("REMOTE_ADDR")) bgneal@232: _bump_post_count(request.user) bgneal@232: return HttpResponseRedirect(reverse('forums-new_topic_thanks', bgneal@232: kwargs={'tid': topic.pk})) bgneal@232: else: bgneal@232: form = NewTopicForm(request.user, forum) bgneal@286: bgneal@1031: return render(request, 'forums/new_topic.html', { bgneal@232: 'forum': forum, bgneal@232: 'form': form, bgneal@1031: }) bgneal@232: bgneal@232: bgneal@232: @login_required bgneal@232: def new_topic_thanks(request, tid): bgneal@232: """ bgneal@232: This view displays the success page for a newly created topic. bgneal@232: """ bgneal@232: topic = get_object_or_404(Topic.objects.select_related(), pk=tid) bgneal@1031: return render(request, 'forums/new_topic_thanks.html', { bgneal@232: 'forum': topic.forum, bgneal@232: 'topic': topic, bgneal@1031: }) bgneal@232: bgneal@232: bgneal@232: @require_POST bgneal@232: def quick_reply_ajax(request): bgneal@232: """ bgneal@232: This function handles the quick reply to a thread function. This bgneal@232: function is meant to be the target of an AJAX post, and returns bgneal@232: the HTML for the new post, which the client-side script appends bgneal@232: to the document. bgneal@232: """ bgneal@232: if not request.user.is_authenticated(): bgneal@232: return HttpResponseForbidden('Please login or register to post.') bgneal@232: bgneal@232: form = NewPostForm(request.POST) bgneal@232: if form.is_valid(): bgneal@459: if not perms.can_post(form.topic, request.user): bgneal@232: return HttpResponseForbidden("You don't have permission to post in this topic.") bgneal@232: if antispam.utils.spam_check(request, form.cleaned_data['body']): bgneal@232: return HttpResponseForbidden(antispam.BUSTED_MESSAGE) bgneal@232: bgneal@232: post = form.save(request.user, request.META.get("REMOTE_ADDR", "")) bgneal@232: post.unread = True bgneal@285: post.attach_list = post.attachments.all() bgneal@232: _bump_post_count(request.user) bgneal@446: _update_last_visit(request.user, form.topic, datetime.datetime.now()) bgneal@285: bgneal@1031: return render(request, 'forums/display_post.html', { bgneal@232: 'post': post, bgneal@459: 'can_moderate': perms.can_moderate(form.topic.forum, request.user), bgneal@232: 'can_reply': True, bgneal@1031: }) bgneal@232: bgneal@963: # The client side javascript is pretty simplistic right now and we don't bgneal@963: # want to change it yet. It is expecting a single error string. Just grab bgneal@963: # the first error message and use that. bgneal@963: errors = form.errors.as_data() bgneal@963: msg = errors.values()[0][0].message if errors else 'Unknown error' bgneal@963: return HttpResponseBadRequest(msg) bgneal@232: bgneal@232: bgneal@374: def _goto_post(post): bgneal@374: """ bgneal@374: Calculate what page the given post is on in its parent topic, then bgneal@374: return a redirect to it. bgneal@374: bgneal@374: """ bgneal@374: count = post.topic.posts.filter(creation_date__lt=post.creation_date).count() bgneal@374: page = count / POSTS_PER_PAGE + 1 bgneal@374: url = (reverse('forums-topic_index', kwargs={'id': post.topic.id}) + bgneal@374: '?page=%s#p%s' % (page, post.id)) bgneal@374: return HttpResponseRedirect(url) bgneal@374: bgneal@374: bgneal@232: def goto_post(request, post_id): bgneal@232: """ bgneal@232: This function calculates what page a given post is on, then redirects bgneal@232: to that URL. This function is the target of get_absolute_url() for bgneal@232: Post objects. bgneal@232: """ bgneal@232: post = get_object_or_404(Post.objects.select_related(), pk=post_id) bgneal@374: return _goto_post(post) bgneal@232: bgneal@232: bgneal@232: @require_POST bgneal@232: def flag_post(request): bgneal@232: """ bgneal@232: This function handles the flagging of posts by users. This function should bgneal@232: be the target of an AJAX post. bgneal@232: """ bgneal@232: if not request.user.is_authenticated(): bgneal@232: return HttpResponseForbidden('Please login or register to flag a post.') bgneal@232: bgneal@232: id = request.POST.get('id') bgneal@232: if id is None: bgneal@232: return HttpResponseBadRequest('No post id') bgneal@232: bgneal@232: try: bgneal@232: post = Post.objects.get(pk=id) bgneal@232: except Post.DoesNotExist: bgneal@232: return HttpResponseBadRequest('No post with id %s' % id) bgneal@232: bgneal@232: flag = FlaggedPost(user=request.user, post=post) bgneal@232: flag.save() bgneal@232: email_admins('A Post Has Been Flagged', """Hello, bgneal@232: bgneal@232: A user has flagged a forum post for review. bgneal@232: """) bgneal@232: return HttpResponse('The post was flagged. A moderator will review the post shortly. ' \ bgneal@232: 'Thanks for helping to improve the discussions on this site.') bgneal@232: bgneal@232: bgneal@232: @login_required bgneal@232: def edit_post(request, id): bgneal@232: """ bgneal@232: This view function allows authorized users to edit posts. bgneal@232: The superuser, forum moderators, and original author can edit posts. bgneal@232: """ bgneal@232: post = get_object_or_404(Post.objects.select_related(), pk=id) bgneal@232: bgneal@459: can_moderate = perms.can_moderate(post.topic.forum, request.user) bgneal@232: can_edit = can_moderate or request.user == post.user bgneal@232: bgneal@232: if not can_edit: bgneal@232: return HttpResponseForbidden("You don't have permission to edit that post.") bgneal@232: bgneal@295: topic_name = None bgneal@295: first_post = Post.objects.filter(topic=post.topic).order_by('creation_date')[0] bgneal@295: if first_post.id == post.id: bgneal@295: topic_name = post.topic.name bgneal@295: bgneal@232: if request.method == "POST": bgneal@295: form = PostForm(request.POST, instance=post, topic_name=topic_name) bgneal@232: if form.is_valid(): bgneal@232: if antispam.utils.spam_check(request, form.cleaned_data['body']): bgneal@232: return HttpResponseRedirect(reverse('antispam-suspended')) bgneal@232: post = form.save(commit=False) bgneal@232: post.touch() bgneal@963: post.save(html=form.body_html) bgneal@469: notify_updated_post(post) bgneal@285: bgneal@295: # if we are editing a first post, save the parent topic as well bgneal@295: if topic_name: bgneal@295: post.topic.save() bgneal@469: notify_updated_topic(post.topic) bgneal@295: bgneal@285: # Save any attachments bgneal@286: form.attach_proc.save_attachments(post) bgneal@285: bgneal@232: return HttpResponseRedirect(post.get_absolute_url()) bgneal@232: else: bgneal@295: form = PostForm(instance=post, topic_name=topic_name) bgneal@232: bgneal@1031: return render(request, 'forums/edit_post.html', { bgneal@232: 'forum': post.topic.forum, bgneal@232: 'topic': post.topic, bgneal@232: 'post': post, bgneal@232: 'form': form, bgneal@232: 'can_moderate': can_moderate, bgneal@1031: }) bgneal@232: bgneal@232: bgneal@232: @require_POST bgneal@232: def delete_post(request): bgneal@232: """ bgneal@232: This view function allows superusers and forum moderators to delete posts. bgneal@232: This function is the target of AJAX calls from the client. bgneal@232: """ bgneal@232: if not request.user.is_authenticated(): bgneal@232: return HttpResponseForbidden('Please login to delete a post.') bgneal@232: bgneal@232: id = request.POST.get('id') bgneal@232: if id is None: bgneal@232: return HttpResponseBadRequest('No post id') bgneal@232: bgneal@232: post = get_object_or_404(Post.objects.select_related(), pk=id) bgneal@232: bgneal@460: if not perms.can_moderate(post.topic.forum, request.user): bgneal@232: return HttpResponseForbidden("You don't have permission to delete that post.") bgneal@232: bgneal@232: delete_single_post(post) bgneal@232: return HttpResponse("The post has been deleted.") bgneal@232: bgneal@232: bgneal@232: def delete_single_post(post): bgneal@232: """ bgneal@232: This function deletes a single post. It handles the case of where bgneal@232: a post is the sole post in a topic by deleting the topic also. It bgneal@232: adjusts any foreign keys in Topic or Forum objects that might be pointing bgneal@232: to this post before deleting the post to avoid a cascading delete. bgneal@232: """ bgneal@232: if post.topic.post_count == 1 and post == post.topic.last_post: bgneal@232: _delete_topic(post.topic) bgneal@232: else: bgneal@232: _delete_post(post) bgneal@232: bgneal@232: bgneal@232: def _delete_post(post): bgneal@232: """ bgneal@232: Internal function to delete a single post object. bgneal@232: Decrements the post author's post count. bgneal@232: Adjusts the parent topic and forum's last_post as needed. bgneal@232: """ bgneal@232: # Adjust post creator's post count bgneal@789: profile = post.user.profile bgneal@232: if profile.forum_post_count > 0: bgneal@232: profile.forum_post_count -= 1 bgneal@562: profile.save(content_update=False) bgneal@232: bgneal@232: # If this post is the last_post in a topic, we need to update bgneal@232: # both the topic and parent forum's last post fields. If we don't bgneal@232: # the cascading delete will delete them also! bgneal@232: bgneal@232: topic = post.topic bgneal@232: if topic.last_post == post: bgneal@232: topic.last_post_pre_delete() bgneal@232: topic.save() bgneal@232: bgneal@232: forum = topic.forum bgneal@232: if forum.last_post == post: bgneal@232: forum.last_post_pre_delete() bgneal@232: forum.save() bgneal@232: bgneal@285: # delete any attachments bgneal@285: post.attachments.clear() bgneal@285: bgneal@232: # Should be safe to delete the post now: bgneal@232: post.delete() bgneal@232: bgneal@232: bgneal@232: def _delete_topic(topic): bgneal@232: """ bgneal@232: Internal function to delete an entire topic. bgneal@232: Deletes the topic and all posts contained within. bgneal@232: Adjusts the parent forum's last_post as needed. bgneal@232: Note that we don't bother adjusting all the users' bgneal@232: post counts as that doesn't seem to be worth the effort. bgneal@232: """ bgneal@318: parent_forum = topic.forum bgneal@318: if parent_forum.last_post and parent_forum.last_post.topic == topic: bgneal@318: parent_forum.last_post_pre_delete(deleting_topic=True) bgneal@318: parent_forum.save() bgneal@232: bgneal@232: # delete subscriptions to this topic bgneal@232: topic.subscribers.clear() bgneal@232: topic.bookmarkers.clear() bgneal@232: bgneal@285: # delete all attachments bgneal@285: posts = Post.objects.filter(topic=topic) bgneal@285: for post in posts: bgneal@285: post.attachments.clear() bgneal@285: bgneal@318: # Null out the topic's last post so we don't have a foreign key pointing bgneal@318: # to a post when we delete posts. bgneal@318: topic.last_post = None bgneal@318: topic.save() bgneal@318: bgneal@318: # delete all posts in bulk bgneal@318: posts.delete() bgneal@318: bgneal@318: # It should be safe to just delete the topic now. bgneal@232: topic.delete() bgneal@232: bgneal@318: # Resync parent forum's post and topic counts bgneal@318: parent_forum.sync() bgneal@318: parent_forum.save() bgneal@318: bgneal@232: bgneal@232: @login_required bgneal@232: def new_post(request, topic_id): bgneal@232: """ bgneal@232: This function is the view for creating a normal, non-quick reply bgneal@232: to a topic. bgneal@232: """ bgneal@232: topic = get_object_or_404(Topic.objects.select_related(), pk=topic_id) bgneal@459: can_post = perms.can_post(topic, request.user) bgneal@232: bgneal@232: if can_post: bgneal@232: if request.method == 'POST': bgneal@232: form = PostForm(request.POST) bgneal@232: if form.is_valid(): bgneal@232: if antispam.utils.spam_check(request, form.cleaned_data['body']): bgneal@232: return HttpResponseRedirect(reverse('antispam-suspended')) bgneal@232: post = form.save(commit=False) bgneal@232: post.topic = topic bgneal@232: post.user = request.user bgneal@232: post.user_ip = request.META.get("REMOTE_ADDR", "") bgneal@963: post.save(html=form.body_html) bgneal@469: notify_new_post(post) bgneal@285: bgneal@285: # Save any attachments bgneal@286: form.attach_proc.save_attachments(post) bgneal@285: bgneal@232: _bump_post_count(request.user) bgneal@446: _update_last_visit(request.user, topic, datetime.datetime.now()) bgneal@232: return HttpResponseRedirect(post.get_absolute_url()) bgneal@232: else: bgneal@232: quote_id = request.GET.get('quote') bgneal@232: if quote_id: bgneal@232: quote_post = get_object_or_404(Post.objects.select_related(), bgneal@232: pk=quote_id) bgneal@566: form = PostForm(initial={'body': quote_message(quote_post.user.username, bgneal@232: quote_post.body)}) bgneal@232: else: bgneal@232: form = PostForm() bgneal@232: else: bgneal@232: form = None bgneal@232: bgneal@1031: return render(request, 'forums/new_post.html', { bgneal@232: 'forum': topic.forum, bgneal@232: 'topic': topic, bgneal@232: 'form': form, bgneal@232: 'can_post': can_post, bgneal@1031: }) bgneal@232: bgneal@232: bgneal@232: @login_required bgneal@232: def mod_topic_stick(request, id): bgneal@232: """ bgneal@232: This view function is for moderators to toggle the sticky status of a topic. bgneal@232: """ bgneal@232: topic = get_object_or_404(Topic.objects.select_related(), pk=id) bgneal@459: if perms.can_moderate(topic.forum, request.user): bgneal@232: topic.sticky = not topic.sticky bgneal@232: topic.save() bgneal@232: return HttpResponseRedirect(topic.get_absolute_url()) bgneal@232: bgneal@232: return HttpResponseForbidden() bgneal@232: bgneal@232: bgneal@232: @login_required bgneal@232: def mod_topic_lock(request, id): bgneal@232: """ bgneal@232: This view function is for moderators to toggle the locked status of a topic. bgneal@232: """ bgneal@232: topic = get_object_or_404(Topic.objects.select_related(), pk=id) bgneal@459: if perms.can_moderate(topic.forum, request.user): bgneal@232: topic.locked = not topic.locked bgneal@232: topic.save() bgneal@232: return HttpResponseRedirect(topic.get_absolute_url()) bgneal@232: bgneal@232: return HttpResponseForbidden() bgneal@232: bgneal@232: bgneal@232: @login_required bgneal@232: def mod_topic_delete(request, id): bgneal@232: """ bgneal@232: This view function is for moderators to delete an entire topic. bgneal@232: """ bgneal@232: topic = get_object_or_404(Topic.objects.select_related(), pk=id) bgneal@459: if perms.can_moderate(topic.forum, request.user): bgneal@232: forum_url = topic.forum.get_absolute_url() bgneal@232: _delete_topic(topic) bgneal@232: return HttpResponseRedirect(forum_url) bgneal@232: bgneal@232: return HttpResponseForbidden() bgneal@232: bgneal@232: bgneal@232: @login_required bgneal@232: def mod_topic_move(request, id): bgneal@232: """ bgneal@232: This view function is for moderators to move a topic to a different forum. bgneal@232: """ bgneal@232: topic = get_object_or_404(Topic.objects.select_related(), pk=id) bgneal@459: if not perms.can_moderate(topic.forum, request.user): bgneal@232: return HttpResponseForbidden() bgneal@232: bgneal@232: if request.method == 'POST': bgneal@232: form = MoveTopicForm(request.user, request.POST) bgneal@232: if form.is_valid(): bgneal@232: new_forum = form.cleaned_data['forums'] bgneal@232: old_forum = topic.forum bgneal@232: _move_topic(topic, old_forum, new_forum) bgneal@232: return HttpResponseRedirect(topic.get_absolute_url()) bgneal@232: else: bgneal@232: form = MoveTopicForm(request.user) bgneal@232: bgneal@1031: return render(request, 'forums/move_topic.html', { bgneal@232: 'forum': topic.forum, bgneal@232: 'topic': topic, bgneal@232: 'form': form, bgneal@1031: }) bgneal@232: bgneal@232: bgneal@232: @login_required bgneal@232: def mod_forum(request, slug): bgneal@232: """ bgneal@232: Displays a view to allow moderators to perform various operations bgneal@232: on topics in a forum in bulk. We currently support mass locking/unlocking, bgneal@232: stickying and unstickying, moving, and deleting topics. bgneal@232: """ bgneal@232: forum = get_object_or_404(Forum.objects.select_related(), slug=slug) bgneal@459: if not perms.can_moderate(forum, request.user): bgneal@232: return HttpResponseForbidden() bgneal@232: bgneal@232: topics = forum.topics.select_related('user', 'last_post', 'last_post__user') bgneal@232: paginator = create_topic_paginator(topics) bgneal@232: page_num = get_page_num(request) bgneal@232: try: bgneal@232: page = paginator.page(page_num) bgneal@232: except InvalidPage: bgneal@232: raise Http404 bgneal@232: bgneal@232: # we do this for the template since it is rendered twice bgneal@232: page_nav = render_to_string('forums/pagination.html', {'page': page}) bgneal@232: form = None bgneal@232: bgneal@232: if request.method == 'POST': bgneal@232: topic_ids = request.POST.getlist('topic_ids') bgneal@232: url = reverse('forums-mod_forum', kwargs={'slug':forum.slug}) bgneal@232: url += '?page=%s' % page_num bgneal@232: bgneal@232: if len(topic_ids): bgneal@232: if request.POST.get('sticky'): bgneal@232: _bulk_sticky(forum, topic_ids) bgneal@232: return HttpResponseRedirect(url) bgneal@232: elif request.POST.get('lock'): bgneal@232: _bulk_lock(forum, topic_ids) bgneal@232: return HttpResponseRedirect(url) bgneal@232: elif request.POST.get('delete'): bgneal@232: _bulk_delete(forum, topic_ids) bgneal@232: return HttpResponseRedirect(url) bgneal@232: elif request.POST.get('move'): bgneal@232: form = MoveTopicForm(request.user, request.POST, hide_label=True) bgneal@232: if form.is_valid(): bgneal@232: _bulk_move(topic_ids, forum, form.cleaned_data['forums']) bgneal@232: return HttpResponseRedirect(url) bgneal@286: bgneal@232: if form is None: bgneal@232: form = MoveTopicForm(request.user, hide_label=True) bgneal@232: bgneal@1031: return render(request, 'forums/mod_forum.html', { bgneal@232: 'forum': forum, bgneal@232: 'page': page, bgneal@232: 'page_nav': page_nav, bgneal@232: 'form': form, bgneal@1031: }) bgneal@232: bgneal@232: bgneal@232: @login_required bgneal@232: @require_POST bgneal@382: def catchup_all(request): bgneal@382: """ bgneal@382: This view marks all forums as being read. bgneal@382: """ bgneal@384: forum_ids = Forum.objects.forum_ids_for_user(request.user) bgneal@382: bgneal@1031: TopicLastVisit.objects.filter( bgneal@1031: user=request.user, bgneal@382: topic__forum__id__in=forum_ids).delete() bgneal@382: bgneal@382: now = datetime.datetime.now() bgneal@384: ForumLastVisit.objects.filter(user=request.user, bgneal@384: forum__in=forum_ids).update(begin_date=now, end_date=now) bgneal@382: bgneal@382: return HttpResponseRedirect(reverse('forums-index')) bgneal@382: bgneal@382: bgneal@382: @login_required bgneal@382: @require_POST bgneal@232: def forum_catchup(request, slug): bgneal@232: """ bgneal@232: This view marks all the topics in the forum as being read. bgneal@232: """ bgneal@232: forum = get_object_or_404(Forum.objects.select_related(), slug=slug) bgneal@232: bgneal@459: if not perms.can_access(forum.category, request.user): bgneal@232: return HttpResponseForbidden() bgneal@232: bgneal@232: forum.catchup(request.user) bgneal@232: return HttpResponseRedirect(forum.get_absolute_url()) bgneal@232: bgneal@232: bgneal@232: @login_required bgneal@232: def mod_topic_split(request, id): bgneal@232: """ bgneal@232: This view function allows moderators to split posts off to a new topic. bgneal@232: """ bgneal@232: topic = get_object_or_404(Topic.objects.select_related(), pk=id) bgneal@459: if not perms.can_moderate(topic.forum, request.user): bgneal@232: return HttpResponseRedirect(topic.get_absolute_url()) bgneal@232: bgneal@232: if request.method == "POST": bgneal@232: form = SplitTopicForm(request.user, request.POST) bgneal@232: if form.is_valid(): bgneal@232: if form.split_at: bgneal@232: _split_topic_at(topic, form.post_ids[0], bgneal@232: form.cleaned_data['forums'], bgneal@232: form.cleaned_data['name']) bgneal@232: else: bgneal@232: _split_topic(topic, form.post_ids, bgneal@232: form.cleaned_data['forums'], bgneal@232: form.cleaned_data['name']) bgneal@232: bgneal@232: return HttpResponseRedirect(topic.get_absolute_url()) bgneal@232: else: bgneal@232: form = SplitTopicForm(request.user) bgneal@232: bgneal@232: posts = topic.posts.select_related() bgneal@232: bgneal@1031: return render(request, 'forums/mod_split_topic.html', { bgneal@232: 'forum': topic.forum, bgneal@232: 'topic': topic, bgneal@232: 'posts': posts, bgneal@232: 'form': form, bgneal@1031: }) bgneal@232: bgneal@232: bgneal@232: @login_required bgneal@232: def unread_topics(request): bgneal@232: """Displays the topics with unread posts for a given user.""" bgneal@232: bgneal@232: topics = get_unread_topics(request.user) bgneal@232: bgneal@232: paginator = create_topic_paginator(topics) bgneal@232: page_num = get_page_num(request) bgneal@232: try: bgneal@232: page = paginator.page(page_num) bgneal@232: except InvalidPage: bgneal@232: raise Http404 bgneal@232: bgneal@232: attach_topic_page_ranges(page.object_list) bgneal@232: bgneal@232: # we do this for the template since it is rendered twice bgneal@232: page_nav = render_to_string('forums/pagination.html', {'page': page}) bgneal@232: bgneal@1031: return render(request, 'forums/topic_list.html', { bgneal@232: 'title': 'Topics With Unread Posts', bgneal@232: 'page': page, bgneal@232: 'page_nav': page_nav, bgneal@374: 'unread': True, bgneal@1031: }) bgneal@232: bgneal@232: bgneal@232: def unanswered_topics(request): bgneal@232: """Displays the topics with no replies.""" bgneal@232: bgneal@232: forum_ids = Forum.objects.forum_ids_for_user(request.user) bgneal@232: topics = Topic.objects.filter(forum__id__in=forum_ids, bgneal@232: post_count=1).select_related( bgneal@232: 'forum', 'user', 'last_post', 'last_post__user') bgneal@232: bgneal@232: paginator = create_topic_paginator(topics) bgneal@232: page_num = get_page_num(request) bgneal@232: try: bgneal@232: page = paginator.page(page_num) bgneal@232: except InvalidPage: bgneal@232: raise Http404 bgneal@232: bgneal@232: attach_topic_page_ranges(page.object_list) bgneal@232: bgneal@232: # we do this for the template since it is rendered twice bgneal@232: page_nav = render_to_string('forums/pagination.html', {'page': page}) bgneal@232: bgneal@1031: return render(request, 'forums/topic_list.html', { bgneal@232: 'title': 'Unanswered Topics', bgneal@232: 'page': page, bgneal@232: 'page_nav': page_nav, bgneal@374: 'unread': False, bgneal@1031: }) bgneal@232: bgneal@232: bgneal@409: def active_topics(request, num): bgneal@409: """Displays the last num topics that have been posted to.""" bgneal@409: bgneal@409: # sanity check num bgneal@409: num = min(50, max(10, int(num))) bgneal@409: bgneal@409: # MySQL didn't do this query very well unfortunately... bgneal@522: # bgneal@522: #public_forum_ids = Forum.objects.public_forum_ids() bgneal@409: #topics = Topic.objects.filter(forum__in=public_forum_ids).select_related( bgneal@409: # 'forum', 'user', 'last_post', 'last_post__user').order_by( bgneal@409: # '-update_date')[:num] bgneal@522: bgneal@522: # Save 1 query by using forums.latest to give us a list of the most recent bgneal@522: # topics; forums.latest doesn't save enough info to give us everything we bgneal@522: # need so we hit the database for the rest. bgneal@522: bgneal@522: topic_ids = get_latest_topic_ids(num) bgneal@409: topics = Topic.objects.filter(id__in=topic_ids).select_related( bgneal@409: 'forum', 'user', 'last_post', 'last_post__user').order_by( bgneal@522: '-update_date') bgneal@409: bgneal@409: paginator = create_topic_paginator(topics) bgneal@409: page_num = get_page_num(request) bgneal@409: try: bgneal@409: page = paginator.page(page_num) bgneal@409: except InvalidPage: bgneal@409: raise Http404 bgneal@409: bgneal@409: attach_topic_page_ranges(page.object_list) bgneal@409: bgneal@409: # we do this for the template since it is rendered twice bgneal@409: page_nav = render_to_string('forums/pagination.html', {'page': page}) bgneal@409: bgneal@409: title = 'Last %d Active Topics' % num bgneal@409: bgneal@1031: return render(request, 'forums/topic_list.html', { bgneal@409: 'title': title, bgneal@409: 'page': page, bgneal@409: 'page_nav': page_nav, bgneal@409: 'unread': False, bgneal@1031: }) bgneal@409: bgneal@409: bgneal@232: @login_required bgneal@232: def my_posts(request): bgneal@232: """Displays a list of posts the requesting user made.""" bgneal@232: return _user_posts(request, request.user, request.user, 'My Posts') bgneal@232: bgneal@232: bgneal@232: @login_required bgneal@232: def posts_for_user(request, username): bgneal@232: """Displays a list of posts by the given user. bgneal@232: Only the forums that the requesting user can see are examined. bgneal@232: """ bgneal@232: target_user = get_object_or_404(User, username=username) bgneal@232: return _user_posts(request, target_user, request.user, 'Posts by %s' % username) bgneal@232: bgneal@232: bgneal@232: @login_required bgneal@232: def post_ip_info(request, post_id): bgneal@232: """Displays information about the IP address the post was made from.""" bgneal@232: post = get_object_or_404(Post.objects.select_related(), pk=post_id) bgneal@232: bgneal@459: if not perms.can_moderate(post.topic.forum, request.user): bgneal@232: return HttpResponseForbidden("You don't have permission for this post.") bgneal@232: bgneal@232: ip_users = sorted(set(Post.objects.filter( bgneal@232: user_ip=post.user_ip).values_list('user__username', flat=True))) bgneal@232: bgneal@1031: return render(request, 'forums/post_ip.html', { bgneal@232: 'post': post, bgneal@232: 'ip_users': ip_users, bgneal@1031: }) bgneal@232: bgneal@232: bgneal@232: def _user_posts(request, target_user, req_user, page_title): bgneal@286: """Displays a list of posts made by the target user. bgneal@232: req_user is the user trying to view the posts. Only the forums bgneal@232: req_user can see are searched. bgneal@232: """ bgneal@232: forum_ids = Forum.objects.forum_ids_for_user(req_user) bgneal@232: posts = Post.objects.filter(user=target_user, bgneal@232: topic__forum__id__in=forum_ids).order_by( bgneal@232: '-creation_date').select_related() bgneal@232: bgneal@232: paginator = create_post_paginator(posts) bgneal@232: page_num = get_page_num(request) bgneal@232: try: bgneal@232: page = paginator.page(page_num) bgneal@232: except InvalidPage: bgneal@232: raise Http404 bgneal@232: bgneal@232: # we do this for the template since it is rendered twice bgneal@232: page_nav = render_to_string('forums/pagination.html', {'page': page}) bgneal@232: bgneal@1031: return render(request, 'forums/post_list.html', { bgneal@232: 'title': page_title, bgneal@232: 'page': page, bgneal@232: 'page_nav': page_nav, bgneal@1031: }) bgneal@232: bgneal@232: bgneal@232: def _bump_post_count(user): bgneal@232: """ bgneal@232: Increments the forum_post_count for the given user. bgneal@232: """ bgneal@789: profile = user.profile bgneal@232: profile.forum_post_count += 1 bgneal@562: profile.save(content_update=False) bgneal@232: bgneal@232: bgneal@232: def _move_topic(topic, old_forum, new_forum): bgneal@232: if new_forum != old_forum: bgneal@232: topic.forum = new_forum bgneal@232: topic.save() bgneal@232: # Have to adjust foreign keys to last_post, denormalized counts, etc.: bgneal@232: old_forum.sync() bgneal@232: old_forum.save() bgneal@232: new_forum.sync() bgneal@232: new_forum.save() bgneal@232: bgneal@232: bgneal@232: def _bulk_sticky(forum, topic_ids): bgneal@232: """ bgneal@232: Performs a toggle on the sticky status for a given list of topic ids. bgneal@232: """ bgneal@232: topics = Topic.objects.filter(pk__in=topic_ids) bgneal@232: for topic in topics: bgneal@232: if topic.forum == forum: bgneal@232: topic.sticky = not topic.sticky bgneal@232: topic.save() bgneal@232: bgneal@232: bgneal@232: def _bulk_lock(forum, topic_ids): bgneal@232: """ bgneal@232: Performs a toggle on the locked status for a given list of topic ids. bgneal@232: """ bgneal@232: topics = Topic.objects.filter(pk__in=topic_ids) bgneal@232: for topic in topics: bgneal@232: if topic.forum == forum: bgneal@232: topic.locked = not topic.locked bgneal@232: topic.save() bgneal@232: bgneal@232: bgneal@232: def _bulk_delete(forum, topic_ids): bgneal@232: """ bgneal@232: Deletes the list of topics. bgneal@232: """ bgneal@235: # Because we are deleting stuff, retrieve each topic one at a bgneal@235: # time since we are going to be adjusting de-normalized fields bgneal@235: # during deletes. In particular, we can't do this: bgneal@235: # topics = Topic.objects.filter(pk__in=topic_ids).select_related() bgneal@235: # for topic in topics: bgneal@235: # since topic.forum.last_post can go stale after a delete. bgneal@235: bgneal@235: for id in topic_ids: bgneal@235: try: bgneal@235: topic = Topic.objects.select_related().get(pk=id) bgneal@235: except Topic.DoesNotExist: bgneal@235: continue bgneal@235: _delete_topic(topic) bgneal@232: bgneal@232: bgneal@232: def _bulk_move(topic_ids, old_forum, new_forum): bgneal@232: """ bgneal@232: Moves the list of topics to a new forum. bgneal@232: """ bgneal@232: topics = Topic.objects.filter(pk__in=topic_ids).select_related() bgneal@232: for topic in topics: bgneal@232: if topic.forum == old_forum: bgneal@232: _move_topic(topic, old_forum, new_forum) bgneal@232: bgneal@232: bgneal@445: def _update_last_visit(user, topic, visit_time): bgneal@232: """ bgneal@232: Does the bookkeeping for the last visit status for the user to the bgneal@232: topic/forum. bgneal@232: """ bgneal@232: now = datetime.datetime.now() bgneal@232: try: bgneal@232: flv = ForumLastVisit.objects.get(user=user, forum=topic.forum) bgneal@232: except ForumLastVisit.DoesNotExist: bgneal@232: flv = ForumLastVisit(user=user, forum=topic.forum) bgneal@232: flv.begin_date = now bgneal@232: bgneal@232: flv.end_date = now bgneal@232: flv.save() bgneal@232: bgneal@232: if topic.update_date > flv.begin_date: bgneal@232: try: bgneal@232: tlv = TopicLastVisit.objects.get(user=user, topic=topic) bgneal@232: except TopicLastVisit.DoesNotExist: bgneal@445: tlv = TopicLastVisit(user=user, topic=topic, last_visit=datetime.datetime.min) bgneal@232: bgneal@445: if visit_time > tlv.last_visit: bgneal@445: tlv.last_visit = visit_time bgneal@445: tlv.save() bgneal@232: bgneal@232: bgneal@232: def _split_topic_at(topic, post_id, new_forum, new_name): bgneal@232: """ bgneal@232: This function splits the post given by post_id and all posts that come bgneal@232: after it in the given topic to a new topic in a new forum. bgneal@232: It is assumed the caller has been checked for moderator rights. bgneal@232: """ bgneal@232: post = get_object_or_404(Post, id=post_id) bgneal@232: if post.topic == topic: bgneal@232: post_ids = Post.objects.filter(topic=topic, bgneal@232: creation_date__gte=post.creation_date).values_list('id', flat=True) bgneal@232: _split_topic(topic, post_ids, new_forum, new_name) bgneal@232: bgneal@232: bgneal@232: def _split_topic(topic, post_ids, new_forum, new_name): bgneal@232: """ bgneal@232: This function splits the posts given by the post_ids list in the bgneal@232: given topic to a new topic in a new forum. bgneal@232: It is assumed the caller has been checked for moderator rights. bgneal@232: """ bgneal@232: posts = Post.objects.filter(topic=topic, id__in=post_ids) bgneal@232: if len(posts) > 0: bgneal@232: new_topic = Topic(forum=new_forum, name=new_name, user=posts[0].user) bgneal@232: new_topic.save() bgneal@469: notify_new_topic(new_topic) bgneal@232: for post in posts: bgneal@232: post.topic = new_topic bgneal@232: post.save() bgneal@286: bgneal@232: topic.post_count_update() bgneal@232: topic.save() bgneal@232: new_topic.post_count_update() bgneal@232: new_topic.save() bgneal@232: topic.forum.sync() bgneal@232: topic.forum.save() bgneal@232: new_forum.sync() bgneal@232: new_forum.save()