diff gcalendar/views.py @ 817:74e84f5fc948

For issue #72, paginate calendar events.
author Brian Neal <bgneal@gmail.com>
date Wed, 10 Sep 2014 19:38:55 -0500
parents 9e803323a0d0
children 68c3343f3318
line wrap: on
line diff
--- a/gcalendar/views.py	Wed Sep 10 19:21:59 2014 -0500
+++ b/gcalendar/views.py	Wed Sep 10 19:38:55 2014 -0500
@@ -4,6 +4,7 @@
 
 from django.contrib.auth.decorators import login_required
 from django.core.urlresolvers import reverse
+from django.core.paginator import Paginator, PageNotAnInteger, EmptyPage
 from django.http import HttpResponse
 from django.http import HttpResponseBadRequest
 from django.http import HttpResponseForbidden
@@ -59,9 +60,19 @@
 
 @login_required
 def edit_events(request):
-    events = Event.objects.filter(user=request.user, status=Event.ON_CAL).order_by('start_date')
+    events = Event.objects.filter(user=request.user, status=Event.ON_CAL).\
+            order_by('-start_date')
+    paginator = Paginator(events, 25)
+    num = request.GET.get('page')
+    try:
+        page = paginator.page(num)
+    except PageNotAnInteger:
+        page = paginator.page(1)
+    except EmptyPage:
+        page = paginator.page(paginator.num_pages)
+
     return render_to_response('gcalendar/edit.html', {
-        'events': events,
+        'page': page,
         },
         context_instance = RequestContext(request))