diff --git a/musician/api.py b/musician/api.py index 4913a23..9a38e01 100644 --- a/musician/api.py +++ b/musician/api.py @@ -23,6 +23,7 @@ API_PATHS = { 'domain-list': 'domains/', 'domain-detail': 'domains/{pk}/', 'address-list': 'addresses/', + 'address-detail': 'addresses/{pk}/', 'mailbox-list': 'mailboxes/', 'mailinglist-list': 'lists/', 'saas-list': 'saas/', @@ -114,13 +115,22 @@ class Orchestra(object): def create_mail_address(self, data): resource = '{}-list'.format(MailService.api_name) - - # transform form data to expected format - data["domain"] = {"url": data["domain"]} - data["mailboxes"] = [{"url": mbox} for mbox in data["mailboxes"]] - return self.request("POST", resource=resource, data=data) + def retrieve_mail_address(self, pk): + path = API_PATHS.get('address-detail').format_map({'pk': pk}) + url = urllib.parse.urljoin(self.base_url, path) + status, data = self.request("GET", url=url, raise_exception=False) + if status == 404: + raise Http404(_("No object found matching the query")) + + return MailService.new_from_json(data) + + def update_mail_address(self, pk, data): + path = API_PATHS.get('address-detail').format_map({'pk': pk}) + url = urllib.parse.urljoin(self.base_url, path) + return self.request("PUT", url=url, data=data) + def retrieve_mail_address_list(self, querystring=None): def get_mailbox_id(value): mailboxes = value.get('mailboxes') diff --git a/musician/forms.py b/musician/forms.py index bbfd2de..1302fb2 100644 --- a/musician/forms.py +++ b/musician/forms.py @@ -31,8 +31,13 @@ class MailForm(forms.Form): forward = forms.EmailField(required=False) def __init__(self, *args, **kwargs): + instance = kwargs.pop('instance', None) + if instance is not None: + kwargs['initial'] = instance.deserialize() + domains = kwargs.pop('domains') mailboxes = kwargs.pop('mailboxes') + super().__init__(*args, **kwargs) self.fields['domain'].choices = [(d.url, d.name) for d in domains] self.fields['mailboxes'].choices = [(m['url'], m['name']) for m in mailboxes] @@ -42,3 +47,13 @@ class MailForm(forms.Form): 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 + + 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 diff --git a/musician/models.py b/musician/models.py index 978a0c8..7ac681f 100644 --- a/musician/models.py +++ b/musician/models.py @@ -225,12 +225,13 @@ class DomainRecord(OrchestraModel): return '<%s: %s>' % (self.type, self.value) +# TODO(@slamora) rename to Address class MailService(OrchestraModel): api_name = 'address' verbose_name = _('Mail addresses') description = _('Description details for mail addresses page.') fields = ('mail_address', 'aliases', 'type', 'type_detail') - param_defaults = {} + param_defaults = {"id": None,} FORWARD = 'forward' MAILBOX = 'mailbox' @@ -239,6 +240,15 @@ class MailService(OrchestraModel): self.data = kwargs super().__init__(**kwargs) + def deserialize(self): + data = { + 'name': self.data['name'], + 'domain': self.data['domain']['url'], + 'mailboxes': [mbox['url'] for mbox in self.data['mailboxes']], + 'forward': self.data['forward'], + } + return data + @property def aliases(self): return [ diff --git a/musician/templates/musician/mail.html b/musician/templates/musician/mail.html index f1fcf14..38f0fe3 100644 --- a/musician/templates/musician/mail.html +++ b/musician/templates/musician/mail.html @@ -26,7 +26,7 @@
{% for obj in object_list %}