bgneal@81: """
bgneal@81: Views for the forums application.
bgneal@81: """
bgneal@83: from django.contrib.auth.decorators import login_required
bgneal@82: from django.http import Http404
bgneal@83: from django.http import HttpResponseRedirect
bgneal@83: from django.core.urlresolvers import reverse
bgneal@82: from django.shortcuts import get_object_or_404
bgneal@81: from django.shortcuts import render_to_response
bgneal@81: from django.template import RequestContext
bgneal@81: 
bgneal@81: from forums.models import Forum
bgneal@83: from forums.models import Topic
bgneal@83: from forums.forms import NewTopicForm
bgneal@81: 
bgneal@81: 
bgneal@81: def index(request):
bgneal@82:     """
bgneal@82:     This view displays all the forums available, ordered in each category.
bgneal@82:     """
bgneal@81:     forums = Forum.objects.all().select_related()
bgneal@81:     cats = {}
bgneal@81:     for forum in forums:
bgneal@81:         cat = cats.setdefault(forum.category.id, {
bgneal@81:             'cat': forum.category,
bgneal@81:             'forums': [],
bgneal@81:             })
bgneal@81:         cat['forums'].append(forum)
bgneal@81: 
bgneal@81:     cmpdef = lambda a, b: cmp(a['cat'].position, b['cat'].position)
bgneal@81:     cats = sorted(cats.values(), cmpdef)
bgneal@81: 
bgneal@81:     return render_to_response('forums/index.html', {
bgneal@81:         'cats': cats,
bgneal@81:         },
bgneal@81:         context_instance=RequestContext(request))
bgneal@81: 
bgneal@82: 
bgneal@81: def forum_index(request, slug):
bgneal@82:     """
bgneal@82:     Displays all the topics in a forum.
bgneal@82:     """
bgneal@82:     forum = get_object_or_404(Forum, slug=slug)
bgneal@82:     topics = forum.topics.select_related()
bgneal@82:     
bgneal@82:     return render_to_response('forums/forum_index.html', {
bgneal@82:         'forum': forum,
bgneal@82:         'topics': topics,
bgneal@82:         },
bgneal@82:         context_instance=RequestContext(request))
bgneal@82: 
bgneal@82: 
bgneal@82: def topic_index(request, id):
bgneal@82:     """
bgneal@82:     Displays all the posts in a topic.
bgneal@82:     """
bgneal@82:     raise Http404
bgneal@83: 
bgneal@83: 
bgneal@83: @login_required
bgneal@83: def new_topic(request, slug):
bgneal@83:     """
bgneal@83:     This view handles the creation of new topics.
bgneal@83:     """
bgneal@83:     forum = get_object_or_404(Forum, slug=slug)
bgneal@83:     if request.method == 'POST':
bgneal@83:         form = NewTopicForm(request.POST)
bgneal@83:         if form.is_valid():
bgneal@83:             topic = form.save(forum, request.user, request.META.get("REMOTE_ADDR"))
bgneal@83:             return HttpResponseRedirect(reverse('forums-new_topic_thanks',
bgneal@83:                                             kwargs={'tid': topic.pk}))
bgneal@83:     else:
bgneal@83:         form = NewTopicForm()
bgneal@83:     
bgneal@83:     return render_to_response('forums/new_topic.html', {
bgneal@83:         'forum': forum,
bgneal@83:         'form': form,
bgneal@83:         },
bgneal@83:         context_instance=RequestContext(request))
bgneal@83: 
bgneal@83: 
bgneal@83: @login_required
bgneal@83: def new_topic_thanks(request, tid):
bgneal@83:     """
bgneal@83:     This view displays the success page for a newly created topic.
bgneal@83:     """
bgneal@83:     topic = get_object_or_404(Topic, pk=tid)
bgneal@83:     return render_to_response('forums/new_topic_thanks.html', {
bgneal@83:         'forum': topic.forum,
bgneal@83:         'topic': topic,
bgneal@83:         },
bgneal@83:         context_instance=RequestContext(request))