Permalink
Browse files

Use urls with namespaces.

Use request.user.notifications instead of Notifications.objects...
Use render() instead of render_to_response()
  • Loading branch information...
1 parent 182be42 commit bb6f533da8762bf11a438d5d8aa9e22d1f4e2a9b @schinckel schinckel committed Oct 24, 2012
Showing with 18 additions and 10 deletions.
  1. +18 −10 notifications/views.py
View
@@ -2,17 +2,20 @@
from django.contrib.auth.decorators import login_required
from django.contrib.auth.models import User
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
-from django.shortcuts import get_object_or_404, render_to_response, redirect
+from django.shortcuts import get_object_or_404, render, redirect
from django.template.context import RequestContext
from .utils import slug2id
from notifications.models import Notification
@login_required
-def list(request):
+def all(request):
"""
Index page for authenticated user
"""
- actions = Notification.objects.filter(recipient=request.user)
+ return render(request, 'notifications/list.html', {
+ 'notifications': request.user.notifications.all()
+ })
+ actions = request.user.notifications.all()
paginator = Paginator(actions, 16) # Show 16 notifications per page
page = request.GET.get('p')
@@ -25,20 +28,25 @@ def list(request):
except EmptyPage:
# If page is out of range (e.g. 9999), deliver last page of results.
action_list = paginator.page(paginator.num_pages)
-
+
return render_to_response('notifications/list.html', {
'member': request.user,
'action_list': action_list,
}, context_instance=RequestContext(request))
@login_required
-def read_all(request):
- Notification.objects.mark_all_as_read(request.user)
-
- return redirect('notifications_list')
+def unread(request):
+ return render(request, 'notifications/list.html', {
+ 'notifications': request.user.notifications.unread()
+ })
+
+@login_required
+def mark_all_as_read(request):
+ request.user.notifications.mark_all_as_read()
+ return redirect('notifications:all')
@login_required
-def read(request, slug=None):
+def mark_as_read(request, slug=None):
id = slug2id(slug)
notification = get_object_or_404(Notification, recipient=request.user, id=id)
@@ -49,4 +57,4 @@ def read(request, slug=None):
if next:
return redirect(next)
- return redirect('notifications_list')
+ return redirect('notifications:all')

0 comments on commit bb6f533

Please sign in to comment.