changeset 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 62eb9cbbcffc
files gpp/forums/models.py gpp/forums/urls.py gpp/forums/views.py
diffstat 3 files changed, 21 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/gpp/forums/models.py	Sun Sep 13 00:21:54 2009 +0000
+++ b/gpp/forums/models.py	Sun Sep 13 04:05:37 2009 +0000
@@ -121,6 +121,10 @@
     class Meta:
         ordering = ('creation_date', )
 
+    @models.permalink
+    def get_absolute_url(self):
+        return ('forums-goto_post', [self.pk])
+
     def summary(self):
         LIMIT = 50
         if len(self.body) < LIMIT:
--- a/gpp/forums/urls.py	Sun Sep 13 00:21:54 2009 +0000
+++ b/gpp/forums/urls.py	Sun Sep 13 04:05:37 2009 +0000
@@ -9,6 +9,7 @@
     url(r'^topic/(?P<id>\d+)/$', 'topic_index', name='forums-topic_index'),
     url(r'^forum/(?P<slug>[\w\d-]+)/$', 'forum_index', name='forums-forum_index'),
     url(r'^forum/(?P<slug>[\w\d-]+)/new-topic/$', 'new_topic', name='forums-new_topic'),
+    url(r'^post/(\d+)/$', 'goto_post', name='forums-goto_post'),
     url(r'^quick-reply/$', 'quick_reply_ajax', name='forums-quick_reply'),
 )
 
--- 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)
+