Commit 6002dcdb490011e42869bd513544db0f563ed515
1 parent
f3b2dec1
Added support for mailman apiv2
Showing
2 changed files
with
19 additions
and
14 deletions
Show diff stats
colab/accounts/utils/mailman.py
@@ -27,15 +27,15 @@ MAILMAN_MSGS = { | @@ -27,15 +27,15 @@ MAILMAN_MSGS = { | ||
27 | } | 27 | } |
28 | 28 | ||
29 | 29 | ||
30 | -def get_url(listname=None): | 30 | +def get_url(path, listname=None): |
31 | + url = urlparse.urljoin(settings.MAILMAN_API_URL, path) | ||
31 | if listname: | 32 | if listname: |
32 | - return urlparse.urljoin(settings.MAILMAN_API_URL, listname) | ||
33 | - | ||
34 | - return settings.MAILMAN_API_URL | 33 | + return urlparse.urljoin(url, listname) |
34 | + return url | ||
35 | 35 | ||
36 | 36 | ||
37 | def subscribe(listname, address): | 37 | def subscribe(listname, address): |
38 | - url = get_url(listname) | 38 | + url = get_url('subscribe/', listname=listname) |
39 | try: | 39 | try: |
40 | result = requests.put(url, timeout=TIMEOUT, data={'address': address}) | 40 | result = requests.put(url, timeout=TIMEOUT, data={'address': address}) |
41 | msg_type, message = MAILMAN_MSGS[result.json()] | 41 | msg_type, message = MAILMAN_MSGS[result.json()] |
@@ -46,7 +46,7 @@ def subscribe(listname, address): | @@ -46,7 +46,7 @@ def subscribe(listname, address): | ||
46 | 46 | ||
47 | 47 | ||
48 | def unsubscribe(listname, address): | 48 | def unsubscribe(listname, address): |
49 | - url = get_url(listname) | 49 | + url = get_url('subscribe/', listname) |
50 | try: | 50 | try: |
51 | result = requests.delete(url, timeout=TIMEOUT, data={'address': address}) | 51 | result = requests.delete(url, timeout=TIMEOUT, data={'address': address}) |
52 | msg_type, message = MAILMAN_MSGS[result.json()] | 52 | msg_type, message = MAILMAN_MSGS[result.json()] |
@@ -57,7 +57,7 @@ def unsubscribe(listname, address): | @@ -57,7 +57,7 @@ def unsubscribe(listname, address): | ||
57 | 57 | ||
58 | 58 | ||
59 | def update_subscription(address, lists): | 59 | def update_subscription(address, lists): |
60 | - current_lists = mailing_lists(address=address) | 60 | + current_lists = mailing_lists(address=address, names_only=True) |
61 | info_messages = [] | 61 | info_messages = [] |
62 | 62 | ||
63 | for maillist in current_lists: | 63 | for maillist in current_lists: |
@@ -72,16 +72,21 @@ def update_subscription(address, lists): | @@ -72,16 +72,21 @@ def update_subscription(address, lists): | ||
72 | 72 | ||
73 | 73 | ||
74 | def mailing_lists(**kwargs): | 74 | def mailing_lists(**kwargs): |
75 | - url = get_url() | ||
76 | - path = 'lists/' | 75 | + url = get_url('lists/') |
77 | 76 | ||
78 | try: | 77 | try: |
79 | - lists = requests.get(url + path, timeout=TIMEOUT, params=kwargs) | 78 | + lists = requests.get(url, timeout=TIMEOUT, params=kwargs) |
80 | except: | 79 | except: |
81 | LOGGER.exception('Unable to list mailing lists') | 80 | LOGGER.exception('Unable to list mailing lists') |
82 | return [] | 81 | return [] |
83 | 82 | ||
84 | - return lists.json() | 83 | + if 'names_only' in kwargs and kwargs['names_only']: |
84 | + names_only = [] | ||
85 | + for l in lists.json(): | ||
86 | + names_only.append(l['listname']) | ||
87 | + return names_only | ||
88 | + else: | ||
89 | + return lists.json() | ||
85 | 90 | ||
86 | 91 | ||
87 | def is_private_list(name): | 92 | def is_private_list(name): |
@@ -115,7 +120,7 @@ def get_list_description(listname, lists=None): | @@ -115,7 +120,7 @@ def get_list_description(listname, lists=None): | ||
115 | 120 | ||
116 | 121 | ||
117 | def list_users(listname): | 122 | def list_users(listname): |
118 | - url = get_url(listname) | 123 | + url = get_url('members/', listname) |
119 | 124 | ||
120 | params = {} | 125 | params = {} |
121 | 126 |
colab/accounts/views.py
@@ -153,11 +153,11 @@ class ManageUserSubscriptionsView(UserProfileBaseMixin, DetailView): | @@ -153,11 +153,11 @@ class ManageUserSubscriptionsView(UserProfileBaseMixin, DetailView): | ||
153 | 153 | ||
154 | user = self.get_object() | 154 | user = self.get_object() |
155 | emails = user.emails.values_list('address', flat=True) | 155 | emails = user.emails.values_list('address', flat=True) |
156 | - all_lists = mailman.all_lists(description=True) | 156 | + all_lists = mailman.all_lists() |
157 | 157 | ||
158 | for email in emails: | 158 | for email in emails: |
159 | lists = [] | 159 | lists = [] |
160 | - lists_for_address = mailman.mailing_lists(address=email) | 160 | + lists_for_address = mailman.mailing_lists(address=email, names_only=True) |
161 | for mlist in all_lists: | 161 | for mlist in all_lists: |
162 | if mlist.get('listname') in lists_for_address: | 162 | if mlist.get('listname') in lists_for_address: |
163 | checked = True | 163 | checked = True |