changeset 232:a46788862737

Implement a forum favorites feature for #82
author Brian Neal <bgneal@gmail.com>
date Sun, 01 Aug 2010 21:26:12 +0000
parents a2d388ed106e
children 6dde069debd4
files gpp/forums/admin.py gpp/forums/models.py gpp/forums/signals.py gpp/forums/spam.py gpp/forums/subscriptions.py gpp/forums/urls.py gpp/forums/views.py gpp/forums/views/__init__.py gpp/forums/views/favorites.py gpp/forums/views/main.py gpp/forums/views/spam.py gpp/forums/views/subscriptions.py gpp/templates/bio/view_profile.html gpp/templates/forums/favorite_status.html gpp/templates/forums/index.html gpp/templates/forums/manage_subscriptions.html gpp/templates/forums/manage_topics.html gpp/templates/forums/topic.html media/icons/add.png
diffstat 18 files changed, 1435 insertions(+), 1244 deletions(-) [+]
line wrap: on
line diff
--- a/gpp/forums/admin.py	Wed Jul 14 02:35:39 2010 +0000
+++ b/gpp/forums/admin.py	Sun Aug 01 21:26:12 2010 +0000
@@ -31,7 +31,7 @@
 class TopicAdmin(admin.ModelAdmin):
     list_display = ('name', 'forum', 'creation_date', 'update_date', 'user', 'sticky', 'locked',
             'post_count')
-    raw_id_fields = ('user', 'last_post', 'subscribers')
+    raw_id_fields = ('user', 'last_post', 'subscribers', 'bookmarkers')
     search_fields = ('name', )
     date_hierarchy = 'creation_date'
     list_filter = ('creation_date', 'update_date', )
--- a/gpp/forums/models.py	Wed Jul 14 02:35:39 2010 +0000
+++ b/gpp/forums/models.py	Sun Aug 01 21:26:12 2010 +0000
@@ -174,6 +174,8 @@
     locked = models.BooleanField(blank=True, default=False)
     subscribers = models.ManyToManyField(User, related_name='subscriptions',
             verbose_name='subscribers', blank=True)
+    bookmarkers = models.ManyToManyField(User, related_name='favorite_topics',
+            verbose_name='bookmarkers', blank=True)
 
     # denormalized fields to reduce database hits
     post_count = models.IntegerField(blank=True, default=0)
--- a/gpp/forums/signals.py	Wed Jul 14 02:35:39 2010 +0000
+++ b/gpp/forums/signals.py	Sun Aug 01 21:26:12 2010 +0000
@@ -5,7 +5,7 @@
 from django.db.models.signals import post_delete
 
 from forums.models import Topic, Post
-from forums.subscriptions import notify_topic_subscribers
+from forums.views.subscriptions import notify_topic_subscribers
 
 
 def on_topic_save(sender, **kwargs):
