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.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})

View File

@ -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')

View File

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

View File

@ -19,11 +19,17 @@
</tr>
</thead>
<tbody>
{% for mailbox in mailboxes %}
{% for mailbox in object_list %}
<tr>
<td>{{ mailbox.name }}</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>
</tr>
{% endfor %}

View File

@ -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"