From 0246d0a22ec5154bf25625d8c4238d1a2cc144ca Mon Sep 17 00:00:00 2001 From: Santiago Lamora Date: Mon, 27 Sep 2021 12:40:52 +0200 Subject: [PATCH] Encapsulate Mailbox as a service --- musician/api.py | 7 +++---- musician/models.py | 19 +++++++++++++++++++ musician/templates/musician/mail_base.html | 4 ++-- musician/templates/musician/mailboxes.html | 10 ++++++++-- musician/views.py | 15 ++------------- 5 files changed, 34 insertions(+), 21 deletions(-) diff --git a/musician/api.py b/musician/api.py index bffaa3e..8e38e7c 100644 --- a/musician/api.py +++ b/musician/api.py @@ -7,7 +7,7 @@ from django.http import Http404 from django.urls.exceptions import NoReverseMatch from django.utils.translation import gettext_lazy as _ -from .models import Address, DatabaseService, Domain, SaasService, UserAccount, WebSite +from .models import Address, DatabaseService, Domain, Mailbox, SaasService, UserAccount, WebSite DOMAINS_PATH = 'domains/' TOKEN_PATH = '/api-token-auth/' @@ -179,9 +179,8 @@ class Orchestra(object): return addresses def retrieve_mailbox_list(self): - # TODO(@slamora) encapsulate as a Service class - raw_mailboxes = self.retrieve_service_list('mailbox') - return raw_mailboxes + mailboxes = self.retrieve_service_list(Mailbox.api_name) + return [Mailbox.new_from_json(mailbox_data) for mailbox_data in mailboxes] def retrieve_domain(self, pk): path = API_PATHS.get('domain-detail').format_map({'pk': pk}) diff --git a/musician/models.py b/musician/models.py index b0094b3..9e62d51 100644 --- a/musician/models.py +++ b/musician/models.py @@ -17,6 +17,7 @@ class OrchestraModel: api_name = None verbose_name = None fields = () + param_defaults = {} id = None def __init__(self, **kwargs): @@ -294,6 +295,24 @@ class Address(OrchestraModel): return mailbox_details +class Mailbox(OrchestraModel): + api_name = 'mailbox' + verbose_name = _('Mailbox') + description = _('Description details for mailbox page.') + fields = ('name', 'filtering', 'addresses', 'active') + param_defaults = { + 'name': None, + 'filtering': None, + 'is_active': True, + 'addresses': [], + } + + @classmethod + def new_from_json(cls, data, **kwargs): + addresses = [Address.new_from_json(addr) for addr in data.get('addresses', [])] + return super().new_from_json(data=data, addresses=addresses) + + class MailinglistService(OrchestraModel): api_name = 'mailinglist' verbose_name = _('Mailing list') diff --git a/musician/templates/musician/mail_base.html b/musician/templates/musician/mail_base.html index 1529874..1790e90 100644 --- a/musician/templates/musician/mail_base.html +++ b/musician/templates/musician/mail_base.html @@ -16,8 +16,8 @@ {% trans "Addresses" %} - diff --git a/musician/templates/musician/mailboxes.html b/musician/templates/musician/mailboxes.html index 2c731b5..a9e0764 100644 --- a/musician/templates/musician/mailboxes.html +++ b/musician/templates/musician/mailboxes.html @@ -19,11 +19,17 @@ - {% for mailbox in mailboxes %} + {% for mailbox in object_list %} {{ mailbox.name }} {{ mailbox.filtering }} - {{ mailbox.addresses }} + + {% for addr in mailbox.addresses %} + + {{ addr.data.name }}@{{ addr.data.domain.name }} +
+ {% endfor %} + {% endfor %} diff --git a/musician/views.py b/musician/views.py index 412f0e5..336f4ea 100644 --- a/musician/views.py +++ b/musician/views.py @@ -19,7 +19,7 @@ from .auth import logout as auth_logout from .forms import LoginForm, MailForm from .mixins import (CustomContextMixin, ExtendedPaginationMixin, UserTokenRequiredMixin) -from .models import (Address, Bill, DatabaseService, MailinglistService, +from .models import (Address, Bill, DatabaseService, Mailbox, MailinglistService, PaymentSource, SaasService, UserAccount) from .settings import ALLOWED_RESOURCES from .utils import get_bootstraped_percent @@ -289,24 +289,13 @@ class MailingListsView(ServiceListView): class MailboxesView(ServiceListView): - # TODO (@slamora) refactor after encapsulating Mailbox as a service - # service_class = Mailbox + service_class = Mailbox template_name = "musician/mailboxes.html" extra_context = { # Translators: This message appears on the page title 'title': _('Mailboxes'), } - def get_queryset(self): - # TODO (@slamora) refactor after encapsulating Mailbox as a service - return self.orchestra.retrieve_mailbox_list() - - def get_context_data(self, **kwargs): - context = super().get_context_data(**kwargs) - # TODO (@slamora) refactor after encapsulating Mailbox as a service - context['mailboxes'] = context['object_list'] - return context - class DatabasesView(ServiceListView): template_name = "musician/databases.html"