From f6b481546fbfc433d319cb626bd680248917b2ed Mon Sep 17 00:00:00 2001 From: Luke Faraone Date: Wed, 5 Sep 2012 17:38:20 -0400 Subject: [PATCH] Offer to subscribe / create classes if user zwrites to a non-subbed class Here we check if a class exists. If not, we prompt the user to create, sub, and send his message to the class. If the class exists but we're not subbed we prompt the user to sub. This commit also added a decorator to views.py and refactored out some redundant code. (imported from commit 7234ef6c080f2a6de6ff0922635dddd90032f7fe) --- humbug/urls.py | 3 ++- templates/zephyr/index.html | 10 ++++++++ zephyr/static/js/zephyr.js | 42 +++++++++++++++++++++++++++++++++ zephyr/static/styles/zephyr.css | 12 ++++++++++ zephyr/views.py | 25 +++++++++++--------- 5 files changed, 80 insertions(+), 12 deletions(-) diff --git a/humbug/urls.py b/humbug/urls.py index fcbda639d9..28e52761e3 100644 --- a/humbug/urls.py +++ b/humbug/urls.py @@ -19,7 +19,8 @@ urlpatterns = patterns('', 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/')}) + {'document_root': os.path.join(settings.SITE_ROOT, '..', 'zephyr', 'static/')}), + url(r'^subscriptions/exists/(?P.*)$', 'zephyr.views.class_exists', name='class_exists'), # Uncomment the admin/doc line below to enable admin documentation: # url(r'^admin/doc/', include('django.contrib.admindocs.urls')), diff --git a/templates/zephyr/index.html b/templates/zephyr/index.html index 885513b5ce..25732dbdac 100644 --- a/templates/zephyr/index.html +++ b/templates/zephyr/index.html @@ -51,6 +51,16 @@ var people_list = {{ people }};
+
+

The class does not exist.

+ Create and send + Cancel message +
+
+

You're not subscribed to the class .

+ Subscribe and send + Cancel message +
diff --git a/zephyr/static/js/zephyr.js b/zephyr/static/js/zephyr.js index 8dacf901ff..e4035c604e 100644 --- a/zephyr/static/js/zephyr.js +++ b/zephyr/static/js/zephyr.js @@ -42,6 +42,12 @@ $.ajaxSetup({ } }); +function sub(zephyr_class) { + // Supports multiple classes, separate by commas. + // TODO: check the return value and handle an error condition + $.post('/subscriptions/add/', {new_subscriptions: zephyr_class}); +} + $(function() { var status_classes = 'alert-error alert-success alert-info'; var send_status = $('#send-status'); @@ -56,6 +62,42 @@ $(function() { .stop(true).fadeTo(0,1); buttons.attr('disabled', 'disabled'); buttons.blur() + var okay = true; + $.ajaxSetup({async:false}); // so we get blocking gets + $.get("subscriptions/exists/" + $("#class").val(), function(data) { + if (data == "False") { + // The class doesn't exist + okay = false; + send_status.removeClass(status_classes) + send_status.toggle(); + $('#class-dne-name').text($("#class").val()); + $('#class-dne').show(); + $('#create-it').focus() + .click(function() { + sub($("#class").val()); + $("#class-message form").ajaxSubmit(); + $('#class-dne').stop(true).fadeOut(500); + }); + buttons.removeAttr('disabled'); + } + }); + $.ajaxSetup({async:true}); + if (okay && class_list.indexOf($("#class").val()) == -1) { + // You're not subbed to the class + okay = false; + send_status.removeClass(status_classes); + send_status.toggle(); + $('#class-nosub-name').text($("#class").val()); + $('#class-nosub').show(); + $('#sub-it').focus() + .click(function() { + sub($("#class").val()); + $("#class-message form").ajaxSubmit(); + $('#class-nosub').stop(true).fadeOut(500); + }); + buttons.removeAttr('disabled'); + } + return okay; }, success: function (resp, statusText, xhr, form) { form.find('textarea').val(''); diff --git a/zephyr/static/styles/zephyr.css b/zephyr/static/styles/zephyr.css index 06c66f711d..f61dcd5ab7 100644 --- a/zephyr/static/styles/zephyr.css +++ b/zephyr/static/styles/zephyr.css @@ -90,6 +90,18 @@ form.zephyr textarea { display: none; } +#class-dne { + display: none; +} + +#class-nosub { + display: none; +} + +span.classname { + font-weight: bold; +} + #connection-error .alert { margin-bottom: auto; } diff --git a/zephyr/views.py b/zephyr/views.py index dd10574196..778d16501a 100644 --- a/zephyr/views.py +++ b/zephyr/views.py @@ -20,6 +20,13 @@ import datetime import simplejson import socket +def require_post(view_func): + def _wrapped_view_func(request, *args, **kwargs): + if request.method != "POST": + return HttpResponseBadRequest('This form can only be submitted by POST.') + return view_func(request, *args, **kwargs) + return _wrapped_view_func + def json_response(res_type="success", msg="", status=200): return HttpResponse(content=simplejson.dumps({"result":res_type, "msg":msg}), mimetype='application/json', status=status) @@ -95,10 +102,8 @@ def home(request): context_instance=RequestContext(request)) @login_required +@require_post def update(request): - if not request.POST: - # Do something - pass user = request.user user_profile = UserProfile.objects.get(user=user) if request.POST.get('pointer'): @@ -107,11 +112,8 @@ def update(request): return json_success() @asynchronous +@require_post def get_updates_longpoll(request, handler): - if not request.POST: - # TODO: Do something - pass - last_received = request.POST.get('last_received') if not last_received: # TODO: return error? @@ -132,11 +134,8 @@ def get_updates_longpoll(request, handler): user_profile.add_callback(handler.async_callback(on_receive), last_received) @login_required +@require_post def zephyr(request): - if not request.POST: - # TODO: Do something - pass - user_profile = UserProfile.objects.get(user=request.user) zephyr_type = request.POST["type"] if zephyr_type == 'class': @@ -262,3 +261,7 @@ def add_subscriptions(request): new_subscription.save() return HttpResponseRedirect(reverse('zephyr.views.subscriptions')) + +@login_required +def class_exists(request, zephyr_class): + return HttpResponse(bool(ZephyrClass.objects.filter(name=zephyr_class)))