--- a/gpp/forums/spam.py	Wed Jul 14 02:35:39 2010 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,139 +0,0 @@
-"""
-This module contains views for dealing with spam and spammers.
-"""
-import datetime
-import logging
-import textwrap
-
-from django.contrib.auth.decorators import login_required
-from django.core.urlresolvers import reverse
-from django.http import HttpResponseRedirect
-from django.http import HttpResponseForbidden
-from django.shortcuts import get_object_or_404
-from django.shortcuts import render_to_response
-from django.template import RequestContext
-from django.contrib.auth.models import User
-
-from comments.models import Comment
-from forums.models import Post
-from forums.tools import delete_user_posts
-import bio.models
-from core.functions import email_admins
-
-
-SPAMMER_NAILED_SUBJECT = "Spammer Nailed: %s"
-SPAMMER_NAILED_MSG_BODY = """
-The admin/moderator user %s has just deactivated the account of %s for spam.
-"""
-
-def deactivate_spammer(user):
-    """This function deactivate's the user, marks them as a spammer, then
-    deletes the user's comments and forum posts.
-    """
-    user.is_active = False
-    user.save()
-
-    profile = user.get_profile()
-    profile.status = bio.models.STA_SPAMMER
-    profile.status_date = datetime.datetime.now()
-    profile.save()
-
-    Comment.objects.filter(user=user).delete()
-    delete_user_posts(user)
-
-
-def promote_stranger(user):
-    """This function upgrades the user from stranger status to a regular user.
-    """
-    profile = user.get_profile()
-    if user.is_active and profile.status == bio.models.STA_STRANGER:
-        profile.status = bio.models.STA_ACTIVE
-        profile.status_date = datetime.datetime.now()
-        profile.save()
-
-
-@login_required
-def spammer(request, post_id):
-    """This view allows moderators to deactivate spammer accounts."""
-
-    post = get_object_or_404(Post.objects.select_related(), pk=post_id)
-    poster = post.user
-    poster_profile = poster.get_profile()
-
-    can_moderate = request.user.is_superuser or (
-            request.user in post.topic.forum.moderators.all())
-
-    can_deactivate = (poster_profile.status == bio.models.STA_STRANGER and not
-            poster.is_superuser)
-
-    if request.method == "POST" and can_moderate and can_deactivate:
-        deactivate_spammer(poster)
-
-        email_admins(SPAMMER_NAILED_SUBJECT % poster.username, 
-            SPAMMER_NAILED_MSG_BODY % (
-                request.user.username, poster.username))
-
-        logging.info(textwrap.dedent("""\
-            SPAMMER DEACTIVATED: %s nailed %s for spam.
-            IP: %s
-            Message:
-            %s
-            """ % (request.user.username, poster.username, post.user_ip,
-                post.body)))
-
-        return HttpResponseRedirect(reverse('forums-spammer_nailed', args=[
-            poster.id]))
-
-    return render_to_response('forums/spammer.html', {
-        'can_moderate': can_moderate,
-        'can_deactivate': can_deactivate,
-        'post': post,
-        },
-        context_instance=RequestContext(request))
-
-
-@login_required
-def spammer_nailed(request, spammer_id):
-    """This view presents a confirmation screen that the spammer has been
-    deactivated.
-    """
-    user = get_object_or_404(User, pk=spammer_id)
-    profile = user.get_profile()
-
-    success = not user.is_active and profile.status == bio.models.STA_SPAMMER
-
-    return render_to_response('forums/spammer_nailed.html', {
-        'spammer': user,
-        'success': success,
-        },
-        context_instance=RequestContext(request))
-
-
-@login_required
-def stranger(request, post_id):
-    """This view allows a forum moderator or super user to promote a user from
-    stranger status to regular user.
-    """
-    post = get_object_or_404(Post.objects.select_related(), pk=post_id)
-    poster = post.user
-    poster_profile = poster.get_profile()
-
-    can_moderate = request.user.is_superuser or (
-            request.user in post.topic.forum.moderators.all())
-
-    can_promote = poster_profile.status == bio.models.STA_STRANGER
-
-    if request.method == "POST" and can_moderate and can_promote:
-        promote_stranger(poster)
-
-        logging.info("STRANGER PROMOTED: %s promoted %s." % (
-            request.user.username, poster.username))
-
-        return HttpResponseRedirect(post.get_absolute_url())
-
-    return render_to_response('forums/stranger.html', {
-        'can_moderate': can_moderate,
-        'can_promote': can_promote,
-        'post': post,
-        },
-        context_instance=RequestContext(request))
--- a/gpp/forums/subscriptions.py	Wed Jul 14 02:35:39 2010 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,114 +0,0 @@
-"""This module handles the subscriptions of users to forum topics."""
-from django.conf import settings
-from django.contrib.auth.decorators import login_required
-from django.contrib.sites.models import Site
-from django.core.paginator import InvalidPage
-from django.core.urlresolvers import reverse
-from django.http import HttpResponseRedirect
-from django.http import Http404
-from django.template.loader import render_to_string
-from django.shortcuts import get_object_or_404
-from django.shortcuts import render_to_response
-from django.template import RequestContext
-from django.views.decorators.http import require_POST
-
-from forums.models import Topic
-from core.functions import send_mail
-from core.paginator import DiggPaginator
-
-
-def notify_topic_subscribers(post):
-    """The argument post is a newly created post. Send out an email
-    notification to all subscribers of the post's parent Topic."""
-
-    topic = post.topic
-    recipients = topic.subscribers.exclude(
-            id=post.user.id).values_list('email', flat=True)
-
-    if recipients:
-        site = Site.objects.get_current()
-        subject = "[%s] Topic Reply: %s" % (site.name, topic.name)
-        url_prefix = "http://%s" % site.domain
-        post_url = url_prefix + post.get_absolute_url()
-        unsubscribe_url = url_prefix + reverse("forums-manage_subscriptions")
-        msg = render_to_string("forums/topic_notify_email.txt", {
-                'poster': post.user.username,
-                'topic_name': topic.name,
-                'message': post.body,
-                'post_url': post_url,
-                'unsubscribe_url': unsubscribe_url,
-                })
-        for recipient in recipients:
-            send_mail(subject, msg, settings.DEFAULT_FROM_EMAIL, [recipient])
-
-
-@login_required
-@require_POST
-def subscribe_topic(request, topic_id):
-    """Subscribe the user to the requested topic."""
-    topic = get_object_or_404(Topic.objects.select_related(), id=topic_id)
-    if topic.forum.category.can_access(request.user):
-        topic.subscribers.add(request.user)
-        return HttpResponseRedirect(
-            reverse("forums-subscription_status", args=[topic.id]))
-    raise Http404
-
-
-@login_required
-@require_POST
-def unsubscribe_topic(request, topic_id):
-    """Unsubscribe the user to the requested topic."""
-    topic = get_object_or_404(Topic, id=topic_id)
-    topic.subscribers.remove(request.user)
-    return HttpResponseRedirect(
-        reverse("forums-subscription_status", args=[topic.id]))
-
-
-@login_required
-def subscription_status(request, topic_id):
-    """Display the subscription status for the given topic."""
-    topic = get_object_or_404(Topic.objects.select_related(), id=topic_id)
-    is_subscribed = request.user in topic.subscribers.all()
-    return render_to_response('forums/subscription_status.html', {
-        'topic': topic,
-        'is_subscribed': is_subscribed,
-        },
-        context_instance=RequestContext(request))
-
-
-@login_required
-def manage_subscriptions(request):
-    """Display a user's topic subscriptions, and allow them to be deleted."""
-
-    user = request.user
-    if request.method == "POST":
-        if request.POST.get('delete_all'):
-            user.subscriptions.clear()
-        else:
-            delete_ids = request.POST.getlist('delete_ids')
-            try:
-                delete_ids = [int(id) for id in delete_ids]
-            except ValueError:
-                raise Http404
-            for topic in user.subscriptions.filter(id__in=delete_ids):
-                user.subscriptions.remove(topic)
-
-        page_num = request.POST.get('page', 1)
-    else:
-        page_num = request.GET.get('page', 1)
-
-    topics = user.subscriptions.select_related().order_by('-creation_date')
-    paginator = DiggPaginator(topics, 20, body=5, tail=2, margin=3, padding=2)
-    try:
-        page_num = int(page_num)
-    except ValueError:
-        page_num = 1
-    try:
-        page = paginator.page(page_num)
-    except InvalidPage:
-        raise Http404
-
-    return render_to_response('forums/manage_subscriptions.html', {
-        'page': page,
-        },
-        context_instance=RequestContext(request))
--- a/gpp/forums/urls.py	Wed Jul 14 02:35:39 2010 +0000
+++ b/gpp/forums/urls.py	Sun Aug 01 21:26:12 2010 +0000
@@ -3,7 +3,7 @@
 """
 from django.conf.urls.defaults import *
 
-urlpatterns = patterns('forums.views',
+urlpatterns = patterns('forums.views.main',
     url(r'^$', 'index', name='forums-index'),
     url(r'^new-topic-success/(?P<tid>\d+)$', 'new_topic_thanks', name='forums-new_topic_thanks'),
     url(r'^topic/(?P<id>\d+)/$', 'topic_index', name='forums-topic_index'),
@@ -29,14 +29,21 @@
     url(r'^unread/$', 'unread_topics', name='forums-unread_topics'),
 )
 
-urlpatterns += patterns('forums.subscriptions',
+urlpatterns += patterns('forums.views.favorites',
+    url(r'^favorite/(\d+)/$', 'favorite_topic', name='forums-favorite_topic'),
+    url(r'^favorites/$', 'manage_favorites', name='forums-manage_favorites'),
+    url(r'^favorites/(\d+)/$', 'favorites_status', name='forums-favorites_status'),
+    url(r'^unfavorite/(\d+)/$', 'unfavorite_topic', name='forums-unfavorite_topic'),
+)
+
+urlpatterns += patterns('forums.views.subscriptions',
     url(r'^subscribe/(\d+)/$', 'subscribe_topic', name='forums-subscribe_topic'),
     url(r'^subscriptions/$', 'manage_subscriptions', name='forums-manage_subscriptions'),
     url(r'^subscriptions/(\d+)/$', 'subscription_status', name='forums-subscription_status'),
     url(r'^unsubscribe/(\d+)/$', 'unsubscribe_topic', name='forums-unsubscribe_topic'),
 )
 
-urlpatterns += patterns('forums.spam',
+urlpatterns += patterns('forums.views.spam',
     url(r'^spammer/(\d+)/$', 'spammer', name='forums-spammer'),
     url(r'^spammer/nailed/(\d+)/$', 'spammer_nailed', name='forums-spammer_nailed'),
     url(r'^stranger/(\d+)/$', 'stranger', name='forums-stranger'),
--- a/gpp/forums/views.py	Wed Jul 14 02:35:39 2010 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,947 +0,0 @@
-"""
-Views for the forums application.
-"""
-import datetime
-
-from django.contrib.auth.decorators import login_required
-from django.contrib.auth.models import User
-from django.http import Http404
-from django.http import HttpResponse
-from django.http import HttpResponseBadRequest
-from django.http import HttpResponseForbidden
-from django.http import HttpResponseRedirect
-from django.core.urlresolvers import reverse
-from django.core.paginator import InvalidPage
-from django.shortcuts import get_object_or_404
-from django.shortcuts import render_to_response
-from django.shortcuts import redirect
-from django.template.loader import render_to_string
-from django.template import RequestContext
-from django.views.decorators.http import require_POST
-from django.utils.text import wrap
-from django.db.models import F
-
-from core.paginator import DiggPaginator
-from core.functions import email_admins
-from forums.models import Forum, Topic, Post, FlaggedPost, TopicLastVisit, \
-        ForumLastVisit
-from forums.forms import NewTopicForm, NewPostForm, PostForm, MoveTopicForm, \
-        SplitTopicForm
-from forums.unread import get_forum_unread_status, get_topic_unread_status, \
-        get_post_unread_status, get_unread_topics
-
-from bio.models import UserProfile
-import antispam
-import antispam.utils
-
-#######################################################################
-
-TOPICS_PER_PAGE = 50
-POSTS_PER_PAGE = 20
-
-
-def get_page_num(request):
-    """Returns the value of the 'page' variable in GET if it exists, or 1
-    if it does not."""
-
-    try:
-        page_num = int(request.GET.get('page', 1))
-    except ValueError:
-        page_num = 1
-
-    return page_num
-
-
-def create_topic_paginator(topics):
-   return DiggPaginator(topics, TOPICS_PER_PAGE, body=5, tail=2, margin=3, padding=2)
-
-def create_post_paginator(posts):
-   return DiggPaginator(posts, POSTS_PER_PAGE, body=5, tail=2, margin=3, padding=2)
-
-
-def attach_topic_page_ranges(topics):
-    """Attaches a page_range attribute to each topic in the supplied list.
-    This attribute will be None if it is a single page topic. This is used
-    by the templates to generate "goto page x" links.
-    """
-    for topic in topics:
-        if topic.post_count > POSTS_PER_PAGE:
-            pp = DiggPaginator(range(topic.post_count), POSTS_PER_PAGE, 
-                    body=2, tail=3, margin=1)
-            topic.page_range = pp.page(1).page_range
-        else:
-            topic.page_range = None
-
-#######################################################################
-
-def index(request):
-    """
-    This view displays all the forums available, ordered in each category.
-    """
-    # check for special forum queries
-    query = request.GET.get("query")
-    if query == "unread":
-        return redirect('forums-unread_topics')
-    elif query == "unanswered":
-        return redirect('forums-unanswered_topics')
-    elif query == "mine":
-        return redirect('forums-my_posts')
-
-    public_forums = Forum.objects.public_forums()
-    feeds = [{'name': 'All Forums', 'feed': '/feeds/forums/'}]
-
-    forums = Forum.objects.forums_for_user(request.user)
-    get_forum_unread_status(forums, request.user)
-    cats = {}
-    for forum in forums:
-        forum.has_feed = forum in public_forums
-        if forum.has_feed:
-            feeds.append({
-                'name': '%s Forum' % forum.name,
-                'feed': '/feeds/forums/%s/' % forum.slug,
-                })
-
-        cat = cats.setdefault(forum.category.id, {
-            'cat': forum.category,
-            'forums': [],
-            })
-        cat['forums'].append(forum)
-
-    cmpdef = lambda a, b: cmp(a['cat'].position, b['cat'].position)
-    cats = sorted(cats.values(), cmpdef)
-
-    return render_to_response('forums/index.html', {
-        'cats': cats,
-        'feeds': feeds,
-        },
-        context_instance=RequestContext(request))
-
-
-def forum_index(request, slug):
-    """
-    Displays all the topics in a forum.
-    """
-    forum = get_object_or_404(Forum.objects.select_related(), slug=slug)
-
-    if not forum.category.can_access(request.user):
-        return HttpResponseForbidden()
-
-    topics = forum.topics.select_related('user', 'last_post', 'last_post__user')
-    get_topic_unread_status(forum, topics, request.user)
-
-    paginator = create_topic_paginator(topics)
-    page_num = get_page_num(request)
-    try:
-        page = paginator.page(page_num)
-    except InvalidPage:
-        raise Http404
-
-    attach_topic_page_ranges(page.object_list)
-
-    # we do this for the template since it is rendered twice
-    page_nav = render_to_string('forums/pagination.html', {'page': page})
-
-    can_moderate = _can_moderate(forum, request.user)
-    
-    return render_to_response('forums/forum_index.html', {
-        'forum': forum,
-        'page': page,
-        'page_nav': page_nav,
-        'can_moderate': can_moderate,
-        },
-        context_instance=RequestContext(request))
-
-
-def topic_index(request, id):
-    """
-    Displays all the posts in a topic.
-    """
-    topic = get_object_or_404(Topic.objects.select_related(
-        'forum', 'forum__category', 'last_post'), pk=id)
-
-    if not topic.forum.category.can_access(request.user):
-        return HttpResponseForbidden()
-
-    topic.view_count = F('view_count') + 1
-    topic.save(force_update=True)
-
-    posts = topic.posts.select_related()
-
-    paginator = create_post_paginator(posts)
-    page_num = get_page_num(request)
-    try:
-        page = paginator.page(page_num)
-    except InvalidPage:
-        raise Http404
-    get_post_unread_status(topic, page.object_list, request.user)
-
-    # Attach user profiles to each post to avoid using get_user_profile() in
-    # the template.
-    users = set(post.user.id for post in page.object_list)
-
-    profiles = UserProfile.objects.filter(user__id__in=users).select_related()
-    user_profiles = dict((profile.user.id, profile) for profile in profiles)
-
-    for post in page.object_list:
-        post.user_profile = user_profiles[post.user.id]
-
-    last_page = page_num == paginator.num_pages
-
-    if request.user.is_authenticated() and last_page:
-        _update_last_visit(request.user, topic)
-
-    # we do this for the template since it is rendered twice
-    page_nav = render_to_string('forums/pagination.html', {'page': page})
-
-    can_moderate = _can_moderate(topic.forum, request.user)
-
-    can_reply = request.user.is_authenticated() and (
-        not topic.locked or can_moderate)
-
-    is_subscribed = request.user.is_authenticated() and (
-            topic in request.user.subscriptions.all())
-
-    return render_to_response('forums/topic.html', {
-        'forum': topic.forum,
-        'topic': topic,
-        'page': page,
-        'page_nav': page_nav,
-        'last_page': last_page,
-        'can_moderate': can_moderate,
-        'can_reply': can_reply,
-        'form': NewPostForm(initial={'topic_id': topic.id}),
-        'is_subscribed': is_subscribed,
-        },
-        context_instance=RequestContext(request))
-
-
-@login_required
-def new_topic(request, slug):
-    """
-    This view handles the creation of new topics.
-    """
-    forum = get_object_or_404(Forum.objects.select_related(), slug=slug)
-
-    if not forum.category.can_access(request.user):
-        return HttpResponseForbidden()
-
-    if request.method == 'POST':
-        form = NewTopicForm(request.user, forum, request.POST)
-        if form.is_valid():
-            if antispam.utils.spam_check(request, form.cleaned_data['body']):
-                return HttpResponseRedirect(reverse('antispam-suspended'))
-
-            topic = form.save(request.META.get("REMOTE_ADDR"))
-            _bump_post_count(request.user)
-            return HttpResponseRedirect(reverse('forums-new_topic_thanks',
-                                            kwargs={'tid': topic.pk}))
-    else:
-        form = NewTopicForm(request.user, forum)
-    
-    return render_to_response('forums/new_topic.html', {
-        'forum': forum,
-        'form': form,
-        },
-        context_instance=RequestContext(request))
-
-
-@login_required
-def new_topic_thanks(request, tid):
-    """
-    This view displays the success page for a newly created topic.
-    """
-    topic = get_object_or_404(Topic.objects.select_related(), pk=tid)
-    return render_to_response('forums/new_topic_thanks.html', {
-        'forum': topic.forum,
-        'topic': topic,
-        },
-        context_instance=RequestContext(request))
-
-
-@require_POST
-def quick_reply_ajax(request):
-    """
-    This function handles the quick reply to a thread function. This
-    function is meant to be the target of an AJAX post, and returns
-    the HTML for the new post, which the client-side script appends
-    to the document.
-    """
-    if not request.user.is_authenticated():
-        return HttpResponseForbidden('Please login or register to post.')
-
-    form = NewPostForm(request.POST)
-    if form.is_valid():
-        if not _can_post_in_topic(form.topic, request.user):
-            return HttpResponseForbidden("You don't have permission to post in this topic.")
-        if antispam.utils.spam_check(request, form.cleaned_data['body']):
-            return HttpResponseForbidden(antispam.BUSTED_MESSAGE)
-
-        post = form.save(request.user, request.META.get("REMOTE_ADDR", ""))
-        post.unread = True
-        post.user_profile = request.user.get_profile()
-        _bump_post_count(request.user)
-        _update_last_visit(request.user, form.topic)
-        return render_to_response('forums/display_post.html', {
-            'post': post,
-            'can_moderate': _can_moderate(form.topic.forum, request.user),
-            'can_reply': True,
-            },
-            context_instance=RequestContext(request))
-
-    return HttpResponseBadRequest("Invalid post.");
-
-
-def goto_post(request, post_id):
-    """
-    This function calculates what page a given post is on, then redirects
-    to that URL. This function is the target of get_absolute_url() for
-    Post objects.
-    """
-    post = get_object_or_404(Post.objects.select_related(), pk=post_id)
-    count = post.topic.posts.filter(creation_date__lt=post.creation_date).count()
-    page = count / POSTS_PER_PAGE + 1
-    url = reverse('forums-topic_index', kwargs={'id': post.topic.id}) + \
-        '?page=%s#p%s' % (page, post.id)
-    return HttpResponseRedirect(url)
-
-
-@require_POST
-def flag_post(request):
-    """
-    This function handles the flagging of posts by users. This function should
-    be the target of an AJAX post.
-    """
-    if not request.user.is_authenticated():
-        return HttpResponseForbidden('Please login or register to flag a post.')
-
-    id = request.POST.get('id')
-    if id is None:
-        return HttpResponseBadRequest('No post id')
-
-    try:
-        post = Post.objects.get(pk=id)
-    except Post.DoesNotExist:
-        return HttpResponseBadRequest('No post with id %s' % id)
-
-    flag = FlaggedPost(user=request.user, post=post)
-    flag.save()
-    email_admins('A Post Has Been Flagged', """Hello,
-
-A user has flagged a forum post for review.
-""")
-    return HttpResponse('The post was flagged. A moderator will review the post shortly. ' \
-            'Thanks for helping to improve the discussions on this site.')
-
-
-@login_required
-def edit_post(request, id):
-    """
-    This view function allows authorized users to edit posts.
-    The superuser, forum moderators, and original author can edit posts.
-    """
-    post = get_object_or_404(Post.objects.select_related(), pk=id)
-
-    can_moderate = _can_moderate(post.topic.forum, request.user)
-    can_edit = can_moderate or request.user == post.user
-
-    if not can_edit:
-        return HttpResponseForbidden("You don't have permission to edit that post.")
-
-    if request.method == "POST":
-        form = PostForm(request.POST, instance=post)
-        if form.is_valid():
-            if antispam.utils.spam_check(request, form.cleaned_data['body']):
-                return HttpResponseRedirect(reverse('antispam-suspended'))
-            post = form.save(commit=False)
-            post.touch()
-            post.save()
-            return HttpResponseRedirect(post.get_absolute_url())
-    else:
-        form = PostForm(instance=post)
-
-    post.user_profile = request.user.get_profile()
-
-    return render_to_response('forums/edit_post.html', {
-        'forum': post.topic.forum,
-        'topic': post.topic,
-        'post': post,
-        'form': form,
-        'can_moderate': can_moderate,
-        },
-        context_instance=RequestContext(request))
-
-
-@require_POST
-def delete_post(request):
-    """
-    This view function allows superusers and forum moderators to delete posts.
-    This function is the target of AJAX calls from the client.
-    """
-    if not request.user.is_authenticated():
-        return HttpResponseForbidden('Please login to delete a post.')
-
-    id = request.POST.get('id')
-    if id is None:
-        return HttpResponseBadRequest('No post id')
-
-    post = get_object_or_404(Post.objects.select_related(), pk=id)
-
-    can_delete = request.user.is_superuser or \
-            request.user in post.topic.forum.moderators.all()
-
-    if not can_delete:
-        return HttpResponseForbidden("You don't have permission to delete that post.")
-
-    delete_single_post(post)
-    return HttpResponse("The post has been deleted.")
-
-
-def delete_single_post(post):
-    """
-    This function deletes a single post. It handles the case of where
-    a post is the sole post in a topic by deleting the topic also. It
-    adjusts any foreign keys in Topic or Forum objects that might be pointing
-    to this post before deleting the post to avoid a cascading delete.
-    """
-    if post.topic.post_count == 1 and post == post.topic.last_post:
-        _delete_topic(post.topic)
-    else:
-        _delete_post(post)
-
-
-def _delete_post(post):
-    """
-    Internal function to delete a single post object.
-    Decrements the post author's post count.
-    Adjusts the parent topic and forum's last_post as needed.
-    """
-    # Adjust post creator's post count
-    profile = post.user.get_profile()
-    if profile.forum_post_count > 0:
-        profile.forum_post_count -= 1
-        profile.save()
-
-    # If this post is the last_post in a topic, we need to update
-    # both the topic and parent forum's last post fields. If we don't
-    # the cascading delete will delete them also!
-
-    topic = post.topic
-    if topic.last_post == post:
-        topic.last_post_pre_delete()
-        topic.save()
-
-    forum = topic.forum
-    if forum.last_post == post:
-        forum.last_post_pre_delete()
-        forum.save()
-
-    # Should be safe to delete the post now:
-    post.delete()
-
-
-def _delete_topic(topic):
-    """
-    Internal function to delete an entire topic.
-    Deletes the topic and all posts contained within.
-    Adjusts the parent forum's last_post as needed.
-    Note that we don't bother adjusting all the users'
-    post counts as that doesn't seem to be worth the effort.
-    """
-    if topic.forum.last_post and topic.forum.last_post.topic == topic:
-        topic.forum.last_post_pre_delete()
-        topic.forum.save()
-
-    # delete subscriptions to this topic
-    topic.subscribers.clear()
-
-    # It should be safe to just delete the topic now. This will
-    # automatically delete all posts in the topic.
-    topic.delete()
-
-
-@login_required
-def new_post(request, topic_id):
-    """
-    This function is the view for creating a normal, non-quick reply
-    to a topic.
-    """
-    topic = get_object_or_404(Topic.objects.select_related(), pk=topic_id)
-    can_post = _can_post_in_topic(topic, request.user)
-
-    if can_post:
-        if request.method == 'POST':
-            form = PostForm(request.POST)
-            if form.is_valid():
-                if antispam.utils.spam_check(request, form.cleaned_data['body']):
-                    return HttpResponseRedirect(reverse('antispam-suspended'))
-                post = form.save(commit=False)
-                post.topic = topic
-                post.user = request.user
-                post.user_ip = request.META.get("REMOTE_ADDR", "")
-                post.save()
-                _bump_post_count(request.user)
-                _update_last_visit(request.user, topic)
-                return HttpResponseRedirect(post.get_absolute_url())
-        else:
-            quote_id = request.GET.get('quote')
-            if quote_id:
-                quote_post = get_object_or_404(Post.objects.select_related(),
-                        pk=quote_id)
-                form = PostForm(initial={'body': _quote_message(quote_post.user.username,
-                    quote_post.body)})
-            else:
-                form = PostForm()
-    else:
-        form = None
-
-    return render_to_response('forums/new_post.html', {
-        'forum': topic.forum,
-        'topic': topic,
-        'form': form,
-        'can_post': can_post,
-        },
-        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()
-
-
-@login_required
-def mod_topic_move(request, id):
-    """
-    This view function is for moderators to move a topic to a different forum.
-    """
-    topic = get_object_or_404(Topic.objects.select_related(), pk=id)
-    if not _can_moderate(topic.forum, request.user):
-        return HttpResponseForbidden()
-
-    if request.method == 'POST':
-        form = MoveTopicForm(request.user, request.POST)
-        if form.is_valid():
-            new_forum = form.cleaned_data['forums']
-            old_forum = topic.forum
-            _move_topic(topic, old_forum, new_forum)
-            return HttpResponseRedirect(topic.get_absolute_url())
-    else:
-        form = MoveTopicForm(request.user)
-
-    return render_to_response('forums/move_topic.html', {
-        'forum': topic.forum,
-        'topic': topic,
-        'form': form,
-        },
-        context_instance=RequestContext(request))
-
-
-@login_required
-def mod_forum(request, slug):
-    """
-    Displays a view to allow moderators to perform various operations
-    on topics in a forum in bulk. We currently support mass locking/unlocking,
-    stickying and unstickying, moving, and deleting topics.
-    """
-    forum = get_object_or_404(Forum.objects.select_related(), slug=slug)
-    if not _can_moderate(forum, request.user):
-        return HttpResponseForbidden()
-
-    topics = forum.topics.select_related('user', 'last_post', 'last_post__user')
-    paginator = create_topic_paginator(topics)
-    page_num = get_page_num(request)
-    try:
-        page = paginator.page(page_num)
-    except InvalidPage:
-        raise Http404
-
-    # we do this for the template since it is rendered twice
-    page_nav = render_to_string('forums/pagination.html', {'page': page})
-    form = None
-
-    if request.method == 'POST':
-        topic_ids = request.POST.getlist('topic_ids')
-        url = reverse('forums-mod_forum', kwargs={'slug':forum.slug})
-        url += '?page=%s' % page_num
-
-        if len(topic_ids):
-            if request.POST.get('sticky'):
-                _bulk_sticky(forum, topic_ids)
-                return HttpResponseRedirect(url)
-            elif request.POST.get('lock'):
-                _bulk_lock(forum, topic_ids)
-                return HttpResponseRedirect(url)
-            elif request.POST.get('delete'):
-                _bulk_delete(forum, topic_ids)
-                return HttpResponseRedirect(url)
-            elif request.POST.get('move'):
-                form = MoveTopicForm(request.user, request.POST, hide_label=True)
-                if form.is_valid():
-                    _bulk_move(topic_ids, forum, form.cleaned_data['forums'])
-                    return HttpResponseRedirect(url)
-    
-    if form is None:
-        form = MoveTopicForm(request.user, hide_label=True)
-
-    return render_to_response('forums/mod_forum.html', {
-        'forum': forum,
-        'page': page,
-        'page_nav': page_nav,
-        'form': form,
-        },
-        context_instance=RequestContext(request))
-
-
-@login_required
-@require_POST
-def forum_catchup(request, slug):
-    """
-    This view marks all the topics in the forum as being read.
-    """
-    forum = get_object_or_404(Forum.objects.select_related(), slug=slug)
-
-    if not forum.category.can_access(request.user):
-        return HttpResponseForbidden()
-
-    forum.catchup(request.user)
-    return HttpResponseRedirect(forum.get_absolute_url())
-
-
-@login_required
-def mod_topic_split(request, id):
-    """
-    This view function allows moderators to split posts off to a new topic.
-    """
-    topic = get_object_or_404(Topic.objects.select_related(), pk=id)
-    if not _can_moderate(topic.forum, request.user):
-        return HttpResponseRedirect(topic.get_absolute_url())
-
-    if request.method == "POST":
-        form = SplitTopicForm(request.user, request.POST)
-        if form.is_valid():
-            if form.split_at:
-                _split_topic_at(topic, form.post_ids[0],
-                        form.cleaned_data['forums'],
-                        form.cleaned_data['name'])
-            else:
-                _split_topic(topic, form.post_ids,
-                        form.cleaned_data['forums'],
-                        form.cleaned_data['name'])
-
-            return HttpResponseRedirect(topic.get_absolute_url())
-    else:
-        form = SplitTopicForm(request.user)
-
-    posts = topic.posts.select_related()
-
-    return render_to_response('forums/mod_split_topic.html', {
-        'forum': topic.forum,
-        'topic': topic,
-        'posts': posts,
-        'form': form,
-        },
-        context_instance=RequestContext(request))
-
-
-@login_required
-def unread_topics(request):
-    """Displays the topics with unread posts for a given user."""
-
-    topics = get_unread_topics(request.user)
-
-    paginator = create_topic_paginator(topics)
-    page_num = get_page_num(request)
-    try:
-        page = paginator.page(page_num)
-    except InvalidPage:
-        raise Http404
-
-    attach_topic_page_ranges(page.object_list)
-
-    # we do this for the template since it is rendered twice
-    page_nav = render_to_string('forums/pagination.html', {'page': page})
-
-    return render_to_response('forums/topic_list.html', {
-        'title': 'Topics With Unread Posts',
-        'page': page,
-        'page_nav': page_nav,
-        },
-        context_instance=RequestContext(request))
-
-
-def unanswered_topics(request):
-    """Displays the topics with no replies."""
-
-    forum_ids = Forum.objects.forum_ids_for_user(request.user)
-    topics = Topic.objects.filter(forum__id__in=forum_ids,
-            post_count=1).select_related(
-                    'forum', 'user', 'last_post', 'last_post__user')
-
-    paginator = create_topic_paginator(topics)
-    page_num = get_page_num(request)
-    try:
-        page = paginator.page(page_num)
-    except InvalidPage:
-        raise Http404
-
-    attach_topic_page_ranges(page.object_list)
-
-    # we do this for the template since it is rendered twice
-    page_nav = render_to_string('forums/pagination.html', {'page': page})
-
-    return render_to_response('forums/topic_list.html', {
-        'title': 'Unanswered Topics',
-        'page': page,
-        'page_nav': page_nav,
-        },
-        context_instance=RequestContext(request))
-
-
-@login_required
-def my_posts(request):
-    """Displays a list of posts the requesting user made."""
-    return _user_posts(request, request.user, request.user, 'My Posts')
-
-
-@login_required
-def posts_for_user(request, username):
-    """Displays a list of posts by the given user.
-    Only the forums that the requesting user can see are examined.
-    """
-    target_user = get_object_or_404(User, username=username)
-    return _user_posts(request, target_user, request.user, 'Posts by %s' % username)
-
-
-@login_required
-def post_ip_info(request, post_id):
-    """Displays information about the IP address the post was made from."""
-    post = get_object_or_404(Post.objects.select_related(), pk=post_id)
-
-    if not _can_moderate(post.topic.forum, request.user):
-        return HttpResponseForbidden("You don't have permission for this post.")
-
-    ip_users = sorted(set(Post.objects.filter(
-        user_ip=post.user_ip).values_list('user__username', flat=True)))
-
-    return render_to_response('forums/post_ip.html', {
-        'post': post,
-        'ip_users': ip_users,
-        },
-        context_instance=RequestContext(request))
-
-
-def _user_posts(request, target_user, req_user, page_title):
-    """Displays a list of posts made by the target user. 
-    req_user is the user trying to view the posts. Only the forums
-    req_user can see are searched.
-    """
-    forum_ids = Forum.objects.forum_ids_for_user(req_user)
-    posts = Post.objects.filter(user=target_user,
-            topic__forum__id__in=forum_ids).order_by(
-                    '-creation_date').select_related()
-
-    paginator = create_post_paginator(posts)
-    page_num = get_page_num(request)
-    try:
-        page = paginator.page(page_num)
-    except InvalidPage:
-        raise Http404
-
-    # we do this for the template since it is rendered twice
-    page_nav = render_to_string('forums/pagination.html', {'page': page})
-
-    return render_to_response('forums/post_list.html', {
-        'title': page_title,
-        'page': page,
-        'page_nav': page_nav,
-        },
-        context_instance=RequestContext(request))
-
-
-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
-    and false otherwise.
-    """
-    return (not topic.locked and topic.forum.category.can_access(user)) or \
-            (user.is_superuser or user in topic.forum.moderators.all())
-
-
-def _bump_post_count(user):
-    """
-    Increments the forum_post_count for the given user.
-    """
-    profile = user.get_profile()
-    profile.forum_post_count += 1
-    profile.save()
-
-
-def _quote_message(who, message):
-    """
-    Builds a message reply by quoting the existing message in a
-    typical email-like fashion. The quoting is compatible with Markdown.
-    """
-    header = '*%s wrote:*\n\n' % (who, )
-    lines = wrap(message, 55).split('\n')
-    for i, line in enumerate(lines):
-        lines[i] = '> ' + line
-    return header + '\n'.join(lines)
-
-
-def _move_topic(topic, old_forum, new_forum):
-    if new_forum != old_forum:
-        topic.forum = new_forum
-        topic.save()
-        # Have to adjust foreign keys to last_post, denormalized counts, etc.:
-        old_forum.sync()
-        old_forum.save()
-        new_forum.sync()
-        new_forum.save()
-
-
-def _bulk_sticky(forum, topic_ids):
-    """
-    Performs a toggle on the sticky status for a given list of topic ids.
-    """
-    topics = Topic.objects.filter(pk__in=topic_ids)
-    for topic in topics:
-        if topic.forum == forum:
-            topic.sticky = not topic.sticky
-            topic.save()
-
-
-def _bulk_lock(forum, topic_ids):
-    """
-    Performs a toggle on the locked status for a given list of topic ids.
-    """
-    topics = Topic.objects.filter(pk__in=topic_ids)
-    for topic in topics:
-        if topic.forum == forum:
-            topic.locked = not topic.locked
-            topic.save()
-
-
-def _bulk_delete(forum, topic_ids):
-    """
-    Deletes the list of topics.
-    """
-    topics = Topic.objects.filter(pk__in=topic_ids).select_related()
-    for topic in topics:
-        if topic.forum == forum:
-            _delete_topic(topic)
-
-
-def _bulk_move(topic_ids, old_forum, new_forum):
-    """
-    Moves the list of topics to a new forum.
-    """
-    topics = Topic.objects.filter(pk__in=topic_ids).select_related()
-    for topic in topics:
-        if topic.forum == old_forum:
-            _move_topic(topic, old_forum, new_forum)
-
-
-def _update_last_visit(user, topic):
-    """
-    Does the bookkeeping for the last visit status for the user to the
-    topic/forum.
-    """
-    now = datetime.datetime.now()
-    try:
-        flv = ForumLastVisit.objects.get(user=user, forum=topic.forum)
-    except ForumLastVisit.DoesNotExist:
-        flv = ForumLastVisit(user=user, forum=topic.forum)
-        flv.begin_date = now
-
-    flv.end_date = now
-    flv.save()
-
-    if topic.update_date > flv.begin_date:
-        try:
-            tlv = TopicLastVisit.objects.get(user=user, topic=topic)
-        except TopicLastVisit.DoesNotExist:
-            tlv = TopicLastVisit(user=user, topic=topic)
-
-        tlv.touch()
-        tlv.save()
-
-
-def _split_topic_at(topic, post_id, new_forum, new_name):
-    """
-    This function splits the post given by post_id and all posts that come
-    after it in the given topic to a new topic in a new forum.
-    It is assumed the caller has been checked for moderator rights.
-    """
-    post = get_object_or_404(Post, id=post_id)
-    if post.topic == topic:
-        post_ids = Post.objects.filter(topic=topic,
-                creation_date__gte=post.creation_date).values_list('id', flat=True)
-        _split_topic(topic, post_ids, new_forum, new_name)
-
-
-def _split_topic(topic, post_ids, new_forum, new_name):
-    """
-    This function splits the posts given by the post_ids list in the
-    given topic to a new topic in a new forum.
-    It is assumed the caller has been checked for moderator rights.
-    """
-    posts = Post.objects.filter(topic=topic, id__in=post_ids)
-    if len(posts) > 0:
-        new_topic = Topic(forum=new_forum, name=new_name, user=posts[0].user)
-        new_topic.save()
-        for post in posts:
-            post.topic = new_topic
-            post.save()
-        
-        topic.post_count_update()
-        topic.save()
-        new_topic.post_count_update()
-        new_topic.save()
-        topic.forum.sync()
-        topic.forum.save()
-        new_forum.sync()
-        new_forum.save()
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/gpp/forums/views/favorites.py	Sun Aug 01 21:26:12 2010 +0000
@@ -0,0 +1,92 @@
+"""
+This module contains view functions related to forum favorites (bookmarks).
+"""
+from django.contrib.auth.decorators import login_required
+from django.core.urlresolvers import reverse
+from django.views.decorators.http import require_POST
+from django.shortcuts import get_object_or_404
+from django.shortcuts import render_to_response
+from django.template import RequestContext
+from django.http import HttpResponseRedirect
+from django.http import HttpResponseForbidden
+from django.http import Http404
+
+from core.paginator import DiggPaginator
+from forums.models import Topic
+
+
+@login_required
+@require_POST
+def favorite_topic(request, topic_id):
+    """
+    This function handles the "favoriting" (bookmarking) of a forum topic by a
+    user.
+    """
+    topic = get_object_or_404(Topic.objects.select_related(), id=topic_id)
+    if topic.forum.category.can_access(request.user):
+        topic.bookmarkers.add(request.user)
+        return HttpResponseRedirect(
+            reverse("forums-favorites_status", args=[topic.id]))
+    raise Http404   # TODO return HttpResponseForbidden instead
+
+
+@login_required
+def manage_favorites(request):
+    """Display a user's favorite topics and allow them to be deleted."""
+
+    user = request.user
+    if request.method == "POST":
+        if request.POST.get('delete_all'):
+            user.favorite_topics.clear()
+        else:
+            delete_ids = request.POST.getlist('delete_ids')
+            try:
+                delete_ids = [int(id) for id in delete_ids]
+            except ValueError:
+                raise Http404
+            for topic in user.favorite_topics.filter(id__in=delete_ids):
+                user.favorite_topics.remove(topic)
+
+        page_num = request.POST.get('page', 1)
+    else:
+        page_num = request.GET.get('page', 1)
+
+    topics = user.favorite_topics.select_related().order_by('-update_date')
+    paginator = DiggPaginator(topics, 20, body=5, tail=2, margin=3, padding=2)
+    try:
+        page_num = int(page_num)
+    except ValueError:
+        page_num = 1
+    try:
+        page = paginator.page(page_num)
+    except InvalidPage:
+        raise Http404
+
+    return render_to_response('forums/manage_topics.html', {
+        'page_title': 'Favorite Topics',
+        'description': 'Your favorite topics are listed below.',
+        'page': page,
+        },
+        context_instance=RequestContext(request))
+
+@login_required
+def favorites_status(request, topic_id):
+    """Display the favorite status for the given topic."""
+    topic = get_object_or_404(Topic.objects.select_related(), id=topic_id)
+    is_favorite = request.user in topic.bookmarkers.all()
+    return render_to_response('forums/favorite_status.html', {
+        'topic': topic,
+        'is_favorite': is_favorite,
+        },
+        context_instance=RequestContext(request))
+
+@login_required
+@require_POST
+def unfavorite_topic(request, topic_id):
+    """
+    Un-favorite the user from the requested topic.
+    """
+    topic = get_object_or_404(Topic, id=topic_id)
+    topic.bookmarkers.remove(request.user)
+    return HttpResponseRedirect(
+        reverse("forums-favorites_status", args=[topic.id]))
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/gpp/forums/views/main.py	Sun Aug 01 21:26:12 2010 +0000
@@ -0,0 +1,956 @@
+"""
+Views for the forums application.
+"""
+import datetime
+
+from django.contrib.auth.decorators import login_required
+from django.contrib.auth.models import User
+from django.http import Http404
+from django.http import HttpResponse
+from django.http import HttpResponseBadRequest
+from django.http import HttpResponseForbidden
+from django.http import HttpResponseRedirect
+from django.core.urlresolvers import reverse
+from django.core.paginator import InvalidPage
+from django.shortcuts import get_object_or_404
+from django.shortcuts import render_to_response
+from django.shortcuts import redirect
+from django.template.loader import render_to_string
+from django.template import RequestContext
+from django.views.decorators.http import require_POST
+from django.utils.text import wrap
+from django.db.models import F
+
+from core.paginator import DiggPaginator
+from core.functions import email_admins
+from forums.models import Forum, Topic, Post, FlaggedPost, TopicLastVisit, \
+        ForumLastVisit
+from forums.forms import NewTopicForm, NewPostForm, PostForm, MoveTopicForm, \
+        SplitTopicForm
+from forums.unread import get_forum_unread_status, get_topic_unread_status, \
+        get_post_unread_status, get_unread_topics
+
+from bio.models import UserProfile
+import antispam
+import antispam.utils
+
+#######################################################################
+
+TOPICS_PER_PAGE = 50
+POSTS_PER_PAGE = 20
+
+
+def get_page_num(request):
+    """Returns the value of the 'page' variable in GET if it exists, or 1
+    if it does not."""
+
+    try:
+        page_num = int(request.GET.get('page', 1))
+    except ValueError:
+        page_num = 1
+
+    return page_num
+
+
+def create_topic_paginator(topics):
+   return DiggPaginator(topics, TOPICS_PER_PAGE, body=5, tail=2, margin=3, padding=2)
+
+def create_post_paginator(posts):
+   return DiggPaginator(posts, POSTS_PER_PAGE, body=5, tail=2, margin=3, padding=2)
+
+
+def attach_topic_page_ranges(topics):
+    """Attaches a page_range attribute to each topic in the supplied list.
+    This attribute will be None if it is a single page topic. This is used
+    by the templates to generate "goto page x" links.
+    """
+    for topic in topics:
+        if topic.post_count > POSTS_PER_PAGE:
+            pp = DiggPaginator(range(topic.post_count), POSTS_PER_PAGE, 
+                    body=2, tail=3, margin=1)
+            topic.page_range = pp.page(1).page_range
+        else:
+            topic.page_range = None
+
+#######################################################################
+
+SPECIAL_QUERIES = {
+    'unread': 'forums-unread_topics',
+    'unanswered': 'forums-unanswered_topics',
+    'mine': 'forums-my_posts',
+    'favorites': 'forums-manage_favorites',
+    'subscriptions': 'forums-manage_subscriptions',
+}
+
+def index(request):
+    """
+    This view displays all the forums available, ordered in each category.
+    """
+    # check for special forum queries
+    query = request.GET.get("query")
+    if query in SPECIAL_QUERIES:
+        return redirect(SPECIAL_QUERIES[query])
+
+    public_forums = Forum.objects.public_forums()
+    feeds = [{'name': 'All Forums', 'feed': '/feeds/forums/'}]
+
+    forums = Forum.objects.forums_for_user(request.user)
+    get_forum_unread_status(forums, request.user)
+    cats = {}
+    for forum in forums:
+        forum.has_feed = forum in public_forums
+        if forum.has_feed:
+            feeds.append({
+                'name': '%s Forum' % forum.name,
+                'feed': '/feeds/forums/%s/' % forum.slug,
+                })
+
+        cat = cats.setdefault(forum.category.id, {
+            'cat': forum.category,
+            'forums': [],
+            })
+        cat['forums'].append(forum)
+
+    cmpdef = lambda a, b: cmp(a['cat'].position, b['cat'].position)
+    cats = sorted(cats.values(), cmpdef)
+
+    return render_to_response('forums/index.html', {
+        'cats': cats,
+        'feeds': feeds,
+        },
+        context_instance=RequestContext(request))
+
+
+def forum_index(request, slug):
+    """
+    Displays all the topics in a forum.
+    """
+    forum = get_object_or_404(Forum.objects.select_related(), slug=slug)
+
+    if not forum.category.can_access(request.user):
+        return HttpResponseForbidden()
+
+    topics = forum.topics.select_related('user', 'last_post', 'last_post__user')
+    get_topic_unread_status(forum, topics, request.user)
+
+    paginator = create_topic_paginator(topics)
+    page_num = get_page_num(request)
+    try:
+        page = paginator.page(page_num)
+    except InvalidPage:
+        raise Http404
+
+    attach_topic_page_ranges(page.object_list)
+
+    # we do this for the template since it is rendered twice
+    page_nav = render_to_string('forums/pagination.html', {'page': page})
+
+    can_moderate = _can_moderate(forum, request.user)
+    
+    return render_to_response('forums/forum_index.html', {
+        'forum': forum,
+        'page': page,
+        'page_nav': page_nav,
+        'can_moderate': can_moderate,
+        },
+        context_instance=RequestContext(request))
+
+
+def topic_index(request, id):
+    """
+    Displays all the posts in a topic.
+    """
+    topic = get_object_or_404(Topic.objects.select_related(
+        'forum', 'forum__category', 'last_post'), pk=id)
+
+    if not topic.forum.category.can_access(request.user):
+        return HttpResponseForbidden()
+
+    topic.view_count = F('view_count') + 1
+    topic.save(force_update=True)
+
+    posts = topic.posts.select_related()
+
+    paginator = create_post_paginator(posts)
+    page_num = get_page_num(request)
+    try:
+        page = paginator.page(page_num)
+    except InvalidPage:
+        raise Http404
+    get_post_unread_status(topic, page.object_list, request.user)
+
+    # Attach user profiles to each post to avoid using get_user_profile() in
+    # the template.
+    users = set(post.user.id for post in page.object_list)
+
+    profiles = UserProfile.objects.filter(user__id__in=users).select_related()
+    user_profiles = dict((profile.user.id, profile) for profile in profiles)
+
+    for post in page.object_list:
+        post.user_profile = user_profiles[post.user.id]
+
+    last_page = page_num == paginator.num_pages
+
+    if request.user.is_authenticated() and last_page:
+        _update_last_visit(request.user, topic)
+
+    # we do this for the template since it is rendered twice
+    page_nav = render_to_string('forums/pagination.html', {'page': page})
+
+    can_moderate = _can_moderate(topic.forum, request.user)
+
+    can_reply = request.user.is_authenticated() and (
+        not topic.locked or can_moderate)
+
+    is_favorite = request.user.is_authenticated() and (
+            topic in request.user.favorite_topics.all())
+
+    is_subscribed = request.user.is_authenticated() and (
+            topic in request.user.subscriptions.all())
+
+    return render_to_response('forums/topic.html', {
+        'forum': topic.forum,
+        'topic': topic,
+        'page': page,
+        'page_nav': page_nav,
+        'last_page': last_page,
+        'can_moderate': can_moderate,
+        'can_reply': can_reply,
+        'form': NewPostForm(initial={'topic_id': topic.id}),
+        'is_favorite': is_favorite,
+        'is_subscribed': is_subscribed,
+        },
+        context_instance=RequestContext(request))
+
+
+@login_required
+def new_topic(request, slug):
+    """
+    This view handles the creation of new topics.
+    """
+    forum = get_object_or_404(Forum.objects.select_related(), slug=slug)
+
+    if not forum.category.can_access(request.user):
+        return HttpResponseForbidden()
+
+    if request.method == 'POST':
+        form = NewTopicForm(request.user, forum, request.POST)
+        if form.is_valid():
+            if antispam.utils.spam_check(request, form.cleaned_data['body']):
+                return HttpResponseRedirect(reverse('antispam-suspended'))
+
+            topic = form.save(request.META.get("REMOTE_ADDR"))
+            _bump_post_count(request.user)
+            return HttpResponseRedirect(reverse('forums-new_topic_thanks',
+                                            kwargs={'tid': topic.pk}))
+    else:
+        form = NewTopicForm(request.user, forum)
+    
+    return render_to_response('forums/new_topic.html', {
+        'forum': forum,
+        'form': form,
+        },
+        context_instance=RequestContext(request))
+
+
+@login_required
+def new_topic_thanks(request, tid):
+    """
+    This view displays the success page for a newly created topic.
+    """
+    topic = get_object_or_404(Topic.objects.select_related(), pk=tid)
+    return render_to_response('forums/new_topic_thanks.html', {
+        'forum': topic.forum,
+        'topic': topic,
+        },
+        context_instance=RequestContext(request))
+
+
+@require_POST
+def quick_reply_ajax(request):
+    """
+    This function handles the quick reply to a thread function. This
+    function is meant to be the target of an AJAX post, and returns
+    the HTML for the new post, which the client-side script appends
+    to the document.
+    """
+    if not request.user.is_authenticated():
+        return HttpResponseForbidden('Please login or register to post.')
+
+    form = NewPostForm(request.POST)
+    if form.is_valid():
+        if not _can_post_in_topic(form.topic, request.user):
+            return HttpResponseForbidden("You don't have permission to post in this topic.")
+        if antispam.utils.spam_check(request, form.cleaned_data['body']):
+            return HttpResponseForbidden(antispam.BUSTED_MESSAGE)
+
+        post = form.save(request.user, request.META.get("REMOTE_ADDR", ""))
+        post.unread = True
+        post.user_profile = request.user.get_profile()
+        _bump_post_count(request.user)
+        _update_last_visit(request.user, form.topic)
+        return render_to_response('forums/display_post.html', {
+            'post': post,
+            'can_moderate': _can_moderate(form.topic.forum, request.user),
+            'can_reply': True,
+            },
+            context_instance=RequestContext(request))
+
+    return HttpResponseBadRequest("Invalid post.");
+
+
+def goto_post(request, post_id):
+    """
+    This function calculates what page a given post is on, then redirects
+    to that URL. This function is the target of get_absolute_url() for
+    Post objects.
+    """
+    post = get_object_or_404(Post.objects.select_related(), pk=post_id)
+    count = post.topic.posts.filter(creation_date__lt=post.creation_date).count()
+    page = count / POSTS_PER_PAGE + 1
+    url = reverse('forums-topic_index', kwargs={'id': post.topic.id}) + \
+        '?page=%s#p%s' % (page, post.id)
+    return HttpResponseRedirect(url)
+
+
+@require_POST
+def flag_post(request):
+    """
+    This function handles the flagging of posts by users. This function should
+    be the target of an AJAX post.
+    """
+    if not request.user.is_authenticated():
+        return HttpResponseForbidden('Please login or register to flag a post.')
+
+    id = request.POST.get('id')
+    if id is None:
+        return HttpResponseBadRequest('No post id')
+
+    try:
+        post = Post.objects.get(pk=id)
+    except Post.DoesNotExist:
+        return HttpResponseBadRequest('No post with id %s' % id)
+
+    flag = FlaggedPost(user=request.user, post=post)
+    flag.save()
+    email_admins('A Post Has Been Flagged', """Hello,
+
+A user has flagged a forum post for review.
+""")
+    return HttpResponse('The post was flagged. A moderator will review the post shortly. ' \
+            'Thanks for helping to improve the discussions on this site.')
+
+
+@login_required
+def edit_post(request, id):
+    """
+    This view function allows authorized users to edit posts.
+    The superuser, forum moderators, and original author can edit posts.
+    """
+    post = get_object_or_404(Post.objects.select_related(), pk=id)
+
+    can_moderate = _can_moderate(post.topic.forum, request.user)
+    can_edit = can_moderate or request.user == post.user
+
+    if not can_edit:
+        return HttpResponseForbidden("You don't have permission to edit that post.")
+
+    if request.method == "POST":
+        form = PostForm(request.POST, instance=post)
+        if form.is_valid():
+            if antispam.utils.spam_check(request, form.cleaned_data['body']):
+                return HttpResponseRedirect(reverse('antispam-suspended'))
+            post = form.save(commit=False)
+            post.touch()
+            post.save()
+            return HttpResponseRedirect(post.get_absolute_url())
+    else:
+        form = PostForm(instance=post)
+
+    post.user_profile = request.user.get_profile()
+
+    return render_to_response('forums/edit_post.html', {
+        'forum': post.topic.forum,
+        'topic': post.topic,
+        'post': post,
+        'form': form,
+        'can_moderate': can_moderate,
+        },
+        context_instance=RequestContext(request))
+
+
+@require_POST
+def delete_post(request):
+    """
+    This view function allows superusers and forum moderators to delete posts.
+    This function is the target of AJAX calls from the client.
+    """
+    if not request.user.is_authenticated():
+        return HttpResponseForbidden('Please login to delete a post.')
+
+    id = request.POST.get('id')
+    if id is None:
+        return HttpResponseBadRequest('No post id')
+
+    post = get_object_or_404(Post.objects.select_related(), pk=id)
+
+    can_delete = request.user.is_superuser or \
+            request.user in post.topic.forum.moderators.all()
+
+    if not can_delete:
+        return HttpResponseForbidden("You don't have permission to delete that post.")
+
+    delete_single_post(post)
+    return HttpResponse("The post has been deleted.")
+
+
+def delete_single_post(post):
+    """
+    This function deletes a single post. It handles the case of where
+    a post is the sole post in a topic by deleting the topic also. It
+    adjusts any foreign keys in Topic or Forum objects that might be pointing
+    to this post before deleting the post to avoid a cascading delete.
+    """
+    if post.topic.post_count == 1 and post == post.topic.last_post:
+        _delete_topic(post.topic)
+    else:
+        _delete_post(post)
+
+
+def _delete_post(post):
+    """
+    Internal function to delete a single post object.
+    Decrements the post author's post count.
+    Adjusts the parent topic and forum's last_post as needed.
+    """
+    # Adjust post creator's post count
+    profile = post.user.get_profile()
+    if profile.forum_post_count > 0:
+        profile.forum_post_count -= 1
+        profile.save()
+
+    # If this post is the last_post in a topic, we need to update
+    # both the topic and parent forum's last post fields. If we don't
+    # the cascading delete will delete them also!
+
+    topic = post.topic
+    if topic.last_post == post:
+        topic.last_post_pre_delete()
+        topic.save()
+
+    forum = topic.forum
+    if forum.last_post == post:
+        forum.last_post_pre_delete()
+        forum.save()
+
+    # Should be safe to delete the post now:
+    post.delete()
+
+
+def _delete_topic(topic):
+    """
+    Internal function to delete an entire topic.
+    Deletes the topic and all posts contained within.
+    Adjusts the parent forum's last_post as needed.
+    Note that we don't bother adjusting all the users'
+    post counts as that doesn't seem to be worth the effort.
+    """
+    if topic.forum.last_post and topic.forum.last_post.topic == topic:
+        topic.forum.last_post_pre_delete()
+        topic.forum.save()
+
+    # delete subscriptions to this topic
+    topic.subscribers.clear()
+    topic.bookmarkers.clear()
+
+    # It should be safe to just delete the topic now. This will
+    # automatically delete all posts in the topic.
+    topic.delete()
+
+
+@login_required
+def new_post(request, topic_id):
+    """
+    This function is the view for creating a normal, non-quick reply
+    to a topic.
+    """
+    topic = get_object_or_404(Topic.objects.select_related(), pk=topic_id)
+    can_post = _can_post_in_topic(topic, request.user)
+
+    if can_post:
+        if request.method == 'POST':
+            form = PostForm(request.POST)
+            if form.is_valid():
+                if antispam.utils.spam_check(request, form.cleaned_data['body']):
+                    return HttpResponseRedirect(reverse('antispam-suspended'))
+                post = form.save(commit=False)
+                post.topic = topic
+                post.user = request.user
+                post.user_ip = request.META.get("REMOTE_ADDR", "")
+                post.save()
+                _bump_post_count(request.user)
+                _update_last_visit(request.user, topic)
+                return HttpResponseRedirect(post.get_absolute_url())
+        else:
+            quote_id = request.GET.get('quote')
+            if quote_id:
+                quote_post = get_object_or_404(Post.objects.select_related(),
+                        pk=quote_id)
+                form = PostForm(initial={'body': _quote_message(quote_post.user.username,
+                    quote_post.body)})
+            else:
+                form = PostForm()
+    else:
+        form = None
+
+    return render_to_response('forums/new_post.html', {
+        'forum': topic.forum,
+        'topic': topic,
+        'form': form,
+        'can_post': can_post,
+        },
+        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()
+
+
+@login_required
+def mod_topic_move(request, id):
+    """
+    This view function is for moderators to move a topic to a different forum.
+    """
+    topic = get_object_or_404(Topic.objects.select_related(), pk=id)
+    if not _can_moderate(topic.forum, request.user):
+        return HttpResponseForbidden()
+
+    if request.method == 'POST':
+        form = MoveTopicForm(request.user, request.POST)
+        if form.is_valid():
+            new_forum = form.cleaned_data['forums']
+            old_forum = topic.forum
+            _move_topic(topic, old_forum, new_forum)
+            return HttpResponseRedirect(topic.get_absolute_url())
+    else:
+        form = MoveTopicForm(request.user)
+
+    return render_to_response('forums/move_topic.html', {
+        'forum': topic.forum,
+        'topic': topic,
+        'form': form,
+        },
+        context_instance=RequestContext(request))
+
+
+@login_required
+def mod_forum(request, slug):
+    """
+    Displays a view to allow moderators to perform various operations
+    on topics in a forum in bulk. We currently support mass locking/unlocking,
+    stickying and unstickying, moving, and deleting topics.
+    """
+    forum = get_object_or_404(Forum.objects.select_related(), slug=slug)
+    if not _can_moderate(forum, request.user):
+        return HttpResponseForbidden()
+
+    topics = forum.topics.select_related('user', 'last_post', 'last_post__user')
+    paginator = create_topic_paginator(topics)
+    page_num = get_page_num(request)
+    try:
+        page = paginator.page(page_num)
+    except InvalidPage:
+        raise Http404
+
+    # we do this for the template since it is rendered twice
+    page_nav = render_to_string('forums/pagination.html', {'page': page})
+    form = None
+
+    if request.method == 'POST':
+        topic_ids = request.POST.getlist('topic_ids')
+        url = reverse('forums-mod_forum', kwargs={'slug':forum.slug})
+        url += '?page=%s' % page_num
+
+        if len(topic_ids):
+            if request.POST.get('sticky'):
+                _bulk_sticky(forum, topic_ids)
+                return HttpResponseRedirect(url)
+            elif request.POST.get('lock'):
+                _bulk_lock(forum, topic_ids)
+                return HttpResponseRedirect(url)
+            elif request.POST.get('delete'):
+                _bulk_delete(forum, topic_ids)
+                return HttpResponseRedirect(url)
+            elif request.POST.get('move'):
+                form = MoveTopicForm(request.user, request.POST, hide_label=True)
+                if form.is_valid():
+                    _bulk_move(topic_ids, forum, form.cleaned_data['forums'])
+                    return HttpResponseRedirect(url)
+    
+    if form is None:
+        form = MoveTopicForm(request.user, hide_label=True)
+
+    return render_to_response('forums/mod_forum.html', {
+        'forum': forum,
+        'page': page,
+        'page_nav': page_nav,
+        'form': form,
+        },
+        context_instance=RequestContext(request))
+
+
+@login_required
+@require_POST
+def forum_catchup(request, slug):
+    """
+    This view marks all the topics in the forum as being read.
+    """
+    forum = get_object_or_404(Forum.objects.select_related(), slug=slug)
+
+    if not forum.category.can_access(request.user):
+        return HttpResponseForbidden()
+
+    forum.catchup(request.user)
+    return HttpResponseRedirect(forum.get_absolute_url())
+
+
+@login_required
+def mod_topic_split(request, id):
+    """
+    This view function allows moderators to split posts off to a new topic.
+    """
+    topic = get_object_or_404(Topic.objects.select_related(), pk=id)
+    if not _can_moderate(topic.forum, request.user):
+        return HttpResponseRedirect(topic.get_absolute_url())
+
+    if request.method == "POST":
+        form = SplitTopicForm(request.user, request.POST)
+        if form.is_valid():
+            if form.split_at:
+                _split_topic_at(topic, form.post_ids[0],
+                        form.cleaned_data['forums'],
+                        form.cleaned_data['name'])
+            else:
+                _split_topic(topic, form.post_ids,
+                        form.cleaned_data['forums'],
+                        form.cleaned_data['name'])
+
+            return HttpResponseRedirect(topic.get_absolute_url())
+    else:
+        form = SplitTopicForm(request.user)
+
+    posts = topic.posts.select_related()
+
+    return render_to_response('forums/mod_split_topic.html', {
+        'forum': topic.forum,
+        'topic': topic,
+        'posts': posts,
+        'form': form,
+        },
+        context_instance=RequestContext(request))
+
+
+@login_required
+def unread_topics(request):
+    """Displays the topics with unread posts for a given user."""
+
+    topics = get_unread_topics(request.user)
+
+    paginator = create_topic_paginator(topics)
+    page_num = get_page_num(request)
+    try:
+        page = paginator.page(page_num)
+    except InvalidPage:
+        raise Http404
+
+    attach_topic_page_ranges(page.object_list)
+
+    # we do this for the template since it is rendered twice
+    page_nav = render_to_string('forums/pagination.html', {'page': page})
+
+    return render_to_response('forums/topic_list.html', {
+        'title': 'Topics With Unread Posts',
+        'page': page,
+        'page_nav': page_nav,
+        },
+        context_instance=RequestContext(request))
+
+
+def unanswered_topics(request):
+    """Displays the topics with no replies."""
+
+    forum_ids = Forum.objects.forum_ids_for_user(request.user)
+    topics = Topic.objects.filter(forum__id__in=forum_ids,
+            post_count=1).select_related(
+                    'forum', 'user', 'last_post', 'last_post__user')
+
+    paginator = create_topic_paginator(topics)
+    page_num = get_page_num(request)
+    try:
+        page = paginator.page(page_num)
+    except InvalidPage:
+        raise Http404
+
+    attach_topic_page_ranges(page.object_list)
+
+    # we do this for the template since it is rendered twice
+    page_nav = render_to_string('forums/pagination.html', {'page': page})
+
+    return render_to_response('forums/topic_list.html', {
+        'title': 'Unanswered Topics',
+        'page': page,
+        'page_nav': page_nav,
+        },
+        context_instance=RequestContext(request))
+
+
+@login_required
+def my_posts(request):
+    """Displays a list of posts the requesting user made."""
+    return _user_posts(request, request.user, request.user, 'My Posts')
+
+
+@login_required
+def posts_for_user(request, username):
+    """Displays a list of posts by the given user.
+    Only the forums that the requesting user can see are examined.
+    """
+    target_user = get_object_or_404(User, username=username)
+    return _user_posts(request, target_user, request.user, 'Posts by %s' % username)
+
+
+@login_required
+def post_ip_info(request, post_id):
+    """Displays information about the IP address the post was made from."""
+    post = get_object_or_404(Post.objects.select_related(), pk=post_id)
+
+    if not _can_moderate(post.topic.forum, request.user):
+        return HttpResponseForbidden("You don't have permission for this post.")
+
+    ip_users = sorted(set(Post.objects.filter(
+        user_ip=post.user_ip).values_list('user__username', flat=True)))
+
+    return render_to_response('forums/post_ip.html', {
+        'post': post,
+        'ip_users': ip_users,
+        },
+        context_instance=RequestContext(request))
+
+
+def _user_posts(request, target_user, req_user, page_title):
+    """Displays a list of posts made by the target user. 
+    req_user is the user trying to view the posts. Only the forums
+    req_user can see are searched.
+    """
+    forum_ids = Forum.objects.forum_ids_for_user(req_user)
+    posts = Post.objects.filter(user=target_user,
+            topic__forum__id__in=forum_ids).order_by(
+                    '-creation_date').select_related()
+
+    paginator = create_post_paginator(posts)
+    page_num = get_page_num(request)
+    try:
+        page = paginator.page(page_num)
+    except InvalidPage:
+        raise Http404
+
+    # we do this for the template since it is rendered twice
+    page_nav = render_to_string('forums/pagination.html', {'page': page})
+
+    return render_to_response('forums/post_list.html', {
+        'title': page_title,
+        'page': page,
+        'page_nav': page_nav,
+        },
+        context_instance=RequestContext(request))
+
+
+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
+    and false otherwise.
+    """
+    return (not topic.locked and topic.forum.category.can_access(user)) or \
+            (user.is_superuser or user in topic.forum.moderators.all())
+
+
+def _bump_post_count(user):
+    """
+    Increments the forum_post_count for the given user.
+    """
+    profile = user.get_profile()
+    profile.forum_post_count += 1
+    profile.save()
+
+
+def _quote_message(who, message):
+    """
+    Builds a message reply by quoting the existing message in a
+    typical email-like fashion. The quoting is compatible with Markdown.
+    """
+    header = '*%s wrote:*\n\n' % (who, )
+    lines = wrap(message, 55).split('\n')
+    for i, line in enumerate(lines):
+        lines[i] = '> ' + line
+    return header + '\n'.join(lines)
+
+
+def _move_topic(topic, old_forum, new_forum):
+    if new_forum != old_forum:
+        topic.forum = new_forum
+        topic.save()
+        # Have to adjust foreign keys to last_post, denormalized counts, etc.:
+        old_forum.sync()
+        old_forum.save()
+        new_forum.sync()
+        new_forum.save()
+
+
+def _bulk_sticky(forum, topic_ids):
+    """
+    Performs a toggle on the sticky status for a given list of topic ids.
+    """
+    topics = Topic.objects.filter(pk__in=topic_ids)
+    for topic in topics:
+        if topic.forum == forum:
+            topic.sticky = not topic.sticky
+            topic.save()
+
+
+def _bulk_lock(forum, topic_ids):
+    """
+    Performs a toggle on the locked status for a given list of topic ids.
+    """
+    topics = Topic.objects.filter(pk__in=topic_ids)
+    for topic in topics:
+        if topic.forum == forum:
+            topic.locked = not topic.locked
+            topic.save()
+
+
+def _bulk_delete(forum, topic_ids):
+    """
+    Deletes the list of topics.
+    """
+    topics = Topic.objects.filter(pk__in=topic_ids).select_related()
+    for topic in topics:
+        if topic.forum == forum:
+            _delete_topic(topic)
+
+
+def _bulk_move(topic_ids, old_forum, new_forum):
+    """
+    Moves the list of topics to a new forum.
+    """
+    topics = Topic.objects.filter(pk__in=topic_ids).select_related()
+    for topic in topics:
+        if topic.forum == old_forum:
+            _move_topic(topic, old_forum, new_forum)
+
+
+def _update_last_visit(user, topic):
+    """
+    Does the bookkeeping for the last visit status for the user to the
+    topic/forum.
+    """
+    now = datetime.datetime.now()
+    try:
+        flv = ForumLastVisit.objects.get(user=user, forum=topic.forum)
+    except ForumLastVisit.DoesNotExist:
+        flv = ForumLastVisit(user=user, forum=topic.forum)
+        flv.begin_date = now
+
+    flv.end_date = now
+    flv.save()
+
+    if topic.update_date > flv.begin_date:
+        try:
+            tlv = TopicLastVisit.objects.get(user=user, topic=topic)
+        except TopicLastVisit.DoesNotExist:
+            tlv = TopicLastVisit(user=user, topic=topic)
+
+        tlv.touch()
+        tlv.save()
+
+
+def _split_topic_at(topic, post_id, new_forum, new_name):
+    """
+    This function splits the post given by post_id and all posts that come
+    after it in the given topic to a new topic in a new forum.
+    It is assumed the caller has been checked for moderator rights.
+    """
+    post = get_object_or_404(Post, id=post_id)
+    if post.topic == topic:
+        post_ids = Post.objects.filter(topic=topic,
+                creation_date__gte=post.creation_date).values_list('id', flat=True)
+        _split_topic(topic, post_ids, new_forum, new_name)
+
+
+def _split_topic(topic, post_ids, new_forum, new_name):
+    """
+    This function splits the posts given by the post_ids list in the
+    given topic to a new topic in a new forum.
+    It is assumed the caller has been checked for moderator rights.
+    """
+    posts = Post.objects.filter(topic=topic, id__in=post_ids)
+    if len(posts) > 0:
+        new_topic = Topic(forum=new_forum, name=new_name, user=posts[0].user)
+        new_topic.save()
+        for post in posts:
+            post.topic = new_topic
+            post.save()
+        
+        topic.post_count_update()
+        topic.save()
+        new_topic.post_count_update()
+        new_topic.save()
+        topic.forum.sync()
+        topic.forum.save()
+        new_forum.sync()
+        new_forum.save()
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/gpp/forums/views/spam.py	Sun Aug 01 21:26:12 2010 +0000
@@ -0,0 +1,139 @@
+"""
+This module contains views for dealing with spam and spammers.
+"""
+import datetime
+import logging
+import textwrap
+
+from django.contrib.auth.decorators import login_required
+from django.core.urlresolvers import reverse
+from django.http import HttpResponseRedirect
+from django.http import HttpResponseForbidden
+from django.shortcuts import get_object_or_404
+from django.shortcuts import render_to_response
+from django.template import RequestContext
+from django.contrib.auth.models import User
+
+from comments.models import Comment
+from forums.models import Post
+from forums.tools import delete_user_posts
+import bio.models
+from core.functions import email_admins
+
+
+SPAMMER_NAILED_SUBJECT = "Spammer Nailed: %s"
+SPAMMER_NAILED_MSG_BODY = """
+The admin/moderator user %s has just deactivated the account of %s for spam.
+"""
+
+def deactivate_spammer(user):
+    """This function deactivate's the user, marks them as a spammer, then
+    deletes the user's comments and forum posts.
+    """
+    user.is_active = False
+    user.save()
+
+    profile = user.get_profile()
+    profile.status = bio.models.STA_SPAMMER
+    profile.status_date = datetime.datetime.now()
+    profile.save()
+
+    Comment.objects.filter(user=user).delete()
+    delete_user_posts(user)
+
+
+def promote_stranger(user):
+    """This function upgrades the user from stranger status to a regular user.
+    """
+    profile = user.get_profile()
+    if user.is_active and profile.status == bio.models.STA_STRANGER:
+        profile.status = bio.models.STA_ACTIVE
+        profile.status_date = datetime.datetime.now()
+        profile.save()
+
+
+@login_required
+def spammer(request, post_id):
+    """This view allows moderators to deactivate spammer accounts."""
+
+    post = get_object_or_404(Post.objects.select_related(), pk=post_id)
+    poster = post.user
+    poster_profile = poster.get_profile()
+
+    can_moderate = request.user.is_superuser or (
+            request.user in post.topic.forum.moderators.all())
+
+    can_deactivate = (poster_profile.status == bio.models.STA_STRANGER and not
+            poster.is_superuser)
+
+    if request.method == "POST" and can_moderate and can_deactivate:
+        deactivate_spammer(poster)
+
+        email_admins(SPAMMER_NAILED_SUBJECT % poster.username, 
+            SPAMMER_NAILED_MSG_BODY % (
+                request.user.username, poster.username))
+
+        logging.info(textwrap.dedent("""\
+            SPAMMER DEACTIVATED: %s nailed %s for spam.
+            IP: %s
+            Message:
+            %s
+            """ % (request.user.username, poster.username, post.user_ip,
+                post.body)))
+
+        return HttpResponseRedirect(reverse('forums-spammer_nailed', args=[
+            poster.id]))
+
+    return render_to_response('forums/spammer.html', {
+        'can_moderate': can_moderate,
+        'can_deactivate': can_deactivate,
+        'post': post,
+        },
+        context_instance=RequestContext(request))
+
+
+@login_required
+def spammer_nailed(request, spammer_id):
+    """This view presents a confirmation screen that the spammer has been
+    deactivated.
+    """
+    user = get_object_or_404(User, pk=spammer_id)
+    profile = user.get_profile()
+
+    success = not user.is_active and profile.status == bio.models.STA_SPAMMER
+
+    return render_to_response('forums/spammer_nailed.html', {
+        'spammer': user,
+        'success': success,
+        },
+        context_instance=RequestContext(request))
+
+
+@login_required
+def stranger(request, post_id):
+    """This view allows a forum moderator or super user to promote a user from
+    stranger status to regular user.
+    """
+    post = get_object_or_404(Post.objects.select_related(), pk=post_id)
+    poster = post.user
+    poster_profile = poster.get_profile()
+
+    can_moderate = request.user.is_superuser or (
+            request.user in post.topic.forum.moderators.all())
+
+    can_promote = poster_profile.status == bio.models.STA_STRANGER
+
+    if request.method == "POST" and can_moderate and can_promote:
+        promote_stranger(poster)
+
+        logging.info("STRANGER PROMOTED: %s promoted %s." % (
+            request.user.username, poster.username))
+
+        return HttpResponseRedirect(post.get_absolute_url())
+
+    return render_to_response('forums/stranger.html', {
+        'can_moderate': can_moderate,
+        'can_promote': can_promote,
+        'post': post,
+        },
+        context_instance=RequestContext(request))
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/gpp/forums/views/subscriptions.py	Sun Aug 01 21:26:12 2010 +0000
@@ -0,0 +1,116 @@
+"""This module handles the subscriptions of users to forum topics."""
+from django.conf import settings
+from django.contrib.auth.decorators import login_required
+from django.contrib.sites.models import Site
+from django.core.paginator import InvalidPage
+from django.core.urlresolvers import reverse
+from django.http import HttpResponseRedirect
+from django.http import Http404
+from django.template.loader import render_to_string
+from django.shortcuts import get_object_or_404
+from django.shortcuts import render_to_response
+from django.template import RequestContext
+from django.views.decorators.http import require_POST
+
+from forums.models import Topic
+from core.functions import send_mail
+from core.paginator import DiggPaginator
+
+
+def notify_topic_subscribers(post):
+    """The argument post is a newly created post. Send out an email
+    notification to all subscribers of the post's parent Topic."""
+
+    topic = post.topic
+    recipients = topic.subscribers.exclude(
+            id=post.user.id).values_list('email', flat=True)
+
+    if recipients:
+        site = Site.objects.get_current()
+        subject = "[%s] Topic Reply: %s" % (site.name, topic.name)
+        url_prefix = "http://%s" % site.domain
+        post_url = url_prefix + post.get_absolute_url()
+        unsubscribe_url = url_prefix + reverse("forums-manage_subscriptions")
+        msg = render_to_string("forums/topic_notify_email.txt", {
+                'poster': post.user.username,
+                'topic_name': topic.name,
+                'message': post.body,
+                'post_url': post_url,
+                'unsubscribe_url': unsubscribe_url,
+                })
+        for recipient in recipients:
+            send_mail(subject, msg, settings.DEFAULT_FROM_EMAIL, [recipient])
+
+
+@login_required
+@require_POST
+def subscribe_topic(request, topic_id):
+    """Subscribe the user to the requested topic."""
+    topic = get_object_or_404(Topic.objects.select_related(), id=topic_id)
+    if topic.forum.category.can_access(request.user):
+        topic.subscribers.add(request.user)
+        return HttpResponseRedirect(
+            reverse("forums-subscription_status", args=[topic.id]))
+    raise Http404   # TODO return HttpResponseForbidden instead
+
+
+@login_required
+@require_POST
+def unsubscribe_topic(request, topic_id):
+    """Unsubscribe the user to the requested topic."""
+    topic = get_object_or_404(Topic, id=topic_id)
+    topic.subscribers.remove(request.user)
+    return HttpResponseRedirect(
+        reverse("forums-subscription_status", args=[topic.id]))
+
+
+@login_required
+def subscription_status(request, topic_id):
+    """Display the subscription status for the given topic."""
+    topic = get_object_or_404(Topic.objects.select_related(), id=topic_id)
+    is_subscribed = request.user in topic.subscribers.all()
+    return render_to_response('forums/subscription_status.html', {
+        'topic': topic,
+        'is_subscribed': is_subscribed,
+        },
+        context_instance=RequestContext(request))
+
+
+@login_required
+def manage_subscriptions(request):
+    """Display a user's topic subscriptions, and allow them to be deleted."""
+
+    user = request.user
+    if request.method == "POST":
+        if request.POST.get('delete_all'):
+            user.subscriptions.clear()
+        else:
+            delete_ids = request.POST.getlist('delete_ids')
+            try:
+                delete_ids = [int(id) for id in delete_ids]
+            except ValueError:
+                raise Http404
+            for topic in user.subscriptions.filter(id__in=delete_ids):
+                user.subscriptions.remove(topic)
+
+        page_num = request.POST.get('page', 1)
+    else:
+        page_num = request.GET.get('page', 1)
+
+    topics = user.subscriptions.select_related().order_by('-update_date')
+    paginator = DiggPaginator(topics, 20, body=5, tail=2, margin=3, padding=2)
+    try:
+        page_num = int(page_num)
+    except ValueError:
+        page_num = 1
+    try:
+        page = paginator.page(page_num)
+    except InvalidPage:
+        raise Http404
+
+    return render_to_response('forums/manage_topics.html', {
+        'page_title': 'Topic Subscriptions',
+        'description': 'The forum topics you are currently subscribed to are listed below.',
+        'page': page,
+        },
+        context_instance=RequestContext(request))
--- a/gpp/templates/bio/view_profile.html	Wed Jul 14 02:35:39 2010 +0000
+++ b/gpp/templates/bio/view_profile.html	Sun Aug 01 21:26:12 2010 +0000
@@ -22,7 +22,16 @@
     {% endfor %}
    </ul>
    {% endif %}
+   {% if this_is_me %}
+   <p>{% avatar subject %}
+   <ul>
+      <li><a href="">My favorite forum topics</a></li>
+      <li><a href="">My forum topic subscriptions</a></li>
+   </ul>
+   </p>
+   {% else %}
    <p>{% avatar subject %}</p>
+   {% endif %}
 <table id="bio_profile">
    <tr><th>Full Name</th><td>{{ subject.get_full_name }}</td></tr>
    <tr><th>Date Joined</th><td>{{ subject.date_joined|date:"F d, Y" }}</td></tr>
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/gpp/templates/forums/favorite_status.html	Sun Aug 01 21:26:12 2010 +0000
@@ -0,0 +1,23 @@
+{% extends 'base.html' %}
+{% block title %}Forums: Favorite Topics{% endblock %}
+{% block content %}
+<h2>Forums: Favorite Topics</h2>
+<h3>
+   <a href="{% url forums-index %}">SurfGuitar101 Forum Index</a> &raquo;
+   <a href="{% url forums-forum_index slug=topic.forum.slug %}">{{ topic.forum.name }}</a> &raquo;
+   <a href="{% url forums-topic_index id=topic.id %}">{{ topic.name }}</a>
+</h3>
+<p>
+{% if is_favorite %}
+The forum topic <a href="{{ topic.get_absolute_url }}">{{ topic.name }}</a> has been added to your 
+<a href="{% url forums-manage_favorites %}">favorites</a>.
+{% else %}
+The forum topic <a href="{{ topic.get_absolute_url }}">{{ topic.name }}</a> has been removed from your
+<a href="{% url forums-manage_favorites %}">favorites</a>.
+{% endif %}
+</p>
+<p>
+To manage all your forum topic favorites, please visit your 
+<a href="{% url forums-manage_favorites %}">favorites page</a>.
+</p>
+{% endblock %}
--- a/gpp/templates/forums/index.html	Wed Jul 14 02:35:39 2010 +0000
+++ b/gpp/templates/forums/index.html	Sun Aug 01 21:26:12 2010 +0000
@@ -16,6 +16,8 @@
       <option value="unread">Show topics with unread posts</option>
       <option value="mine">Show my posts</option>
       <option value="unanswered">Show unanswered posts</option>
+      <option value="favorites">Show my favorite topics</option>
+      <option value="subscriptions">Show my subscribed topics</option>
    </select><input type="submit" value="Go" />
 </form>
 
--- a/gpp/templates/forums/manage_subscriptions.html	Wed Jul 14 02:35:39 2010 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,39 +0,0 @@
-{% extends 'base.html' %}
-{% block title %}Forums: Topic Subscriptions{% endblock %}
-{% block content %}
-<h2>Forums: Topic Subscriptions</h2>
-
-<h3>
-   <a href="{% url forums-index %}">SurfGuitar101 Forum Index</a> &raquo; Topic Subscriptions
-</h3>
-<p>The forum topics you are currently subscribed to are listed below.</p>
-{% include 'forums/pagination.html' %}
-<form action="." method="post">{% csrf_token %}
-<table class="forum-topic-table">
-   <thead>
-      <tr>
-         <th>Forum</th>
-         <th>Topic</th>
-         <th>Select</th>
-      </tr>
-   </thead>
-   <tbody>
-      {% for topic in page.object_list %}
-         <tr>
-            <td><a href="{{ topic.forum.get_absolute_url }}">{{ topic.forum.name }}</a></td>
-            <td><a href="{{ topic.get_absolute_url }}">{{ topic.name }}</a></td>
-            <td><input type="checkbox" name="delete_ids" value="{{ topic.id }}" /></td>
-         </tr>
-      {% empty %}
-         <tr><td colspan="3"><em>No Topic Subscriptions</em></td></tr>
-      {% endfor %}
-   </tbody>
-</table>
-{% include 'forums/pagination.html' %}
-{% if page.object_list %}
-<input type="hidden" name="page" value="{{ page.number }}" />
-<input type="submit" name="delete_selected" value="Delete Selected" /> &bull;
-<input type="submit" name="delete_all" value="Delete All" />
-{% endif %}
-</form>
-{% endblock %}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/gpp/templates/forums/manage_topics.html	Sun Aug 01 21:26:12 2010 +0000
@@ -0,0 +1,63 @@
+{% extends 'base.html' %}
+{% block title %}Forums: {{ page_title }}{% endblock %}
+{% block custom_js %}
+<script type="text/javascript">
+//<![CDATA[
+$(document).ready(function() {
+   $('#master_check').click(function() {
+      var master_checked = this.checked;
+      $('.topic_box').each(function(index) {
+         this.checked = master_checked;
+      });
+   });
+   $('#topic_form').submit(function() {
+      var checked = false;
+      $('.topic_box').each(function(index) {
+         checked = checked || this.checked;
+      });
+      if (!checked) {
+         alert("Please select some topics to remove.");
+         return false;
+      }
+      return confirm("Are you sure you wish to remove the selected topics from your list?");
+   });
+});
+//]]>
+</script>
+{% endblock %}
+{% block content %}
+<h2>Forums: {{ page_title }}</h2>
+
+<h3>
+   <a href="{% url forums-index %}">SurfGuitar101 Forum Index</a> &raquo; {{ page_title }}
+</h3>
+<p>{{ description }}</p>
+{% include 'forums/pagination.html' %}
+<form id="topic_form" action="." method="post">{% csrf_token %}
+<table class="forum-topic-table">
+   <thead>
+      <tr>
+         <th>Forum</th>
+         <th>Topic</th>
+         <th><input type="checkbox" id="master_check" /></th>
+      </tr>
+   </thead>
+   <tbody>
+      {% for topic in page.object_list %}
+         <tr>
+            <td><a href="{{ topic.forum.get_absolute_url }}">{{ topic.forum.name }}</a></td>
+            <td><a href="{{ topic.get_absolute_url }}">{{ topic.name }}</a></td>
+            <td><input type="checkbox" class="topic_box" name="delete_ids" value="{{ topic.id }}" /></td>
+         </tr>
+      {% empty %}
+         <tr><td colspan="3"><em>No topics found</em></td></tr>
+      {% endfor %}
+   </tbody>
+</table>
+{% include 'forums/pagination.html' %}
+{% if page.object_list %}
+<input type="hidden" name="page" value="{{ page.number }}" />
+<input type="submit" name="delete_selected" value="Delete Selected" />
+{% endif %}
+</form>
+{% endblock %}
--- a/gpp/templates/forums/topic.html	Wed Jul 14 02:35:39 2010 +0000
+++ b/gpp/templates/forums/topic.html	Sun Aug 01 21:26:12 2010 +0000
@@ -59,6 +59,27 @@
 {% endif %}
 
 {% if user.is_authenticated %}
+<form action={% if is_bookmarked %}"{% url forums-unfavorite_topic topic.id %}"{% else %}"{% url forums-favorite_topic topic.id %}"{% endif %} method="post">{% csrf_token %}
+<fieldset>
+   <legend>Favorite Options</legend>
+   <p>
+   {% if is_favorite %}
+      <img src="{{ MEDIA_URL }}icons/delete.png" alt="Favorite" />
+      You currently have saved this topic in your list of favorites.
+      <input type="submit" value="Remove from favorites" />
+   {% else %}
+      <img src="{{ MEDIA_URL }}icons/add.png" alt="Favorite" />
+      Would you like to save this topic to your favorites list?
+      <input type="submit" value="Save to favorites" />
+   {% endif %}
+   </p>
+   <p>
+   To manage all your forum topic favorites, please visit your 
+   <a href="{% url forums-manage_favorites %}">favorites page</a>.
+   </p>
+</fieldset>
+</form>
+
 <form action={% if is_subscribed %}"{% url forums-unsubscribe_topic topic.id %}"{% else %}"{% url forums-subscribe_topic topic.id %}"{% endif %} method="post">{% csrf_token %}
 <fieldset>
    <legend>Subscription Options</legend>
Binary file media/icons/add.png has changed