From 7e56e7a0f44f58fcc68c10f1af9b3f2e3f481b51 Mon Sep 17 00:00:00 2001 From: Jessica McKellar Date: Thu, 30 Aug 2012 12:04:35 -0400 Subject: [PATCH] Add a navigation link for managing subscriptions (for starters, just unsubbing). (imported from commit 23274a8790bf1f017e9c33db52bf746e291f23ba) --- humbug/urls.py | 2 ++ templates/zephyr/index.html | 3 ++- templates/zephyr/subscriptions.html | 37 +++++++++++++++++++++++++++++ zephyr/models.py | 2 +- zephyr/views.py | 30 ++++++++++++++++++++++- 5 files changed, 71 insertions(+), 3 deletions(-) create mode 100644 templates/zephyr/subscriptions.html diff --git a/humbug/urls.py b/humbug/urls.py index 5bf36f89c3..7bf17d01b5 100644 --- a/humbug/urls.py +++ b/humbug/urls.py @@ -16,6 +16,8 @@ urlpatterns = patterns('', url(r'^accounts/login/', 'django.contrib.auth.views.login', {'template_name': 'zephyr/login.html'}), url(r'^accounts/logout/', 'django.contrib.auth.views.logout', {'template_name': 'zephyr/index.html'}), url(r'^accounts/register/', 'zephyr.views.register', name='register'), + url(r'^subscriptions/$', 'zephyr.views.subscriptions', name='subscriptions'), + url(r'^subscriptions/manage/$', 'zephyr.views.manage_subscriptions', name='manage_subscriptions'), url(r'^static/(?P.*)$', 'django.views.static.serve', {'document_root': os.path.join(settings.SITE_ROOT, '..', 'zephyr', 'static/')}) diff --git a/templates/zephyr/index.html b/templates/zephyr/index.html index a10a9448ec..388fcc6ce7 100644 --- a/templates/zephyr/index.html +++ b/templates/zephyr/index.html @@ -1,7 +1,8 @@ {% extends "zephyr/base.html" %} {% block nav %} -
  • Log out
  • +
  • Log out
  • +
  • Manage subscriptions
  • {% endblock %} {% block content %} diff --git a/templates/zephyr/subscriptions.html b/templates/zephyr/subscriptions.html new file mode 100644 index 0000000000..fb48a13760 --- /dev/null +++ b/templates/zephyr/subscriptions.html @@ -0,0 +1,37 @@ +{% extends "zephyr/base.html" %} + +{% block nav %} +
  • Log out
  • +
  • Manage subscriptions
  • +{% endblock %} + +{% block content %} + + + +

    Current subscriptions

    +
    +
    +
    {% csrf_token %} + + + + + + + {% for subscription in subscriptions %} + + + + {% endfor %} +
    Unsubscribe?Class
    + + + {{ subscription }} +
    + +
    +
    +
    + +{% endblock %} diff --git a/zephyr/models.py b/zephyr/models.py index 816227931d..1e79512178 100644 --- a/zephyr/models.py +++ b/zephyr/models.py @@ -91,7 +91,7 @@ def send_zephyr(**kwargs): assert(len(recipients) == 1) elif zephyr.recipient.type == "class": recipients = [UserProfile.objects.get(user=s.userprofile_id) for - s in Subscription.objects.filter(recipient_id=zephyr.recipient)] + s in Subscription.objects.filter(recipient_id=zephyr.recipient, active=True)] else: raise for recipient in recipients: diff --git a/zephyr/views.py b/zephyr/views.py index 0e402cc876..1266ba568c 100644 --- a/zephyr/views.py +++ b/zephyr/views.py @@ -9,7 +9,7 @@ from django.utils.timezone import utc from django.contrib.auth.models import User from zephyr.models import Zephyr, UserProfile, ZephyrClass, Subscription, \ - Recipient, filter_by_subscriptions + Recipient, filter_by_subscriptions, get_display_recipient from zephyr.forms import RegistrationForm import tornado.web @@ -140,3 +140,31 @@ def zephyr(request): new_zephyr.save() return HttpResponse('') + +@login_required +def subscriptions(request): + userprofile = UserProfile.objects.get(user=request.user) + subscriptions = Subscription.objects.filter(userprofile_id=userprofile, active=True) + # For now, don't display the subscription for your ability to receive personals. + sub_names = [get_display_recipient(sub.recipient_id) for sub in subscriptions if sub.recipient_id.type != "personal"] + + return render_to_response('zephyr/subscriptions.html', + {'subscriptions': sub_names, 'user_profile': userprofile}, + context_instance=RequestContext(request)) + +@login_required +def manage_subscriptions(request): + if not request.POST: + # Do something reasonable. + return + user_profile = UserProfile.objects.get(user=request.user) + + unsubs = request.POST.getlist('subscription') + for sub_name in unsubs: + zephyr_class = ZephyrClass.objects.get(name=sub_name) + subscription = Subscription.objects.get( + userprofile_id=user_profile.id, recipient_id=zephyr_class.id) + subscription.active = False + subscription.save() + + return HttpResponseRedirect(reverse('zephyr.views.subscriptions'))