diff --git a/musician/api.py b/musician/api.py index a106040..15ca269 100644 --- a/musician/api.py +++ b/musician/api.py @@ -1,5 +1,4 @@ import urllib.parse -from itertools import groupby import requests from django.conf import settings @@ -178,6 +177,12 @@ class Orchestra(object): raise Http404(_("No mailbox found matching the query")) return Mailbox.new_from_json(data_json) + def update_mailbox(self, pk, data): + path = API_PATHS.get('mailbox-detail').format_map({'pk': pk}) + url = urllib.parse.urljoin(self.base_url, path) + status, response = self.request("PATCH", url=url, data=data, raise_exception=False) + return status, response + def retrieve_mailbox_list(self): mailboxes = self.retrieve_service_list(Mailbox.api_name) return [Mailbox.new_from_json(mailbox_data) for mailbox_data in mailboxes] diff --git a/musician/forms.py b/musician/forms.py index 4823fd7..cd3b74d 100644 --- a/musician/forms.py +++ b/musician/forms.py @@ -94,3 +94,23 @@ class MailboxCreateForm(forms.Form): "password": self.cleaned_data["password2"], } return serialized_data + + +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 diff --git a/musician/models.py b/musician/models.py index ffd594e..146f7c3 100644 --- a/musician/models.py +++ b/musician/models.py @@ -241,6 +241,7 @@ class Address(OrchestraModel): "domain": None, "mailboxes": [], "forward": None, + 'url': None, } FORWARD = 'forward' @@ -324,6 +325,12 @@ class Mailbox(OrchestraModel): addresses = [Address.new_from_json(addr) for addr in data.get('addresses', [])] return super().new_from_json(data=data, addresses=addresses) + def deserialize(self): + data = { + 'addresses': [addr.url for addr in self.addresses], + } + return data + class MailinglistService(OrchestraModel): api_name = 'mailinglist' diff --git a/musician/templates/musician/mailbox_form.html b/musician/templates/musician/mailbox_form.html index b7484bc..af51a04 100644 --- a/musician/templates/musician/mailbox_form.html +++ b/musician/templates/musician/mailbox_form.html @@ -21,7 +21,7 @@ {% if form.instance %}
{% endif %} {% endbuttons %} diff --git a/musician/templates/musician/mailboxes.html b/musician/templates/musician/mailboxes.html index 8260b75..961dd74 100644 --- a/musician/templates/musician/mailboxes.html +++ b/musician/templates/musician/mailboxes.html @@ -5,17 +5,15 @@{% trans "Name" %} | {% trans "Filtering" %} | {% trans "Addresses" %} | -||
---|---|---|---|---|
{{ mailbox.name }} | +{{ mailbox.name }} | {{ mailbox.filtering }} |
{% for addr in mailbox.addresses %}
- {{ addr.data.name }}@{{ addr.data.domain.name }}
+ {{ addr.full_address_name }}
{% endfor %} |
- - - - |