Commit 46870deb79911d876487a0b490d1e1a0c09ed5a5
1 parent
ddae787b
Exists in
master
and in
39 other branches
View to manage list subscriptions
Showing
4 changed files
with
139 additions
and
26 deletions
Show diff stats
src/accounts/models.py
1 | 1 | |
2 | 2 | import urlparse |
3 | -import requests | |
4 | 3 | |
5 | 4 | from django.db import models |
6 | -from django.conf import settings | |
7 | 5 | from django.contrib.auth.models import AbstractUser |
8 | 6 | from django.core.urlresolvers import reverse |
9 | 7 | |
8 | +from .utils import mailman | |
9 | + | |
10 | 10 | |
11 | 11 | class User(AbstractUser): |
12 | 12 | institution = models.CharField(max_length=128, null=True, blank=True) |
... | ... | @@ -26,23 +26,11 @@ class User(AbstractUser): |
26 | 26 | def facebook_link(self): |
27 | 27 | return urlparse.urljoin('https://www.facebook.com', self.facebook) |
28 | 28 | |
29 | - def mailinglists(self, email=None): | |
30 | - list_set = set() | |
31 | - | |
32 | - if not email: | |
33 | - emails = self.emails.values_list('address', flat=True) | |
34 | - else: | |
35 | - emails = [email] | |
36 | - | |
37 | - for email in emails: | |
38 | - try: | |
39 | - lists = requests.get(settings.MAILMAN_API_URL, timeout=1, | |
40 | - params={'address': email}) | |
41 | - list_set.update(lists.json()) | |
42 | - except requests.exceptions.Timeout: | |
43 | - pass | |
29 | + def mailinglists(self): | |
30 | + return mailman.user_lists(self) | |
44 | 31 | |
45 | - return tuple(list_set) | |
32 | + def update_subscription(self, email, lists): | |
33 | + mailman.update_subscription(email, lists) | |
46 | 34 | |
47 | 35 | |
48 | 36 | # We need to have `email` field set as unique but Django does not | ... | ... |
src/accounts/templates/accounts/manage_subscriptions.html
0 → 100644
... | ... | @@ -0,0 +1,43 @@ |
1 | +{% extends 'base.html' %} | |
2 | +{% load i18n %} | |
3 | + | |
4 | +{% block main-content %} | |
5 | + | |
6 | + <br> | |
7 | + | |
8 | + <form method='post'> | |
9 | + {% csrf_token %} | |
10 | + | |
11 | + <div class="row"> | |
12 | + {% for email, lists in membership.items %} | |
13 | + <div class="col-lg-3 col-md-4 col-sm-6 col-xs-12"> | |
14 | + <div class="panel panel-default"> | |
15 | + <div class="panel-heading"> | |
16 | + <h3 class="panel-title">{{ email }}</h3> | |
17 | + </div> | |
18 | + <div class="panel-body"> | |
19 | + {% for list, checked in lists %} | |
20 | + <div class="checkbox"> | |
21 | + <label> | |
22 | + <input name="{{ email }}" value="{{ list }}" type="checkbox" {% if checked %}checked{% endif%}>{{ list }}</input> | |
23 | + </label> | |
24 | + </div> | |
25 | + {% endfor %} | |
26 | + </div> | |
27 | + </div> | |
28 | + </div> | |
29 | + {% endfor %} | |
30 | + </div> | |
31 | + | |
32 | + <div class="row"> | |
33 | + <div class="text-center"> | |
34 | + <button class="btn btn-lg btn-primary" type="submit">{% trans 'Update subscriptions' %}</button> | |
35 | + </div> | |
36 | + </div> | |
37 | + | |
38 | + </form> | |
39 | + | |
40 | + <br><br> | |
41 | + <br><br> | |
42 | + | |
43 | +{% endblock %} | ... | ... |
... | ... | @@ -0,0 +1,68 @@ |
1 | + | |
2 | +import urlparse | |
3 | +import requests | |
4 | + | |
5 | +from django.conf import settings | |
6 | + | |
7 | +TIMEOUT = 1 | |
8 | + | |
9 | + | |
10 | +def get_url(listname=None): | |
11 | + if listname: | |
12 | + return urlparse.urljoin(settings.MAILMAN_API_URL, '/' + listname) | |
13 | + | |
14 | + return settings.MAILMAN_API_URL | |
15 | + | |
16 | + | |
17 | +def subscribe(listname, address): | |
18 | + url = get_url(listname) | |
19 | + try: | |
20 | + requests.put(url, timeout=TIMEOUT, data={'address': address}) | |
21 | + except requests.exceptions.RequestException: | |
22 | + return False | |
23 | + return True | |
24 | + | |
25 | + | |
26 | +def unsubscribe(listname, address): | |
27 | + url = get_url(listname) | |
28 | + try: | |
29 | + requests.delete(url, timeout=TIMEOUT, data={'address': address}) | |
30 | + except requests.exceptions.RequestException: | |
31 | + return False | |
32 | + return True | |
33 | + | |
34 | + | |
35 | +def update_subscription(address, lists): | |
36 | + current_lists = address_lists(address) | |
37 | + print lists | |
38 | + | |
39 | + for maillist in current_lists: | |
40 | + if maillist not in lists: | |
41 | + unsubscribe(maillist, address) | |
42 | + | |
43 | + for maillist in lists: | |
44 | + if maillist not in current_lists: | |
45 | + subscribe(maillist, address) | |
46 | + | |
47 | + | |
48 | +def address_lists(address): | |
49 | + url = get_url() | |
50 | + try: | |
51 | + lists = requests.get(url, timeout=TIMEOUT, params={'address': address}) | |
52 | + except requests.exceptions.RequestException: | |
53 | + return [] | |
54 | + | |
55 | + return lists.json() | |
56 | + | |
57 | + | |
58 | +def all_lists(): | |
59 | + return address_lists('') | |
60 | + | |
61 | + | |
62 | +def user_lists(user): | |
63 | + list_set = set() | |
64 | + | |
65 | + for email in user.emails.values_list('address', flat=True): | |
66 | + list_set.update(address_lists(email)) | |
67 | + | |
68 | + return tuple(list_set) | ... | ... |
src/accounts/views.py
... | ... | @@ -2,11 +2,9 @@ |
2 | 2 | # encoding: utf-8 |
3 | 3 | |
4 | 4 | import datetime |
5 | -import requests | |
6 | 5 | |
7 | 6 | from collections import OrderedDict |
8 | 7 | |
9 | -from django.conf import settings | |
10 | 8 | from django.contrib import messages |
11 | 9 | from django.db.models import Count |
12 | 10 | from django.contrib.auth import get_user_model |
... | ... | @@ -22,6 +20,7 @@ from super_archives.models import EmailAddress, Message |
22 | 20 | from super_archives.utils.email import send_email_lists |
23 | 21 | from search.utils import trans |
24 | 22 | from .forms import UserCreationForm, ListsForm, UserUpdateForm |
23 | +from .utils import mailman | |
25 | 24 | |
26 | 25 | |
27 | 26 | class UserProfileBaseMixin(object): |
... | ... | @@ -133,16 +132,31 @@ class ManageUserSubscriptionsView(UserProfileBaseMixin, DetailView): |
133 | 132 | def post(self, request, *args, **kwargs): |
134 | 133 | user = self.get_object() |
135 | 134 | for email in user.emails.values_list('address', flat=True): |
136 | - current_lists = user.mailinglists(email) | |
137 | - #url = urlparse.urljoin(settings.MAILMAN_API_URL, | |
138 | - # ) | |
139 | - #requests.put() | |
135 | + lists = self.request.POST.getlist(email) | |
136 | + user.update_subscription(email, lists) | |
137 | + | |
140 | 138 | return self.get(request, *args, **kwargs) |
141 | 139 | |
142 | 140 | def get_context_data(self, **kwargs): |
143 | 141 | context = {} |
144 | - resp = requests.get(settings.MAILMAN_API_URL) | |
145 | - context['lists'] = resp.json() | |
142 | + context['membership'] = {} | |
143 | + | |
144 | + user = self.get_object() | |
145 | + emails = user.emails.values_list('address', flat=True) | |
146 | + all_lists = mailman.all_lists() | |
147 | + | |
148 | + for email in emails: | |
149 | + lists = [] | |
150 | + lists_for_address = mailman.address_lists(email) | |
151 | + for listname in all_lists: | |
152 | + if listname in lists_for_address: | |
153 | + checked = True | |
154 | + else: | |
155 | + checked = False | |
156 | + lists.append((listname, checked)) | |
157 | + | |
158 | + context['membership'].update({email: lists}) | |
146 | 159 | |
147 | 160 | context.update(kwargs) |
161 | + | |
148 | 162 | return super(ManageUserSubscriptionsView, self).get_context_data(**context) | ... | ... |