Commit ddd22835 authored by Aurélien Bompard's avatar Aurélien Bompard

Refactor the user views to limit code duplication

parent 1c1a759f
......@@ -726,6 +726,21 @@ class UserPreferences(forms.Form):
"""
Form handling the user's global, address and subscription based preferences
"""
def __init__(self, *args, **kwargs):
self._preferences = kwargs.pop('preferences', None)
super(UserPreferences, self).__init__(*args, **kwargs)
@property
def initial(self):
# Redirect to the preferences, this allows setting the preferences
# after instanciation and it will also set the initial data.
return self._preferences or {}
@initial.setter
def initial(self, value):
pass
choices = ((True, _('Yes')), (False, _('No')))
delivery_mode_choices = (("regular", _('Regular')),
......@@ -803,6 +818,34 @@ class UserPreferences(forms.Form):
"receive_list_copy", "receive_own_postings",
"delivery_mode", "delivery_status"]]
def save(self):
if not self.changed_data:
return
for key in self.changed_data:
if self.cleaned_data[key] is not None:
# None: nothing set yet. Remember to remove this test
# when Mailman accepts None as a "reset to default"
# value.
self._preferences[key] = self.cleaned_data[key]
self._preferences.save()
class UserPreferencesFormset(forms.BaseFormSet):
def __init__(self, *args, **kwargs):
self._preferences = kwargs.pop('preferences')
kwargs["initial"] = self._preferences
super(UserPreferencesFormset, self).__init__(*args, **kwargs)
def _construct_form(self, i, **kwargs):
form = super(UserPreferencesFormset, self)._construct_form(i, **kwargs)
form._preferences = self._preferences[i]
return form
def save(self):
for form in self.forms:
form.save()
class MemberModeration(forms.Form):
"""
......
......@@ -13,7 +13,7 @@
<div class="user-profile">
{% user_nav 'settings_address' 'Address-based settings' %}
{% if nolists %}
{% if formset|length == 0 %}
<h2>{% trans 'No preferences available' %}</h2>
<p>{% trans 'You are not yet subscribed to any lists, so you have no Mailman preferences.' %}</p>
{% else %}
......@@ -23,7 +23,7 @@
{{ formset.management_form }}
<div class="table-responsive">
<table class="table table-striped table-bordered">
{% for form,address in zipped_data %}
{% for form in formset %}
{% if forloop.first %}
<thead>
<tr>
......@@ -37,9 +37,9 @@
</thead >
{% endif %}
<tr>
<td>{{ address }}</td>
<td>{{ form.address }}</td>
{% for field in form %}
{% if field|fieldtype == 'RadioSelect' %}
{% if field|fieldtype_is:'RadioSelect' %}
<td class="text-center">
{% for choice in field %}
<label class="radio-inline">
......
......@@ -4,21 +4,28 @@
{% load bootstrap_tags %}
{% block head_title %}
{% trans 'Member options' %} | {{ list.fqdn_listname }} - {{ block.super }}
{% trans 'Member options' %} | {{ mlist.fqdn_listname }} - {{ block.super }}
{% endblock %}
{% block content %}
<h2>{% trans 'Subscription options for' %} {{ list.fqdn_listname }}</h2>
<hr />
<p class="col-md-offset-2">
{% trans 'Use this form to change the email used for this subscription' %}:
</p>
<form action="{% url 'change_subscription' list.list_id %}" method="post" class="form-horizontal">
{% bootstrap_form_horizontal change_subscription_form 3 8 'Change email used for subscription' %}
</form>
<hr />
<form action="" method="post" class="form-horizontal">
{% bootstrap_form_horizontal form 3 8 'Save changes' %}
<input type="hidden" name="formname" value="preferences" />
</form>
<div class="user-profile">
{% user_nav 'settings_list' 'Subscription settings' %}
<h4 class="margin-bottom">{% trans 'Subscription options for' %} <strong>{{ mlist.fqdn_listname }}</strong></h4>
<hr />
<p class="col-md-offset-2">
{% trans 'Use this form to change the email used for this subscription' %}:
</p>
<form action="{% url 'change_subscription' mlist.list_id %}" method="post" class="form-horizontal">
{% bootstrap_form_horizontal change_subscription_form 3 8 'Change email used for subscription' %}
</form>
<hr />
<form action="" method="post" class="form-horizontal">
{% bootstrap_form_horizontal form 3 8 'Save changes' %}
<input type="hidden" name="formname" value="preferences" />
</form>
</div>
{% endblock content %}
......@@ -19,7 +19,7 @@
<p>{% trans 'You are not yet subscribed to any lists, so you have no Mailman preferences.' %}</p>
{% else %}
<form action="{% url 'user_mailmansettings' %}" method="post" class="form-horizontal">
{% bootstrap_form_horizontal settingsform 3 8 'Save changes' %}
{% bootstrap_form_horizontal form 3 8 'Save changes' %}
</form>
{% endif %}
</div>
......
......@@ -12,7 +12,7 @@
<div class="user-profile">
{% user_nav 'settings_list' 'Subscription settings' %}
{% if not zipped_data or nolists %}
{% if formset|length == 0 %}
<div>
<h4>{% trans 'No preferences available' %}</h4>
</div>
......@@ -24,7 +24,7 @@
{{ formset.management_form }}
<div class="table-responsive">
<table class="table table-striped table-bordered">
{% for form, subscription in zipped_data %}
{% for form in formset %}
{% if forloop.first %}
<thead>
<tr>
......@@ -37,10 +37,14 @@
</tr >
</thead >
{% endif %}
<tr id="{{subscription.list_id}}">
<td>{{ subscription.list_id }}</td>
<tr id="{{ form.list_id }}">
<td>
<a href="{% url 'user_list_options' list_id=form.list_id %}">
{{ form.list_id }}
</a>
</td>
{% for field in form %}
{% if field|fieldtype == 'RadioSelect' %}
{% if field|fieldtype_is:'RadioSelect' %}
<td class="text-center">
{% for choice in field %}
<label class="radio-inline">
......
......@@ -61,7 +61,6 @@ class MailmanUserTest(ViewTestCase):
response = self.client.get(reverse('user_address_preferences'))
self.assertEqual(response.status_code, 200)
self.assertEqual(len(response.context["formset"]), 3)
self.assertEqual(len(response.context["zipped_data"]), 3)
def test_preferences_none(self):
# Mailman does not accept None values for boolean preferences. When
......@@ -129,8 +128,7 @@ class MailmanUserTest(ViewTestCase):
self.client.login(username='user', password='testpass')
response = self.client.get(reverse('user_mailmansettings'))
self.assertEquals(response.status_code, 200)
self.assertIsInstance(response.context['settingsform'],
UserPreferences)
self.assertIsInstance(response.context['form'], UserPreferences)
def test_presence_of_form_in_user_subscription_preferences(self):
self.client.login(username='user', password='testpass')
......@@ -138,8 +136,8 @@ class MailmanUserTest(ViewTestCase):
pre_confirmed=True, pre_approved=True)
response = self.client.get(reverse('user_subscription_preferences'))
self.assertEquals(response.status_code, 200)
self.assertIsNotNone(response.context['zipped_data'])
self.assertEquals(len(response.context['zipped_data']), 1)
self.assertIsNotNone(response.context['formset'])
self.assertEquals(len(response.context['formset']), 1)
def test_presence_of_form_in_user_list_options(self):
self.client.login(username='user', password='testpass')
......
......@@ -81,7 +81,7 @@ urlpatterns = [
user_views.UserMailmanSettingsView.as_view(),
name='user_mailmansettings'),
url(r'^accounts/list-options/(?P<list_id>[^/]+)/$',
user_views.user_list_options,
user_views.UserListOptionsView.as_view(),
name='user_list_options'),
# /domains/
url(r'^domains/$', domain_views.domain_index, name='domain_index'),
......
......@@ -22,9 +22,7 @@ from __future__ import absolute_import, unicode_literals
from django.views.generic import TemplateView
from django_mailman3.lib.mailman import get_mailman_client
from postorius.models import (List, MailmanUser, MailmanApiError,
Mailman404Error)
from postorius import utils
from postorius.models import List
from postorius.auth.utils import set_user_access_props
......@@ -59,55 +57,3 @@ class MailingListView(TemplateView, MailmanClientMixin):
if 'template' in kwargs:
self.template = kwargs['template']
return super(MailingListView, self).dispatch(request, *args, **kwargs)
class MailmanUserView(TemplateView, MailmanClientMixin):
"""A generic view for everything based on a mailman.client
user object.
Sets self.mm_user to user object if user_id in **kwargs.
"""
def _get_first_address(self, user_obj):
for address in user_obj.addresses:
return address
def _get_user(self, user_id):
try:
user_obj = MailmanUser.objects.get(address=user_id)
except Mailman404Error:
user_obj = None
# replace display_name with first address if display_name is not set
if user_obj is not None:
if (user_obj.display_name == 'None' or
user_obj.display_name is None):
user_obj.display_name = ''
user_obj.first_address = self._get_first_address(user_obj)
return user_obj
def _get_memberships(self):
memberships = []
for m in self.mm_user.subscriptions:
if m.role != "member":
continue
memberships.append(m)
return memberships
def dispatch(self, request, *args, **kwargs):
# get the user object.
user_id = None
if 'user_id' in kwargs:
user_id = kwargs['user_id']
elif request.user.is_authenticated():
user_id = request.user.email
if user_id is not None:
try:
self.mm_user = self._get_user(user_id)
except MailmanApiError:
return utils.render_api_error(request)
# set the template
if 'template' in kwargs:
self.template = kwargs['template']
return super(MailmanUserView, self).dispatch(request, *args, **kwargs)
This diff is collapsed.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment