diff --git a/src/postorius/forms.py b/src/postorius/forms.py
index 9579c91..01b57c3 100644
--- a/src/postorius/forms.py
+++ b/src/postorius/forms.py
@@ -921,3 +921,14 @@
if email == user_email:
raise forms.ValidationError(_('Please provide a different email address than your own.'))
return cleaned_data
+
+class ChangeSubscriptionForm(forms.Form):
+ email = forms.ChoiceField()
+
+ def __init__(self, user_emails, *args, **kwargs):
+ super(ChangeSubscriptionForm, self).__init__(*args, **kwargs)
+ self.fields['email'] = forms.ChoiceField(
+ label=_('Select Email'),
+ required=False,
+ widget=forms.Select(),
+ choices=((address, address) for address in user_emails))
diff --git a/src/postorius/templates/postorius/lists/summary.html b/src/postorius/templates/postorius/lists/summary.html
index db6a435..80cfcee 100644
--- a/src/postorius/templates/postorius/lists/summary.html
+++ b/src/postorius/templates/postorius/lists/summary.html
@@ -20,6 +20,13 @@
{% trans "You are subscribed to this list with your address:" %}
{{ subscribed_address }}
+
Change Subscription
+
+ Unsubscribe
{% trans "Unsubscribe" %} {{ subscribed_address }}
{% else %}
{% trans 'Subscribe to this list' %}
diff --git a/src/postorius/tests/test_forms.py b/src/postorius/tests/test_forms.py
index 091c92f..b58a9ab 100644
--- a/src/postorius/tests/test_forms.py
+++ b/src/postorius/tests/test_forms.py
@@ -17,7 +17,7 @@
from django.utils import unittest
from postorius.forms import (
- UserPreferences, DomainNew, ListSubscribe)
+ UserPreferences, DomainNew, ListSubscribe, ChangeSubscriptionForm)
class UserPreferencesTest(unittest.TestCase):
@@ -76,3 +76,16 @@
'display_name' : 'Someone',
})
self.assertFalse(form.is_valid())
+
+class ChangeSubscriptionTest(unittest.TestCase):
+ def test_subscription_changes_only_to_user_addresses(self):
+ user_emails = ['one@example.com', 'two@example.com']
+ form = ChangeSubscriptionForm(user_emails,
+ {'email': 'abcd@d.com'})
+ self.assertFalse(form.is_valid())
+
+ def test_subscription_works(self):
+ user_emails = ['one@example.com', 'two@example.com']
+ form = ChangeSubscriptionForm(user_emails,
+ {'email': 'two@example.com'})
+ self.assertTrue(form.is_valid())
diff --git a/src/postorius/urls.py b/src/postorius/urls.py
index 68341b2..971481b 100644
--- a/src/postorius/urls.py
+++ b/src/postorius/urls.py
@@ -44,6 +44,9 @@
url(r'^subscribe$',
ListSubscribeView.as_view(
), name='list_subscribe'),
+ url(r'^change_subscription$',
+ ChangeSubscriptionView.as_view(),
+ name='change_subscription'),
url(r'^unsubscribe/(?P[^/]+)$',
ListUnsubscribeView.as_view(
), name='list_unsubscribe'),
diff --git a/src/postorius/views/list.py b/src/postorius/views/list.py
index 87ceaa8..867d0c3 100644
--- a/src/postorius/views/list.py
+++ b/src/postorius/views/list.py
@@ -177,31 +177,74 @@
try:
mm_user = MailmanUser.objects.get(address=request.user.email)
user_emails = [str(address) for address in getattr(mm_user, 'addresses')]
- # TODO:maxking - add the clause below in above
- # statement after the subscription policy is sorted
- # out
- # if address.verified_on is not None]
+ # TODO:maxking - add the clause below in above
+ # statement after the subscription policy is sorted out
+ # if address.verified_on is not None]
+ except AttributeError:
+ # Anonymous User, everyone logged out.
+ user_emails = None
except Mailman404Error:
# The user does not have a mailman user associated with it.
user_emails = [request.user.email]
userSubscribed = False
subscribed_address = None
- for address in user_emails:
- try:
- userMember = self.mailing_list.get_member(address)
- except ValueError:
- pass
- else:
- userSubscribed = True
- subscribed_address = address
+ if user_emails is not None:
+ for address in user_emails:
+ try:
+ userMember = self.mailing_list.get_member(address)
+ except ValueError:
+ pass
+ else:
+ userSubscribed = True
+ subscribed_address = address
+ data = {'list': self.mailing_list,
+ 'userSubscribed': userSubscribed,
+ 'subscribed_address': subscribed_address}
+ if user_emails is not None:
+ data['change_subscription_form'] = ChangeSubscriptionForm(user_emails,
+ initial={'email': subscribed_address})
+ data['subscribe_form'] = ListSubscribe(user_emails),
+ else:
+ data['change_subscription_form'] = None
return render_to_response(
- 'postorius/lists/summary.html',
- {'list': self.mailing_list,
- 'subscribe_form': ListSubscribe(user_emails),
- 'userSubscribed': userSubscribed,
- 'subscribed_address': subscribed_address},
+ 'postorius/lists/summary.html', data,
context_instance=RequestContext(request))
+class ChangeSubscriptionView(MailingListView):
+ """Change mailing list subscription
+ """
+
+ @method_decorator(login_required)
+ def post(self, request, list_id):
+ try:
+ mm_user = MailmanUser.objects.get(address=request.user.email)
+ user_emails = [str(address) for address in mm_user.addresses]
+ form = ListSubscribe(user_emails, request.POST)
+ for address in user_emails:
+ try:
+ userMember = self.mailing_list.get_member(address)
+ except ValueError:
+ pass
+ else:
+ userSubscribed = True
+ old_email = address
+ if form.is_valid():
+ email = form.cleaned_data['email']
+ if old_email == email:
+ messages.error(request, 'You are already subscribed')
+ else:
+ self.mailing_list.unsubscribe(old_email)
+ self.mailing_list.subscribe(email)
+ messages.success(request,
+ 'Subscription changed to {} address'.format(email))
+ else:
+ messages.error(request, 'Something went wrong. '
+ 'Please try again.')
+ except MailmanApiError:
+ return utils.render_api_error(request)
+ except HTTPError, e:
+ messages.error(request, e.msg)
+ return redirect('list_summary', self.mailing_list.list_id)
class ListSubscribeView(MailingListView):