Commit de0130e04688d2abd14df2d4431d0b4071ed29a3
1 parent
55f81ec7
Exists in
master
and in
39 other branches
Adding view to manage list subscription
Partialy implemented. Not working yet.
Showing
3 changed files
with
44 additions
and
5 deletions
Show diff stats
src/accounts/models.py
| ... | ... | @@ -26,12 +26,22 @@ 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): | |
| 29 | + def mailinglists(self, email=None): | |
| 30 | 30 | list_set = set() |
| 31 | - for email in self.emails.all(): | |
| 32 | - lists = requests.get(settings.MAILMAN_API_URL, timeout=1, | |
| 33 | - params={'address': email.address}) | |
| 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 | + except requests.exceptions.Timeout: | |
| 42 | + pass | |
| 34 | 43 | list_set.update(lists.json()) |
| 44 | + | |
| 35 | 45 | return tuple(list_set) |
| 36 | 46 | |
| 37 | 47 | ... | ... |
src/accounts/urls.py
| 1 | 1 | |
| 2 | 2 | from django.conf.urls import patterns, include, url |
| 3 | 3 | |
| 4 | -from .views import UserProfileDetailView, UserProfileUpdateView | |
| 4 | +from .views import UserProfileDetailView, UserProfileUpdateView, \ | |
| 5 | + ManageUserSubscriptionsView | |
| 5 | 6 | |
| 6 | 7 | |
| 7 | 8 | urlpatterns = patterns('', |
| ... | ... | @@ -12,4 +13,7 @@ urlpatterns = patterns('', |
| 12 | 13 | |
| 13 | 14 | url(r'^(?P<username>[\w@+.-]+)/edit/?$', |
| 14 | 15 | UserProfileUpdateView.as_view(), name='user_profile_update'), |
| 16 | + | |
| 17 | + url(r'^(?P<username>[\w@+.-]+)/subscriptions/?$', | |
| 18 | + ManageUserSubscriptionsView.as_view(), name='user_subscriptions'), | |
| 15 | 19 | ) | ... | ... |
src/accounts/views.py
| ... | ... | @@ -2,9 +2,11 @@ |
| 2 | 2 | # encoding: utf-8 |
| 3 | 3 | |
| 4 | 4 | import datetime |
| 5 | +import requests | |
| 5 | 6 | |
| 6 | 7 | from collections import OrderedDict |
| 7 | 8 | |
| 9 | +from django.conf import settings | |
| 8 | 10 | from django.contrib import messages |
| 9 | 11 | from django.db.models import Count |
| 10 | 12 | from django.contrib.auth import get_user_model |
| ... | ... | @@ -43,6 +45,7 @@ class UserProfileUpdateView(UserProfileBaseMixin, UpdateView): |
| 43 | 45 | |
| 44 | 46 | return obj |
| 45 | 47 | |
| 48 | + | |
| 46 | 49 | class UserProfileDetailView(UserProfileBaseMixin, DetailView): |
| 47 | 50 | template_name = 'accounts/user_detail.html' |
| 48 | 51 | |
| ... | ... | @@ -121,3 +124,25 @@ def signup(request): |
| 121 | 124 | 'Profiles not validated are deleted in 24h.')) |
| 122 | 125 | |
| 123 | 126 | return redirect('user_profile', username=user.username) |
| 127 | + | |
| 128 | + | |
| 129 | +class ManageUserSubscriptionsView(UserProfileBaseMixin, DetailView): | |
| 130 | + http_method_names = [u'get', u'post'] | |
| 131 | + template_name = u'accounts/manage_subscriptions.html' | |
| 132 | + | |
| 133 | + def post(self, request, *args, **kwargs): | |
| 134 | + user = self.get_object() | |
| 135 | + 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() | |
| 140 | + return self.get(request, *args, **kwargs) | |
| 141 | + | |
| 142 | + def get_context_data(self, **kwargs): | |
| 143 | + context = {} | |
| 144 | + resp = requests.get(settings.MAILMAN_API_URL) | |
| 145 | + context['lists'] = resp.json() | |
| 146 | + | |
| 147 | + context.update(kwargs) | |
| 148 | + return super(ManageUserSubscriptionsView, self).get_context_data(**context) | ... | ... |