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 @@
{% trans 'You are not yet subscribed to any lists, so you have no Mailman preferences.' %}
-{% trans 'Use this form to change the email used for this subscription' %}: -
+ {% trans 'Use this form to change the email used for this subscription' %}: +