annotate gpp/forums/views.py @ 96:93d9e74a471e

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