diff 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
line wrap: on
line diff
--- a/gpp/forums/views.py	Sun Sep 13 00:21:54 2009 +0000
+++ b/gpp/forums/views.py	Sun Sep 13 04:05:37 2009 +0000
@@ -7,6 +7,7 @@
 from django.http import HttpResponseForbidden
 from django.http import HttpResponseRedirect
 from django.core.urlresolvers import reverse
+from django.core.paginator import InvalidPage
 from django.shortcuts import get_object_or_404
 from django.shortcuts import render_to_response
 from django.template import RequestContext
@@ -15,6 +16,7 @@
 from core.paginator import DiggPaginator
 from forums.models import Forum
 from forums.models import Topic
+from forums.models import Post
 from forums.forms import NewTopicForm
 from forums.forms import PostForm
 
@@ -147,3 +149,17 @@
 
     return HttpResponseBadRequest();
 
+
+def goto_post(request, post_id):
+    """
+    This function calculates what page a given post is on, then redirects
+    to that URL. This function is the target of get_absolute_url() for
+    Post objects.
+    """
+    post = get_object_or_404(Post, pk=post_id)
+    count = post.topic.posts.filter(creation_date__lt=post.creation_date).count()
+    page = count / POSTS_PER_PAGE + 1
+    url = reverse('forums-topic_index', kwargs={'id': post.topic.id}) + \
+        '?page=%s#p%s' % (page, post.id)
+    return HttpResponseRedirect(url)
+