2019-10-29 09:58:54 +00:00
|
|
|
|
|
2021-06-23 11:47:27 +00:00
|
|
|
|
from django import forms
|
2019-10-29 09:58:54 +00:00
|
|
|
|
from django.contrib.auth.forms import AuthenticationForm
|
2021-06-23 11:47:27 +00:00
|
|
|
|
from django.core.exceptions import ValidationError
|
2021-10-05 11:10:53 +00:00
|
|
|
|
from django.utils.translation import gettext_lazy as _
|
2019-10-29 09:58:54 +00:00
|
|
|
|
|
|
|
|
|
from . import api
|
|
|
|
|
|
|
|
|
|
class LoginForm(AuthenticationForm):
|
|
|
|
|
|
|
|
|
|
def clean(self):
|
|
|
|
|
username = self.cleaned_data.get('username')
|
|
|
|
|
password = self.cleaned_data.get('password')
|
|
|
|
|
|
|
|
|
|
if username is not None and password:
|
2019-10-30 12:05:46 +00:00
|
|
|
|
orchestra = api.Orchestra(username=username, password=password)
|
|
|
|
|
|
|
|
|
|
if orchestra.auth_token is None:
|
2019-10-29 09:58:54 +00:00
|
|
|
|
raise self.get_invalid_login_error()
|
|
|
|
|
else:
|
2019-10-30 12:05:46 +00:00
|
|
|
|
self.username = username
|
|
|
|
|
self.token = orchestra.auth_token
|
2020-01-23 16:37:08 +00:00
|
|
|
|
self.user = orchestra.retrieve_profile()
|
2019-10-29 09:58:54 +00:00
|
|
|
|
|
|
|
|
|
return self.cleaned_data
|
2021-06-23 11:47:27 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class MailForm(forms.Form):
|
|
|
|
|
name = forms.CharField()
|
|
|
|
|
domain = forms.ChoiceField()
|
|
|
|
|
mailboxes = forms.MultipleChoiceField(required=False)
|
|
|
|
|
forward = forms.EmailField(required=False)
|
|
|
|
|
|
|
|
|
|
def __init__(self, *args, **kwargs):
|
2021-10-01 11:36:52 +00:00
|
|
|
|
self.instance = kwargs.pop('instance', None)
|
|
|
|
|
if self.instance is not None:
|
|
|
|
|
kwargs['initial'] = self.instance.deserialize()
|
2021-06-24 11:08:16 +00:00
|
|
|
|
|
2021-06-23 11:47:27 +00:00
|
|
|
|
domains = kwargs.pop('domains')
|
|
|
|
|
mailboxes = kwargs.pop('mailboxes')
|
2021-06-24 11:08:16 +00:00
|
|
|
|
|
2021-06-23 11:47:27 +00:00
|
|
|
|
super().__init__(*args, **kwargs)
|
|
|
|
|
self.fields['domain'].choices = [(d.url, d.name) for d in domains]
|
2021-09-27 11:17:49 +00:00
|
|
|
|
self.fields['mailboxes'].choices = [(m.url, m.name) for m in mailboxes]
|
2021-06-23 11:47:27 +00:00
|
|
|
|
|
|
|
|
|
def clean(self):
|
|
|
|
|
cleaned_data = super().clean()
|
|
|
|
|
if not cleaned_data.get('mailboxes') and not cleaned_data.get('forward'):
|
|
|
|
|
raise ValidationError("A mailbox or forward address should be provided.")
|
|
|
|
|
return cleaned_data
|
2021-06-24 11:08:16 +00:00
|
|
|
|
|
|
|
|
|
def serialize(self):
|
|
|
|
|
assert hasattr(self, 'cleaned_data')
|
|
|
|
|
serialized_data = {
|
|
|
|
|
"name": self.cleaned_data["name"],
|
|
|
|
|
"domain": {"url": self.cleaned_data["domain"]},
|
|
|
|
|
"mailboxes": [{"url": mbox} for mbox in self.cleaned_data["mailboxes"]],
|
|
|
|
|
"forward": self.cleaned_data["forward"],
|
|
|
|
|
}
|
|
|
|
|
return serialized_data
|
2021-10-05 11:10:53 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class MailboxCreateForm(forms.Form):
|
|
|
|
|
error_messages = {
|
|
|
|
|
'password_mismatch': _('The two password fields didn’t match.'),
|
|
|
|
|
}
|
|
|
|
|
name = forms.CharField()
|
|
|
|
|
password = forms.CharField(
|
|
|
|
|
label=_("Password"),
|
|
|
|
|
strip=False,
|
|
|
|
|
widget=forms.PasswordInput(attrs={'autocomplete': 'new-password'}),
|
|
|
|
|
)
|
|
|
|
|
password2 = forms.CharField(
|
|
|
|
|
label=_("Password confirmation"),
|
|
|
|
|
widget=forms.PasswordInput(attrs={'autocomplete': 'new-password'}),
|
|
|
|
|
strip=False,
|
|
|
|
|
help_text=_("Enter the same password as before, for verification."),
|
|
|
|
|
)
|
2021-10-07 12:10:25 +00:00
|
|
|
|
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]
|
2021-10-05 11:10:53 +00:00
|
|
|
|
|
|
|
|
|
def clean_password2(self):
|
|
|
|
|
password = self.cleaned_data.get("password")
|
|
|
|
|
password2 = self.cleaned_data.get("password2")
|
|
|
|
|
if password and password2 and password != password2:
|
|
|
|
|
raise ValidationError(
|
|
|
|
|
self.error_messages['password_mismatch'],
|
|
|
|
|
code='password_mismatch',
|
|
|
|
|
)
|
|
|
|
|
return password2
|
|
|
|
|
|
|
|
|
|
def serialize(self):
|
|
|
|
|
assert self.is_valid()
|
|
|
|
|
serialized_data = {
|
|
|
|
|
"name": self.cleaned_data["name"],
|
|
|
|
|
"password": self.cleaned_data["password2"],
|
2021-10-07 12:10:25 +00:00
|
|
|
|
"addresses": self.cleaned_data["addresses"],
|
2021-10-05 11:10:53 +00:00
|
|
|
|
}
|
|
|
|
|
return serialized_data
|
2021-10-07 11:51:31 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class MailboxUpdateForm(forms.Form):
|
|
|
|
|
addresses = forms.MultipleChoiceField(required=False)
|
|
|
|
|
|
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
|
self.instance = kwargs.pop('instance', None)
|
|
|
|
|
if self.instance is not None:
|
|
|
|
|
kwargs['initial'] = self.instance.deserialize()
|
|
|
|
|
|
|
|
|
|
addresses = kwargs.pop('addresses')
|
|
|
|
|
super().__init__(*args, **kwargs)
|
|
|
|
|
self.fields['addresses'].choices = [(addr.url, addr.full_address_name) for addr in addresses]
|
|
|
|
|
|
|
|
|
|
def serialize(self):
|
|
|
|
|
assert self.is_valid()
|
|
|
|
|
serialized_data = {
|
|
|
|
|
"addresses": self.cleaned_data["addresses"],
|
|
|
|
|
}
|
|
|
|
|
return serialized_data
|