annotate gpp/forums/views.py @ 91:62af8cd8f57b

Forums: added support for Post get_absolute_url()
author Brian Neal <bgneal@gmail.com>
date Sun, 13 Sep 2009 04:05:37 +0000
parents 317c7bcaecee
children 4c33e266db03
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@90 7 from django.http import HttpResponseForbidden
bgneal@83 8 from django.http import HttpResponseRedirect
bgneal@83 9 from django.core.urlresolvers import reverse
bgneal@91 10 from django.core.paginator import InvalidPage
bgneal@82 11 from django.shortcuts import get_object_or_404
bgneal@81 12 from django.shortcuts import render_to_response
bgneal@81 13 from django.template import RequestContext
bgneal@89 14 from django.views.decorators.http import require_POST
bgneal@81 15
bgneal@90 16 from core.paginator import DiggPaginator
bgneal@81 17 from forums.models import Forum
bgneal@83 18 from forums.models import Topic
bgneal@91 19 from forums.models import Post
bgneal@83 20 from forums.forms import NewTopicForm
bgneal@86 21 from forums.forms import PostForm
bgneal@81 22
bgneal@90 23 #######################################################################
bgneal@90 24
bgneal@90 25 POSTS_PER_PAGE = 2
bgneal@90 26
bgneal@90 27 def create_paginator(links):
bgneal@90 28 return DiggPaginator(links, POSTS_PER_PAGE, body=5, tail=2, margin=3, padding=2)
bgneal@90 29
bgneal@90 30 #######################################################################
bgneal@81 31
bgneal@81 32 def index(request):
bgneal@82 33 """
bgneal@82 34 This view displays all the forums available, ordered in each category.
bgneal@82 35 """
bgneal@81 36 forums = Forum.objects.all().select_related()
bgneal@81 37 cats = {}
bgneal@81 38 for forum in forums:
bgneal@81 39 cat = cats.setdefault(forum.category.id, {
bgneal@81 40 'cat': forum.category,
bgneal@81 41 'forums': [],
bgneal@81 42 })
bgneal@81 43 cat['forums'].append(forum)
bgneal@81 44
bgneal@81 45 cmpdef = lambda a, b: cmp(a['cat'].position, b['cat'].position)
bgneal@81 46 cats = sorted(cats.values(), cmpdef)
bgneal@81 47
bgneal@81 48 return render_to_response('forums/index.html', {
bgneal@81 49 'cats': cats,
bgneal@81 50 },
bgneal@81 51 context_instance=RequestContext(request))
bgneal@81 52
bgneal@82 53
bgneal@81 54 def forum_index(request, slug):
bgneal@82 55 """
bgneal@82 56 Displays all the topics in a forum.
bgneal@82 57 """
bgneal@82 58 forum = get_object_or_404(Forum, slug=slug)
bgneal@82 59 topics = forum.topics.select_related()
bgneal@82 60
bgneal@82 61 return render_to_response('forums/forum_index.html', {
bgneal@82 62 'forum': forum,
bgneal@82 63 'topics': topics,
bgneal@82 64 },
bgneal@82 65 context_instance=RequestContext(request))
bgneal@82 66
bgneal@82 67
bgneal@82 68 def topic_index(request, id):
bgneal@82 69 """
bgneal@82 70 Displays all the posts in a topic.
bgneal@82 71 """
bgneal@86 72 topic = get_object_or_404(Topic, pk=id)
bgneal@86 73 topic.view_count += 1
bgneal@86 74 topic.save()
bgneal@86 75
bgneal@86 76 posts = topic.posts.select_related()
bgneal@90 77 paginator = create_paginator(posts)
bgneal@90 78 page_num = int(request.GET.get('page', '1'))
bgneal@90 79 try:
bgneal@90 80 page = paginator.page(page_num)
bgneal@90 81 except InvalidPage:
bgneal@90 82 raise Http404
bgneal@90 83
bgneal@90 84 last_page = page_num == paginator.num_pages
bgneal@86 85
bgneal@86 86 return render_to_response('forums/topic.html', {
bgneal@86 87 'forum': topic.forum,
bgneal@86 88 'topic': topic,
bgneal@90 89 'page': page,
bgneal@87 90 'last_page': last_page,
bgneal@89 91 'form': PostForm(initial={'topic_id': topic.id}),
bgneal@86 92 },
bgneal@86 93 context_instance=RequestContext(request))
bgneal@83 94
bgneal@83 95
bgneal@83 96 @login_required
bgneal@83 97 def new_topic(request, slug):
bgneal@83 98 """
bgneal@83 99 This view handles the creation of new topics.
bgneal@83 100 """
bgneal@83 101 forum = get_object_or_404(Forum, slug=slug)
bgneal@83 102 if request.method == 'POST':
bgneal@83 103 form = NewTopicForm(request.POST)
bgneal@83 104 if form.is_valid():
bgneal@83 105 topic = form.save(forum, request.user, request.META.get("REMOTE_ADDR"))
bgneal@83 106 return HttpResponseRedirect(reverse('forums-new_topic_thanks',
bgneal@83 107 kwargs={'tid': topic.pk}))
bgneal@83 108 else:
bgneal@83 109 form = NewTopicForm()
bgneal@83 110
bgneal@83 111 return render_to_response('forums/new_topic.html', {
bgneal@83 112 'forum': forum,
bgneal@83 113 'form': form,
bgneal@83 114 },
bgneal@83 115 context_instance=RequestContext(request))
bgneal@83 116
bgneal@83 117
bgneal@83 118 @login_required
bgneal@83 119 def new_topic_thanks(request, tid):
bgneal@83 120 """
bgneal@83 121 This view displays the success page for a newly created topic.
bgneal@83 122 """
bgneal@83 123 topic = get_object_or_404(Topic, pk=tid)
bgneal@83 124 return render_to_response('forums/new_topic_thanks.html', {
bgneal@83 125 'forum': topic.forum,
bgneal@83 126 'topic': topic,
bgneal@83 127 },
bgneal@83 128 context_instance=RequestContext(request))
bgneal@89 129
bgneal@89 130
bgneal@89 131 @require_POST
bgneal@89 132 def quick_reply_ajax(request):
bgneal@89 133 """
bgneal@89 134 This function handles the quick reply to a thread function. This
bgneal@89 135 function is meant to be the target of an AJAX post, and returns
bgneal@89 136 the HTML for the new post, which the client-side script appends
bgneal@89 137 to the document.
bgneal@89 138 """
bgneal@90 139 if not request.user.is_authenticated():
bgneal@90 140 return HttpResponseForbidden()
bgneal@90 141
bgneal@89 142 form = PostForm(request.POST)
bgneal@89 143 if form.is_valid():
bgneal@89 144 post = form.save(request.user, request.META.get("REMOTE_ADDR"))
bgneal@89 145 return render_to_response('forums/display_post.html', {
bgneal@89 146 'post': post,
bgneal@89 147 },
bgneal@89 148 context_instance=RequestContext(request))
bgneal@89 149
bgneal@89 150 return HttpResponseBadRequest();
bgneal@89 151
bgneal@91 152
bgneal@91 153 def goto_post(request, post_id):
bgneal@91 154 """
bgneal@91 155 This function calculates what page a given post is on, then redirects
bgneal@91 156 to that URL. This function is the target of get_absolute_url() for
bgneal@91 157 Post objects.
bgneal@91 158 """
bgneal@91 159 post = get_object_or_404(Post, pk=post_id)
bgneal@91 160 count = post.topic.posts.filter(creation_date__lt=post.creation_date).count()
bgneal@91 161 page = count / POSTS_PER_PAGE + 1
bgneal@91 162 url = reverse('forums-topic_index', kwargs={'id': post.topic.id}) + \
bgneal@91 163 '?page=%s#p%s' % (page, post.id)
bgneal@91 164 return HttpResponseRedirect(url)
bgneal@91 165