diff --git a/humbug/urls.py b/humbug/urls.py index 7bf17d01b5..8e83845e06 100644 --- a/humbug/urls.py +++ b/humbug/urls.py @@ -18,6 +18,7 @@ urlpatterns = patterns('', 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'^subscriptions/add/$', 'zephyr.views.add_subscriptions', name='add_subscriptions'), url(r'^static/(?P.*)$', 'django.views.static.serve', {'document_root': os.path.join(settings.SITE_ROOT, '..', 'zephyr', 'static/')}) diff --git a/templates/zephyr/subscriptions.html b/templates/zephyr/subscriptions.html index fb48a13760..7d7ffbbf48 100644 --- a/templates/zephyr/subscriptions.html +++ b/templates/zephyr/subscriptions.html @@ -34,4 +34,15 @@ +

Add new subscriptions

+
+
+
{% csrf_token %} + +
+ +
+
+
+ {% endblock %} diff --git a/zephyr/views.py b/zephyr/views.py index 1266ba568c..5902f47932 100644 --- a/zephyr/views.py +++ b/zephyr/views.py @@ -162,9 +162,48 @@ def manage_subscriptions(request): unsubs = request.POST.getlist('subscription') for sub_name in unsubs: zephyr_class = ZephyrClass.objects.get(name=sub_name) + recipient = Recipient.objects.get(user_or_class=zephyr_class.id, type="class") subscription = Subscription.objects.get( - userprofile_id=user_profile.id, recipient_id=zephyr_class.id) + userprofile_id=user_profile.id, recipient_id=recipient) subscription.active = False subscription.save() return HttpResponseRedirect(reverse('zephyr.views.subscriptions')) + +@login_required +def add_subscriptions(request): + if not request.POST: + # Do something reasonable. + return + user_profile = UserProfile.objects.get(user=request.user) + + new_subs = request.POST.get('new_subscriptions') + if not new_subs: + return HttpResponseRedirect(reverse('zephyr.views.subscriptions')) + + for sub_name in new_subs.split(","): + zephyr_class = ZephyrClass.objects.filter(name=sub_name) + if zephyr_class: + zephyr_class = zephyr_class[0] + else: + zephyr_class = ZephyrClass() + zephyr_class.name = sub_name + zephyr_class.save() + + recipient = Recipient() + recipient.user_or_class = zephyr_class.pk + recipient.type = "class" + recipient.save() + + subscription = Subscription.objects.filter(userprofile_id=user_profile, recipient_id=zephyr_class.id) + if subscription: + subscription = subscription[0] + subscription.active = True + subscription.save() + else: + new_subscription = Subscription() + new_subscription.userprofile_id = user_profile + new_subscription.recipient_id = recipient + new_subscription.save() + + return HttpResponseRedirect(reverse('zephyr.views.subscriptions'))