annotate gpp/forums/views.py @ 89:021492db4aad

Forums: Got the reply function working.
author Brian Neal <bgneal@gmail.com>
date Sat, 12 Sep 2009 21:29:31 +0000
parents 515d1daec811
children 317c7bcaecee
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@89 6 from django.http import HttpResponseBadRequest
bgneal@83 7 from django.http import HttpResponseRedirect
bgneal@83 8 from django.core.urlresolvers import reverse
bgneal@82 9 from django.shortcuts import get_object_or_404
bgneal@81 10 from django.shortcuts import render_to_response
bgneal@81 11 from django.template import RequestContext
bgneal@89 12 from django.views.decorators.http import require_POST
bgneal@81 13
bgneal@81 14 from forums.models import Forum
bgneal@83 15 from forums.models import Topic
bgneal@83 16 from forums.forms import NewTopicForm
bgneal@86 17 from forums.forms import PostForm
bgneal@81 18
bgneal@81 19
bgneal@81 20 def index(request):
bgneal@82 21 """
bgneal@82 22 This view displays all the forums available, ordered in each category.
bgneal@82 23 """
bgneal@81 24 forums = Forum.objects.all().select_related()
bgneal@81 25 cats = {}
bgneal@81 26 for forum in forums:
bgneal@81 27 cat = cats.setdefault(forum.category.id, {
bgneal@81 28 'cat': forum.category,
bgneal@81 29 'forums': [],
bgneal@81 30 })
bgneal@81 31 cat['forums'].append(forum)
bgneal@81 32
bgneal@81 33 cmpdef = lambda a, b: cmp(a['cat'].position, b['cat'].position)
bgneal@81 34 cats = sorted(cats.values(), cmpdef)
bgneal@81 35
bgneal@81 36 return render_to_response('forums/index.html', {
bgneal@81 37 'cats': cats,
bgneal@81 38 },
bgneal@81 39 context_instance=RequestContext(request))
bgneal@81 40
bgneal@82 41
bgneal@81 42 def forum_index(request, slug):
bgneal@82 43 """
bgneal@82 44 Displays all the topics in a forum.
bgneal@82 45 """
bgneal@82 46 forum = get_object_or_404(Forum, slug=slug)
bgneal@82 47 topics = forum.topics.select_related()
bgneal@82 48
bgneal@82 49 return render_to_response('forums/forum_index.html', {
bgneal@82 50 'forum': forum,
bgneal@82 51 'topics': topics,
bgneal@82 52 },
bgneal@82 53 context_instance=RequestContext(request))
bgneal@82 54
bgneal@82 55
bgneal@82 56 def topic_index(request, id):
bgneal@82 57 """
bgneal@82 58 Displays all the posts in a topic.
bgneal@82 59 """
bgneal@86 60 topic = get_object_or_404(Topic, pk=id)
bgneal@86 61 topic.view_count += 1
bgneal@86 62 topic.save()
bgneal@86 63
bgneal@86 64 posts = topic.posts.select_related()
bgneal@87 65 last_page = True # TODO
bgneal@86 66
bgneal@86 67 return render_to_response('forums/topic.html', {
bgneal@86 68 'forum': topic.forum,
bgneal@86 69 'topic': topic,
bgneal@86 70 'posts': posts,
bgneal@87 71 'last_page': last_page,
bgneal@89 72 'form': PostForm(initial={'topic_id': topic.id}),
bgneal@86 73 },
bgneal@86 74 context_instance=RequestContext(request))
bgneal@83 75
bgneal@83 76
bgneal@83 77 @login_required
bgneal@83 78 def new_topic(request, slug):
bgneal@83 79 """
bgneal@83 80 This view handles the creation of new topics.
bgneal@83 81 """
bgneal@83 82 forum = get_object_or_404(Forum, slug=slug)
bgneal@83 83 if request.method == 'POST':
bgneal@83 84 form = NewTopicForm(request.POST)
bgneal@83 85 if form.is_valid():
bgneal@83 86 topic = form.save(forum, request.user, request.META.get("REMOTE_ADDR"))
bgneal@83 87 return HttpResponseRedirect(reverse('forums-new_topic_thanks',
bgneal@83 88 kwargs={'tid': topic.pk}))
bgneal@83 89 else:
bgneal@83 90 form = NewTopicForm()
bgneal@83 91
bgneal@83 92 return render_to_response('forums/new_topic.html', {
bgneal@83 93 'forum': forum,
bgneal@83 94 'form': form,
bgneal@83 95 },
bgneal@83 96 context_instance=RequestContext(request))
bgneal@83 97
bgneal@83 98
bgneal@83 99 @login_required
bgneal@83 100 def new_topic_thanks(request, tid):
bgneal@83 101 """
bgneal@83 102 This view displays the success page for a newly created topic.
bgneal@83 103 """
bgneal@83 104 topic = get_object_or_404(Topic, pk=tid)
bgneal@83 105 return render_to_response('forums/new_topic_thanks.html', {
bgneal@83 106 'forum': topic.forum,
bgneal@83 107 'topic': topic,
bgneal@83 108 },
bgneal@83 109 context_instance=RequestContext(request))
bgneal@89 110
bgneal@89 111
bgneal@89 112 @login_required
bgneal@89 113 @require_POST
bgneal@89 114 def quick_reply_ajax(request):
bgneal@89 115 """
bgneal@89 116 This function handles the quick reply to a thread function. This
bgneal@89 117 function is meant to be the target of an AJAX post, and returns
bgneal@89 118 the HTML for the new post, which the client-side script appends
bgneal@89 119 to the document.
bgneal@89 120 """
bgneal@89 121 form = PostForm(request.POST)
bgneal@89 122 if form.is_valid():
bgneal@89 123 post = form.save(request.user, request.META.get("REMOTE_ADDR"))
bgneal@89 124 return render_to_response('forums/display_post.html', {
bgneal@89 125 'post': post,
bgneal@89 126 },
bgneal@89 127 context_instance=RequestContext(request))
bgneal@89 128
bgneal@89 129 return HttpResponseBadRequest();
bgneal@89 130