From 056f472ee078e49c344755a62e7dc30f6ac891e7 Mon Sep 17 00:00:00 2001 From: Santiago Lamora Date: Thu, 7 Oct 2021 14:10:25 +0200 Subject: [PATCH] Set mailbox related addresses on creation --- musician/forms.py | 7 +++++++ musician/views.py | 10 +++++++++- 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/musician/forms.py b/musician/forms.py index cd3b74d..62ea0ae 100644 --- a/musician/forms.py +++ b/musician/forms.py @@ -76,6 +76,12 @@ class MailboxCreateForm(forms.Form): strip=False, help_text=_("Enter the same password as before, for verification."), ) + addresses = forms.MultipleChoiceField(required=False) + + def __init__(self, *args, **kwargs): + addresses = kwargs.pop('addresses') + super().__init__(*args, **kwargs) + self.fields['addresses'].choices = [(addr.url, addr.full_address_name) for addr in addresses] def clean_password2(self): password = self.cleaned_data.get("password") @@ -92,6 +98,7 @@ class MailboxCreateForm(forms.Form): serialized_data = { "name": self.cleaned_data["name"], "password": self.cleaned_data["password2"], + "addresses": self.cleaned_data["addresses"], } return serialized_data diff --git a/musician/views.py b/musician/views.py index 4cc0b40..56bc28f 100644 --- a/musician/views.py +++ b/musician/views.py @@ -339,6 +339,14 @@ class MailboxCreateView(CustomContextMixin, UserTokenRequiredMixin, FormView): number_of_mailboxes = len(self.orchestra.retrieve_mailbox_list()) return number_of_mailboxes >= profile.allowed_resources('mailbox') + def get_form_kwargs(self): + kwargs = super().get_form_kwargs() + kwargs.update({ + 'addresses': self.orchestra.retrieve_mail_address_list(), + }) + + return kwargs + def form_valid(self, form): serialized_data = form.serialize() status, response = self.orchestra.create_mailbox(serialized_data) @@ -347,11 +355,11 @@ class MailboxCreateView(CustomContextMixin, UserTokenRequiredMixin, FormView): if status == 400: # handle errors & add to form (they will be rendered) form.add_error(field=None, error=response) - return self.form_invalid(form) else: logger.error("{}: {}".format(status, response[:120])) msg = "Sorry, an error occurred while processing your request ({})".format(status) form.add_error(field='__all__', error=msg) + return self.form_invalid(form) return super().form_valid(form)