Encapsulate Mailbox as a service

This commit is contained in:
Santiago L 2021-09-27 12:40:52 +02:00
parent 9ba1d0a23c
commit 0246d0a22e
5 changed files with 34 additions and 21 deletions

View File

@ -7,7 +7,7 @@ from django.http import Http404
from django.urls.exceptions import NoReverseMatch from django.urls.exceptions import NoReverseMatch
from django.utils.translation import gettext_lazy as _ 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/' DOMAINS_PATH = 'domains/'
TOKEN_PATH = '/api-token-auth/' TOKEN_PATH = '/api-token-auth/'
@ -179,9 +179,8 @@ class Orchestra(object):
return addresses return addresses
def retrieve_mailbox_list(self): def retrieve_mailbox_list(self):
# TODO(@slamora) encapsulate as a Service class mailboxes = self.retrieve_service_list(Mailbox.api_name)
raw_mailboxes = self.retrieve_service_list('mailbox') return [Mailbox.new_from_json(mailbox_data) for mailbox_data in mailboxes]
return raw_mailboxes
def retrieve_domain(self, pk): def retrieve_domain(self, pk):
path = API_PATHS.get('domain-detail').format_map({'pk': pk}) path = API_PATHS.get('domain-detail').format_map({'pk': pk})

View File

@ -17,6 +17,7 @@ class OrchestraModel:
api_name = None api_name = None
verbose_name = None verbose_name = None
fields = () fields = ()
param_defaults = {}
id = None id = None
def __init__(self, **kwargs): def __init__(self, **kwargs):
@ -294,6 +295,24 @@ class Address(OrchestraModel):
return mailbox_details 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): class MailinglistService(OrchestraModel):
api_name = 'mailinglist' api_name = 'mailinglist'
verbose_name = _('Mailing list') verbose_name = _('Mailing list')

View File

@ -16,8 +16,8 @@
<a class="nav-link {% if url_name == 'mails' %}active{% endif %}" href="{% url 'musician:mails' %}" role="tab" <a class="nav-link {% if url_name == 'mails' %}active{% endif %}" href="{% url 'musician:mails' %}" role="tab"
aria-selected="true">{% trans "Addresses" %}</a> aria-selected="true">{% trans "Addresses" %}</a>
</li> </li>
<li class="nav-item {% if url_name == 'mailboxes' %}active{% endif %}"> <li class="nav-item">
<a class="nav-link" href="{% url 'musician:mailboxes' %}" role="tab" aria-selected="false">{% trans "Mailboxes" %}</a> <a class="nav-link {% if url_name == 'mailboxes' %}active{% endif %}" href="{% url 'musician:mailboxes' %}" role="tab" aria-selected="false">{% trans "Mailboxes" %}</a>
</li> </li>
</ul> </ul>

View File

@ -19,11 +19,17 @@
</tr> </tr>
</thead> </thead>
<tbody> <tbody>
{% for mailbox in mailboxes %} {% for mailbox in object_list %}
<tr> <tr>
<td>{{ mailbox.name }}</td> <td>{{ mailbox.name }}</td>
<td>{{ mailbox.filtering }}</td> <td>{{ mailbox.filtering }}</td>
<td>{{ mailbox.addresses }}</td> <td>
{% for addr in mailbox.addresses %}
<a href="{% url 'musician:mail-update' addr.data.id %}">
{{ addr.data.name }}@{{ addr.data.domain.name }}
</a><br/>
{% endfor %}
</td>
<td class="pl-4 text-{{ mailbox.is_active|yesno:'success,danger' }}"><i class="fas fa-{{ mailbox.is_active|yesno:'check,times' }}"></i></td> <td class="pl-4 text-{{ mailbox.is_active|yesno:'success,danger' }}"><i class="fas fa-{{ mailbox.is_active|yesno:'check,times' }}"></i></td>
</tr> </tr>
{% endfor %} {% endfor %}

View File

@ -19,7 +19,7 @@ from .auth import logout as auth_logout
from .forms import LoginForm, MailForm from .forms import LoginForm, MailForm
from .mixins import (CustomContextMixin, ExtendedPaginationMixin, from .mixins import (CustomContextMixin, ExtendedPaginationMixin,
UserTokenRequiredMixin) UserTokenRequiredMixin)
from .models import (Address, Bill, DatabaseService, MailinglistService, from .models import (Address, Bill, DatabaseService, Mailbox, MailinglistService,
PaymentSource, SaasService, UserAccount) PaymentSource, SaasService, UserAccount)
from .settings import ALLOWED_RESOURCES from .settings import ALLOWED_RESOURCES
from .utils import get_bootstraped_percent from .utils import get_bootstraped_percent
@ -289,24 +289,13 @@ class MailingListsView(ServiceListView):
class MailboxesView(ServiceListView): class MailboxesView(ServiceListView):
# TODO (@slamora) refactor after encapsulating Mailbox as a service service_class = Mailbox
# service_class = Mailbox
template_name = "musician/mailboxes.html" template_name = "musician/mailboxes.html"
extra_context = { extra_context = {
# Translators: This message appears on the page title # Translators: This message appears on the page title
'title': _('Mailboxes'), '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): class DatabasesView(ServiceListView):
template_name = "musician/databases.html" template_name = "musician/databases.html"