annotate gpp/forums/views.py @ 82:bc3978f023c2

Forums: started the ability to display topics inside a forum.
author Brian Neal <bgneal@gmail.com>
date Sun, 23 Aug 2009 04:04:29 +0000
parents e356ea79a7a2
children 5b4c812b448e
rev   line source
bgneal@81 1 """
bgneal@81 2 Views for the forums application.
bgneal@81 3 """
bgneal@82 4 from django.http import Http404
bgneal@82 5 from django.shortcuts import get_object_or_404
bgneal@81 6 from django.shortcuts import render_to_response
bgneal@81 7 from django.template import RequestContext
bgneal@81 8
bgneal@81 9 from forums.models import Forum
bgneal@81 10
bgneal@81 11
bgneal@81 12 def index(request):
bgneal@82 13 """
bgneal@82 14 This view displays all the forums available, ordered in each category.
bgneal@82 15 """
bgneal@81 16 forums = Forum.objects.all().select_related()
bgneal@81 17 cats = {}
bgneal@81 18 for forum in forums:
bgneal@81 19 cat = cats.setdefault(forum.category.id, {
bgneal@81 20 'cat': forum.category,
bgneal@81 21 'forums': [],
bgneal@81 22 })
bgneal@81 23 cat['forums'].append(forum)
bgneal@81 24
bgneal@81 25 cmpdef = lambda a, b: cmp(a['cat'].position, b['cat'].position)
bgneal@81 26 cats = sorted(cats.values(), cmpdef)
bgneal@81 27
bgneal@81 28 return render_to_response('forums/index.html', {
bgneal@81 29 'cats': cats,
bgneal@81 30 },
bgneal@81 31 context_instance=RequestContext(request))
bgneal@81 32
bgneal@82 33
bgneal@81 34 def forum_index(request, slug):
bgneal@82 35 """
bgneal@82 36 Displays all the topics in a forum.
bgneal@82 37 """
bgneal@82 38 forum = get_object_or_404(Forum, slug=slug)
bgneal@82 39 topics = forum.topics.select_related()
bgneal@82 40
bgneal@82 41 return render_to_response('forums/forum_index.html', {
bgneal@82 42 'forum': forum,
bgneal@82 43 'topics': topics,
bgneal@82 44 },
bgneal@82 45 context_instance=RequestContext(request))
bgneal@82 46
bgneal@82 47
bgneal@82 48 def topic_index(request, id):
bgneal@82 49 """
bgneal@82 50 Displays all the posts in a topic.
bgneal@82 51 """
bgneal@82 52 raise Http404