annotate gpp/forums/views.py @ 86:f81226b5e87b

Forums: Some display work for the posts within a topic. Sketched out a post reply form.
author Brian Neal <bgneal@gmail.com>
date Sat, 05 Sep 2009 20:47:08 +0000
parents 5b4c812b448e
children 515d1daec811
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@86 15 from forums.forms import PostForm
bgneal@81 16
bgneal@81 17
bgneal@81 18 def index(request):
bgneal@82 19 """
bgneal@82 20 This view displays all the forums available, ordered in each category.
bgneal@82 21 """
bgneal@81 22 forums = Forum.objects.all().select_related()
bgneal@81 23 cats = {}
bgneal@81 24 for forum in forums:
bgneal@81 25 cat = cats.setdefault(forum.category.id, {
bgneal@81 26 'cat': forum.category,
bgneal@81 27 'forums': [],
bgneal@81 28 })
bgneal@81 29 cat['forums'].append(forum)
bgneal@81 30
bgneal@81 31 cmpdef = lambda a, b: cmp(a['cat'].position, b['cat'].position)
bgneal@81 32 cats = sorted(cats.values(), cmpdef)
bgneal@81 33
bgneal@81 34 return render_to_response('forums/index.html', {
bgneal@81 35 'cats': cats,
bgneal@81 36 },
bgneal@81 37 context_instance=RequestContext(request))
bgneal@81 38
bgneal@82 39
bgneal@81 40 def forum_index(request, slug):
bgneal@82 41 """
bgneal@82 42 Displays all the topics in a forum.
bgneal@82 43 """
bgneal@82 44 forum = get_object_or_404(Forum, slug=slug)
bgneal@82 45 topics = forum.topics.select_related()
bgneal@82 46
bgneal@82 47 return render_to_response('forums/forum_index.html', {
bgneal@82 48 'forum': forum,
bgneal@82 49 'topics': topics,
bgneal@82 50 },
bgneal@82 51 context_instance=RequestContext(request))
bgneal@82 52
bgneal@82 53
bgneal@82 54 def topic_index(request, id):
bgneal@82 55 """
bgneal@82 56 Displays all the posts in a topic.
bgneal@82 57 """
bgneal@86 58 topic = get_object_or_404(Topic, pk=id)
bgneal@86 59 topic.view_count += 1
bgneal@86 60 topic.save()
bgneal@86 61
bgneal@86 62 posts = topic.posts.select_related()
bgneal@86 63
bgneal@86 64 return render_to_response('forums/topic.html', {
bgneal@86 65 'forum': topic.forum,
bgneal@86 66 'topic': topic,
bgneal@86 67 'posts': posts,
bgneal@86 68 'form': PostForm(),
bgneal@86 69 },
bgneal@86 70 context_instance=RequestContext(request))
bgneal@83 71
bgneal@83 72
bgneal@83 73 @login_required
bgneal@83 74 def new_topic(request, slug):
bgneal@83 75 """
bgneal@83 76 This view handles the creation of new topics.
bgneal@83 77 """
bgneal@83 78 forum = get_object_or_404(Forum, slug=slug)
bgneal@83 79 if request.method == 'POST':
bgneal@83 80 form = NewTopicForm(request.POST)
bgneal@83 81 if form.is_valid():
bgneal@83 82 topic = form.save(forum, request.user, request.META.get("REMOTE_ADDR"))
bgneal@83 83 return HttpResponseRedirect(reverse('forums-new_topic_thanks',
bgneal@83 84 kwargs={'tid': topic.pk}))
bgneal@83 85 else:
bgneal@83 86 form = NewTopicForm()
bgneal@83 87
bgneal@83 88 return render_to_response('forums/new_topic.html', {
bgneal@83 89 'forum': forum,
bgneal@83 90 'form': form,
bgneal@83 91 },
bgneal@83 92 context_instance=RequestContext(request))
bgneal@83 93
bgneal@83 94
bgneal@83 95 @login_required
bgneal@83 96 def new_topic_thanks(request, tid):
bgneal@83 97 """
bgneal@83 98 This view displays the success page for a newly created topic.
bgneal@83 99 """
bgneal@83 100 topic = get_object_or_404(Topic, pk=tid)
bgneal@83 101 return render_to_response('forums/new_topic_thanks.html', {
bgneal@83 102 'forum': topic.forum,
bgneal@83 103 'topic': topic,
bgneal@83 104 },
bgneal@83 105 context_instance=RequestContext(request))