Commits

Dan Carroll committed 4d4135b

Port activity view over from django-activitysync, since it was dependent on this site config. Also, make new paginator improvements work with the blog views (and more independent in general).

Comments (0)

Files changed (5)

mysite/blog/paginator.py

 from django.core.paginator import Paginator, Page, PageNotAnInteger, EmptyPage
+from django.core.urlresolvers import reverse
 
 # Code borrowed from django-pagination.  I ripped it out since I didn't want the
 # rest of the functionality it provided (auto-pagination).
     template string for creating the links to the next and previous pages.
     """
 
-    def __init__(self, object_list, per_page, allow_empty_first_page=True,
-        link_template='?page=%d'):
+    def __init__(self, object_list, per_page, allow_empty_first_page=True):
         orphans = 0 # no orphans
         super(InfinitePaginator, self).__init__(object_list, per_page, orphans,
             allow_empty_first_page)
         # no count or num pages
         del self._num_pages, self._count
-        # bonus links
-        self.link_template = link_template
 
     def validate_number(self, number):
         """
 
     #Bonus methods for creating links
 
-    def next_link(self):
+    def next_link(self, paged_view_name):
         if self.has_next():
-            return self.paginator.link_template % (self.number + 1)
+            return reverse(paged_view_name, args=[self.number + 1])
         return None
 
-    def previous_link(self):
+    def previous_link(self, paged_view_name, first_page_view_name=None):
         if self.has_previous():
-            return self.paginator.link_template % (self.number - 1)
+            if self.number == 2 and first_page_view_name:
+                return reverse(first_page_view_name)
+            else:
+                return reverse(paged_view_name, args=[self.number - 1])
         return None
 
     def page_title(self):
             return 'Page %s' % self.number
         return None
 
-    def create_template_context(self):
+    def create_template_context(self, paged_view_name, first_page_view_name=None):
         return {
             'object_list': self.object_list,
             'page_title': self.page_title(),
             'has_next': self.has_next(),
             'has_previous': self.has_previous(),
-            'next': self.next_link(),
-            'previous': self.previous_link(),
+            'next': self.next_link(paged_view_name),
+            'previous': self.previous_link(paged_view_name, first_page_view_name),
         }
     #url (r'^search/$',
     #    view=blog_views.search,
     #    name='blog_search'),
-        
+
+    url(r'^page/(?P<page>\d+)/$',
+        view=blog_views.blog_entry_list,
+        kwargs={'explicit_page_request': True},
+        name='blog_index_paged'),
     url(r'^$',
         view=blog_views.blog_entry_list,
         name='blog_index'),
-from django.shortcuts import render_to_response, get_object_or_404
+from django.shortcuts import render_to_response, redirect, get_object_or_404
 from django.template import RequestContext
 from django.http import Http404
 from django.views.decorators.cache import cache_page
 #import re
 
 @cache_page(60 * 15)
-def blog_entry_list(request):
-    queryset = Entry.objects.published_for_list()
-
-    # Make sure page request is an int.  If not, deliver first page.
+def blog_entry_list(request, page=1, explicit_page_request=False):
+    # Make sure page parameter is an integer.
     try:
-        page = int(request.GET.get('page', '1'))
+        page = int(page)
     except ValueError:
         page = 1
 
-    paginator = InfinitePaginator(queryset, 10)
+    # Make sure we only have one canonical first page
+    if explicit_page_request and page == 1:
+        return redirect('blog_index')
+
+    # Previous URL used GET parameter 'page', so let's check
+    # for that and redirect to new view if necessary
+    if not explicit_page_request:
+        try:
+            requestNum = request.GET['page']
+            if requestNum != None and requestNum.isdigit():
+                return redirect('blog_index_paged', page=requestNum)
+        except KeyError:
+            pass
+
+    entry_list = Entry.objects.published_for_list()
+    paginator = InfinitePaginator(entry_list, 10)
     try:
         entries = paginator.page(page)
     except:
         raise Http404
 
     return render_to_response('blog/entry_list.html',
-                entries.create_template_context(),
+                entries.create_template_context('blog_index_paged', 'blog_index'),
                 context_instance=RequestContext(request))
 
 
     url(r'^admin/', include(admin.site.urls)),
     
     # Main sections
-    url(r'^activity/$', view='activitysync.views.activity', name='main_activity'),
-    url(r'^activity/page/(?P<page>\d+)/$', view='activitysync.views.activity', kwargs={'explicit_page_request': True}, name='activity_paged'),
+    url(r'^activity/$', view='mysite.views.activity', name='main_activity'),
+    url(r'^activity/page/(?P<page>\d+)/$',
+        view='mysite.views.activity',
+        kwargs={'explicit_page_request': True},
+        name='activity_paged'),
     url(r'^blog/', include('mysite.blog.urls')),
     
     # RSS feeds
 from django.contrib.sites.models import Site
-from django.http import HttpResponse
-from django.shortcuts import render_to_response
+from django.http import HttpResponse, Http404
+from django.shortcuts import render_to_response, redirect
 from django.template import RequestContext
 from django.views.decorators.cache import cache_page
 
 from mysite.blog.models import Entry
 from activitysync.models import Activity
 from blog.views import blog_entry_detail
+from mysite.blog.paginator import InfinitePaginator
 
 @cache_page(60 * 15)
 def index(request):
                  'activities': Activity.objects.published()[:5] },
                 context_instance=RequestContext(request))
 
+
+def activity(request, page="1", explicit_page_request=False):
+    # Make sure page parameter is an integer
+    try:
+        page = int(page)
+    except:
+        raise Http404
+
+    # Make sure we only have one canonical first page
+    if explicit_page_request and page == 1:
+        return redirect('main_activity')
+
+    # Previous URL used GET parameter 'page', so let's check
+    # for that and redirect to new view if necessary
+    if not explicit_page_request:
+        try:
+            requestNum = request.GET['page']
+            if requestNum != None and requestNum.isdigit():
+                return redirect('activity_paged', page=requestNum)
+        except KeyError:
+            pass
+
+    activity_list = Activity.objects.published().defer("username", "author", "comments", "guid")
+    paginator = InfinitePaginator(activity_list, 25)
+
+    try:
+        activities = paginator.page(page)
+    except:
+        raise Http404
+
+    return render_to_response('activity.html',
+                activities.create_template_context('activity_paged', 'main_activity'),
+                context_instance=RequestContext(request))
+
+
 def robots(request):
     return HttpResponse("User-Agent: *\nDisallow:\nSitemap: http://%s/sitemap.xml" %
         Site.objects.get_current().domain,