From bb6f533da8762bf11a438d5d8aa9e22d1f4e2a9b Mon Sep 17 00:00:00 2001 From: Matthew Schinckel Date: Thu, 25 Oct 2012 09:42:32 +1030 Subject: [PATCH] Use urls with namespaces. Use request.user.notifications instead of Notifications.objects... Use render() instead of render_to_response() --- notifications/views.py | 28 ++++++++++++++++++---------- 1 file changed, 18 insertions(+), 10 deletions(-) diff --git a/notifications/views.py b/notifications/views.py index 68922eb..083b112 100644 --- a/notifications/views.py +++ b/notifications/views.py @@ -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')