Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Split notices/notice_settings in two separate views to do less db que…

…ries. Also don't save notice settings when we don't need to.

Signed-off-by: Brian Rosner <brosner@gmail.com>
  • Loading branch information...
commit 879a0c70b3a8d2e9c95dde7addce76e046450c64 1 parent 6a1db8f
@diox diox authored brosner committed
Showing with 27 additions and 10 deletions.
  1. +3 −2 notification/urls.py
  2. +24 −8 notification/views.py
View
5 notification/urls.py
@@ -1,10 +1,11 @@
from django.conf.urls.defaults import *
-from notification.views import notices, mark_all_seen, feed_for_user, single
+from notification.views import notices, mark_all_seen, feed_for_user, single, notice_settings
urlpatterns = patterns('',
url(r'^$', notices, name="notification_notices"),
- url(r'^(\d+)/$', single, name="notification_notice"),
+ url(r'^settings$', notice_settings, name="notification_notice_settings"),
+ url(r'^(\d+)/$', single, name="notification_notice"),
url(r'^feed/$', feed_for_user, name="notification_feed_for_user"),
url(r'^mark_all_seen/$', mark_all_seen, name="notification_mark_all_seen"),
)
View
32 notification/views.py
@@ -31,6 +31,21 @@ def notices(request):
notices
A list of :model:`notification.Notice` objects that are not archived
and to be displayed on the site.
+ """
+ notices = Notice.objects.notices_for(request.user, on_site=True)
+
+ return render_to_response("notification/notices.html", {
+ "notices": notices,
+ }, context_instance=RequestContext(request))
+
+@login_required
+def notice_settings(request):
+ """
+ The notice settings view.
+
+ Template: :template:`notification/notice_settings.html`
+
+ Context:
notice_types
A list of all :model:`notification.NoticeType` objects.
@@ -44,19 +59,21 @@ def notices(request):
variable called ``form_label``, whose valid value is ``on``.
"""
notice_types = NoticeType.objects.all()
- notices = Notice.objects.notices_for(request.user, on_site=True)
settings_table = []
- for notice_type in NoticeType.objects.all():
+ for notice_type in notice_types:
settings_row = []
for medium_id, medium_display in NOTICE_MEDIA:
form_label = "%s_%s" % (notice_type.label, medium_id)
setting = get_notification_setting(request.user, notice_type, medium_id)
if request.method == "POST":
if request.POST.get(form_label) == "on":
- setting.send = True
+ if not setting.send:
+ setting.send = True
+ setting.save()
else:
- setting.send = False
- setting.save()
+ if setting.send:
+ setting.send = False
+ setting.save()
settings_row.append((form_label, setting.send))
settings_table.append({"notice_type": notice_type, "cells": settings_row})
@@ -65,8 +82,7 @@ def notices(request):
"rows": settings_table,
}
- return render_to_response("notification/notices.html", {
- "notices": notices,
+ return render_to_response("notification/notice_settings.html", {
"notice_types": notice_types,
"notice_settings": notice_settings,
}, context_instance=RequestContext(request))
@@ -164,4 +180,4 @@ def mark_all_seen(request):
notice.unseen = False
notice.save()
return HttpResponseRedirect(reverse("notification_notices"))
-
Please sign in to comment.
Something went wrong with that request. Please try again.