comparison 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
comparison
equal deleted inserted replaced
90:317c7bcaecee 91:62af8cd8f57b
5 from django.http import Http404 5 from django.http import Http404
6 from django.http import HttpResponseBadRequest 6 from django.http import HttpResponseBadRequest
7 from django.http import HttpResponseForbidden 7 from django.http import HttpResponseForbidden
8 from django.http import HttpResponseRedirect 8 from django.http import HttpResponseRedirect
9 from django.core.urlresolvers import reverse 9 from django.core.urlresolvers import reverse
10 from django.core.paginator import InvalidPage
10 from django.shortcuts import get_object_or_404 11 from django.shortcuts import get_object_or_404
11 from django.shortcuts import render_to_response 12 from django.shortcuts import render_to_response
12 from django.template import RequestContext 13 from django.template import RequestContext
13 from django.views.decorators.http import require_POST 14 from django.views.decorators.http import require_POST
14 15
15 from core.paginator import DiggPaginator 16 from core.paginator import DiggPaginator
16 from forums.models import Forum 17 from forums.models import Forum
17 from forums.models import Topic 18 from forums.models import Topic
19 from forums.models import Post
18 from forums.forms import NewTopicForm 20 from forums.forms import NewTopicForm
19 from forums.forms import PostForm 21 from forums.forms import PostForm
20 22
21 ####################################################################### 23 #######################################################################
22 24
145 }, 147 },
146 context_instance=RequestContext(request)) 148 context_instance=RequestContext(request))
147 149
148 return HttpResponseBadRequest(); 150 return HttpResponseBadRequest();
149 151
152
153 def goto_post(request, post_id):
154 """
155 This function calculates what page a given post is on, then redirects
156 to that URL. This function is the target of get_absolute_url() for
157 Post objects.
158 """
159 post = get_object_or_404(Post, pk=post_id)
160 count = post.topic.posts.filter(creation_date__lt=post.creation_date).count()
161 page = count / POSTS_PER_PAGE + 1
162 url = reverse('forums-topic_index', kwargs={'id': post.topic.id}) + \
163 '?page=%s#p%s' % (page, post.id)
164 return HttpResponseRedirect(url)
165