From 6026c80de646bee94f36328d8e024c8295191bbd Mon Sep 17 00:00:00 2001 From: Tim Abbott Date: Fri, 16 Nov 2012 14:15:03 -0500 Subject: [PATCH] API: Add unsubscribe function. (imported from commit 6dc55e9030770500770ce3921a4e77499d64f2d6) --- api/common.py | 4 ++++ api/examples/unsubscribe | 41 ++++++++++++++++++++++++++++++++++++++++ humbug/urls.py | 1 + zephyr/tests.py | 1 + zephyr/views.py | 4 ++++ 5 files changed, 51 insertions(+) create mode 100755 api/examples/unsubscribe diff --git a/api/common.py b/api/common.py index 8902748ea7..8bfc0e74c7 100644 --- a/api/common.py +++ b/api/common.py @@ -110,6 +110,10 @@ class HumbugAPI(): request = {'subscriptions': streams} return self.do_api_query(request, "/api/v1/subscriptions/add") + def remove_subscriptions(self, streams): + request = {'subscriptions': streams} + return self.do_api_query(request, "/api/v1/subscriptions/remove") + def call_on_each_message(self, callback, options = {}): max_message_id = None while True: diff --git a/api/examples/unsubscribe b/api/examples/unsubscribe new file mode 100755 index 0000000000..2b8ad1cf6b --- /dev/null +++ b/api/examples/unsubscribe @@ -0,0 +1,41 @@ +#!/usr/bin/python +import sys +import os +import optparse + +usage = """unsubscribe --user= [options] --streams= + +Ensures the user is not subscribed to the listed streams. + +Example: unsubscribe --user=tabbott@humbughq.com --site=http://127.0.0.1:8000 +""" +parser = optparse.OptionParser(usage=usage) +parser.add_option('--site', + dest='site', + default="https://humbughq.com", + action='store') +parser.add_option('--api-key', + dest='api_key', + default="xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", + action='store') +parser.add_option('--streams', + dest='streams', + default="", + action='store') +parser.add_option('--user', + dest='user', + action='store') +(options, args) = parser.parse_args() + +sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(__file__)))) +import api.common +client = api.common.HumbugAPI(email=options.user, + api_key=options.api_key, + verbose=True, + site=options.site) + +if options.streams == "": + print >>sys.stderr, "Usage:", parser.usage + sys.exit(1) + +print client.remove_subscriptions(options.streams.split()) diff --git a/humbug/urls.py b/humbug/urls.py index 1db64c6652..f62da6e4cb 100644 --- a/humbug/urls.py +++ b/humbug/urls.py @@ -52,6 +52,7 @@ urlpatterns = patterns('', url(r'^api/v1/get_public_streams$', 'zephyr.views.api_get_public_streams'), url(r'^api/v1/subscriptions/list$', 'zephyr.views.api_list_subscriptions'), url(r'^api/v1/subscriptions/add$', 'zephyr.views.api_add_subscriptions'), + url(r'^api/v1/subscriptions/remove$', 'zephyr.views.api_remove_subscriptions'), url(r'^api/v1/send_message$', 'zephyr.views.api_send_message'), url(r'^api/v1/update_pointer$', 'zephyr.views.api_update_pointer'), diff --git a/zephyr/tests.py b/zephyr/tests.py index 5da9068a8d..35fe490b95 100644 --- a/zephyr/tests.py +++ b/zephyr/tests.py @@ -123,6 +123,7 @@ class PublicURLTest(TestCase): "/api/v1/get_public_streams", "/api/v1/subscriptions/list", "/api/v1/subscriptions/add", + "/api/v1/subscriptions/remove", "/api/v1/send_message", "/api/v1/fetch_api_key", "/json/fetch_api_key", diff --git a/zephyr/views.py b/zephyr/views.py index a760083c5f..875b634336 100644 --- a/zephyr/views.py +++ b/zephyr/views.py @@ -708,6 +708,10 @@ def api_list_subscriptions(request, user_profile): def json_list_subscriptions(request, user_profile): return json_success({"subscriptions": gather_subscriptions(user_profile)}) +@authenticated_api_view +def api_remove_subscriptions(request, user_profile): + return remove_subscriptions_backend(request, user_profile) + @authenticated_json_view def json_remove_subscriptions(request, user_profile): return remove_subscriptions_backend(request, user_profile)