annotate gpp/forums/views.py @ 97:96eec1ed0fd3

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