Set mailbox related addresses on creation

This commit is contained in:
Santiago L 2021-10-07 14:10:25 +02:00
parent ddd8ecf634
commit 056f472ee0
2 changed files with 16 additions and 1 deletions

View File

@ -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

View File

@ -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)