annotate gpp/forums/views.py @ 83:5b4c812b448e

Forums: Added the ability to add a new topic. This is very much a work in progress.
author Brian Neal <bgneal@gmail.com>
date Sat, 29 Aug 2009 20:54:16 +0000
parents bc3978f023c2
children f81226b5e87b
rev   line source
bgneal@81 1 """
bgneal@81 2 Views for the forums application.
bgneal@81 3 """
bgneal@83 4 from django.contrib.auth.decorators import login_required
bgneal@82 5 from django.http import Http404
bgneal@83 6 from django.http import HttpResponseRedirect
bgneal@83 7 from django.core.urlresolvers import reverse
bgneal@82 8 from django.shortcuts import get_object_or_404
bgneal@81 9 from django.shortcuts import render_to_response
bgneal@81 10 from django.template import RequestContext
bgneal@81 11
bgneal@81 12 from forums.models import Forum
bgneal@83 13 from forums.models import Topic
bgneal@83 14 from forums.forms import NewTopicForm
bgneal@81 15
bgneal@81 16
bgneal@81 17 def index(request):
bgneal@82 18 """
bgneal@82 19 This view displays all the forums available, ordered in each category.
bgneal@82 20 """
bgneal@81 21 forums = Forum.objects.all().select_related()
bgneal@81 22 cats = {}
bgneal@81 23 for forum in forums:
bgneal@81 24 cat = cats.setdefault(forum.category.id, {
bgneal@81 25 'cat': forum.category,
bgneal@81 26 'forums': [],
bgneal@81 27 })
bgneal@81 28 cat['forums'].append(forum)
bgneal@81 29
bgneal@81 30 cmpdef = lambda a, b: cmp(a['cat'].position, b['cat'].position)
bgneal@81 31 cats = sorted(cats.values(), cmpdef)
bgneal@81 32
bgneal@81 33 return render_to_response('forums/index.html', {
bgneal@81 34 'cats': cats,
bgneal@81 35 },
bgneal@81 36 context_instance=RequestContext(request))
bgneal@81 37
bgneal@82 38
bgneal@81 39 def forum_index(request, slug):
bgneal@82 40 """
bgneal@82 41 Displays all the topics in a forum.
bgneal@82 42 """
bgneal@82 43 forum = get_object_or_404(Forum, slug=slug)
bgneal@82 44 topics = forum.topics.select_related()
bgneal@82 45
bgneal@82 46 return render_to_response('forums/forum_index.html', {
bgneal@82 47 'forum': forum,
bgneal@82 48 'topics': topics,
bgneal@82 49 },
bgneal@82 50 context_instance=RequestContext(request))
bgneal@82 51
bgneal@82 52
bgneal@82 53 def topic_index(request, id):
bgneal@82 54 """
bgneal@82 55 Displays all the posts in a topic.
bgneal@82 56 """
bgneal@82 57 raise Http404
bgneal@83 58
bgneal@83 59
bgneal@83 60 @login_required
bgneal@83 61 def new_topic(request, slug):
bgneal@83 62 """
bgneal@83 63 This view handles the creation of new topics.
bgneal@83 64 """
bgneal@83 65 forum = get_object_or_404(Forum, slug=slug)
bgneal@83 66 if request.method == 'POST':
bgneal@83 67 form = NewTopicForm(request.POST)
bgneal@83 68 if form.is_valid():
bgneal@83 69 topic = form.save(forum, request.user, request.META.get("REMOTE_ADDR"))
bgneal@83 70 return HttpResponseRedirect(reverse('forums-new_topic_thanks',
bgneal@83 71 kwargs={'tid': topic.pk}))
bgneal@83 72 else:
bgneal@83 73 form = NewTopicForm()
bgneal@83 74
bgneal@83 75 return render_to_response('forums/new_topic.html', {
bgneal@83 76 'forum': forum,
bgneal@83 77 'form': form,
bgneal@83 78 },
bgneal@83 79 context_instance=RequestContext(request))
bgneal@83 80
bgneal@83 81
bgneal@83 82 @login_required
bgneal@83 83 def new_topic_thanks(request, tid):
bgneal@83 84 """
bgneal@83 85 This view displays the success page for a newly created topic.
bgneal@83 86 """
bgneal@83 87 topic = get_object_or_404(Topic, pk=tid)
bgneal@83 88 return render_to_response('forums/new_topic_thanks.html', {
bgneal@83 89 'forum': topic.forum,
bgneal@83 90 'topic': topic,
bgneal@83 91 },
bgneal@83 92 context_instance=RequestContext(request))