diff --git a/src/postorius/forms.py b/src/postorius/forms.py index ab82bd2..6fd6872 100644 --- a/src/postorius/forms.py +++ b/src/postorius/forms.py @@ -726,6 +726,21 @@ """ 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 @@ "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): """ diff --git a/src/postorius/templates/postorius/user/address_preferences.html b/src/postorius/templates/postorius/user/address_preferences.html index fc97741..4b03779 100644 --- a/src/postorius/templates/postorius/user/address_preferences.html +++ b/src/postorius/templates/postorius/user/address_preferences.html @@ -13,7 +13,7 @@
{% user_nav 'settings_address' 'Address-based settings' %} - {% if nolists %} + {% if formset|length == 0 %}

{% trans 'No preferences available' %}

{% trans 'You are not yet subscribed to any lists, so you have no Mailman preferences.' %}

{% else %} @@ -23,7 +23,7 @@ {{ formset.management_form }}
- {% for form,address in zipped_data %} + {% for form in formset %} {% if forloop.first %} @@ -37,9 +37,9 @@ {% endif %} - + {% for field in form %} - {% if field|fieldtype == 'RadioSelect' %} + {% if field|fieldtype_is:'RadioSelect' %}
{{ address }}{{ form.address }} {% for choice in field %}