# HG changeset patch # User Brian Neal # Date 1252814737 0 # Node ID 62af8cd8f57be7b92d1d4ee0d0595c70563c937b # Parent 317c7bcaeceea89f6434cbccb70159e094a52467 Forums: added support for Post get_absolute_url() diff -r 317c7bcaecee -r 62af8cd8f57b gpp/forums/models.py --- 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: diff -r 317c7bcaecee -r 62af8cd8f57b gpp/forums/urls.py --- 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\d+)/$', 'topic_index', name='forums-topic_index'), url(r'^forum/(?P[\w\d-]+)/$', 'forum_index', name='forums-forum_index'), url(r'^forum/(?P[\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'), ) diff -r 317c7bcaecee -r 62af8cd8f57b gpp/forums/views.py --- 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) +