annotate gpp/forums/views.py @ 88:3abf0b045749

Updates to base template. Added a link to forums. Display username in header if logged in.
author Brian Neal <bgneal@gmail.com>
date Sat, 12 Sep 2009 18:51:55 +0000
parents 515d1daec811
children 021492db4aad
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@87 63 last_page = True # TODO
bgneal@86 64
bgneal@86 65 return render_to_response('forums/topic.html', {
bgneal@86 66 'forum': topic.forum,
bgneal@86 67 'topic': topic,
bgneal@86 68 'posts': posts,
bgneal@87 69 'last_page': last_page,
bgneal@86 70 'form': PostForm(),
bgneal@86 71 },
bgneal@86 72 context_instance=RequestContext(request))
bgneal@83 73
bgneal@83 74
bgneal@83 75 @login_required
bgneal@83 76 def new_topic(request, slug):
bgneal@83 77 """
bgneal@83 78 This view handles the creation of new topics.
bgneal@83 79 """
bgneal@83 80 forum = get_object_or_404(Forum, slug=slug)
bgneal@83 81 if request.method == 'POST':
bgneal@83 82 form = NewTopicForm(request.POST)
bgneal@83 83 if form.is_valid():
bgneal@83 84 topic = form.save(forum, request.user, request.META.get("REMOTE_ADDR"))
bgneal@83 85 return HttpResponseRedirect(reverse('forums-new_topic_thanks',
bgneal@83 86 kwargs={'tid': topic.pk}))
bgneal@83 87 else:
bgneal@83 88 form = NewTopicForm()
bgneal@83 89
bgneal@83 90 return render_to_response('forums/new_topic.html', {
bgneal@83 91 'forum': forum,
bgneal@83 92 'form': form,
bgneal@83 93 },
bgneal@83 94 context_instance=RequestContext(request))
bgneal@83 95
bgneal@83 96
bgneal@83 97 @login_required
bgneal@83 98 def new_topic_thanks(request, tid):
bgneal@83 99 """
bgneal@83 100 This view displays the success page for a newly created topic.
bgneal@83 101 """
bgneal@83 102 topic = get_object_or_404(Topic, pk=tid)
bgneal@83 103 return render_to_response('forums/new_topic_thanks.html', {
bgneal@83 104 'forum': topic.forum,
bgneal@83 105 'topic': topic,
bgneal@83 106 },
bgneal@83 107 context_instance=RequestContext(request))