Create generic ServiceListView and implement MailView

This commit is contained in:
Santiago Lamora 2019-11-13 11:08:19 +01:00
parent 054b7e92ac
commit 24bce2ab04
5 changed files with 122 additions and 3 deletions

View file

@ -14,6 +14,8 @@ API_PATHS = {
# services
'domain-list': 'domains/',
'address-list': 'addresses/',
'mailbox-list': 'mailboxes/',
'mailinglist-list': 'lists/',
# ... TODO (@slamora) complete list of backend URLs
}

40
musician/models.py Normal file
View file

@ -0,0 +1,40 @@
class MailService:
name = 'address'
verbose_name = 'Mail'
fields = ('mail_address', 'aliases', 'type', 'type_detail')
FORWARD = 'forward'
MAILBOX = 'mailbox'
def __init__(self, data={}):
if self.verbose_name is None:
self.verbose_name = self.name
self.data = data
def get(self, key):
# retrieve attr of the object and if undefined get raw data
return getattr(self, key, self.data.get(key))
@property
def aliases(self):
return [
name + '@' + self.data['domain']['name'] for name in self.data['names'][1:]
]
@property
def mail_address(self):
return self.data['names'][0] + '@' + self.data['domain']['name']
@property
def type(self):
if self.data['forward']:
return self.FORWARD
return self.MAILBOX
@property
def type_detail(self):
if self.type == self.FORWARD:
return self.data['forward']
# TODO(@slamora) retrieve mailbox usage
return {'usage': 0, 'total': 213}

View file

@ -0,0 +1,28 @@
{% extends "musician/base.html" %}
{% load i18n musician %}
{% block content %}
<h1>{{ service.verbose_name }}</h1>
<p>{{ service.description }}</p>
<table class="table table-hover">
<thead class="thead-dark">
<tr>
{% for field_name in service.fields %}
<th scope="col">{{ field_name }}</th>
{% endfor %}
</tr>
</thead>
<tbody>
{% for resource in object_list %}
<tr>
{% for field_name in service.fields %}
<td>{{ resource|get_item:field_name }}</td>
{% endfor %}
</tr>
{% endfor %}
</tbody>
{% include "musician/components/table_paginator.html" %}
</table>
{% endblock %}

View file

@ -0,0 +1,6 @@
from django.template.defaulttags import register
@register.filter
def get_item(dictionary, key):
return dictionary.get(key)

View file

@ -1,4 +1,6 @@
from django.core.exceptions import ImproperlyConfigured
from itertools import groupby
from django.http import HttpResponseRedirect
from django.shortcuts import render
from django.urls import reverse_lazy
@ -12,6 +14,7 @@ from .auth import login as auth_login
from .auth import logout as auth_logout
from .forms import LoginForm
from .mixins import CustomContextMixin, ExtendedPaginationMixin, UserTokenRequiredMixin
from .models import MailService
class DashboardView(CustomContextMixin, UserTokenRequiredMixin, TemplateView):
@ -32,11 +35,51 @@ class DashboardView(CustomContextMixin, UserTokenRequiredMixin, TemplateView):
class ServiceListView(CustomContextMixin, ExtendedPaginationMixin, UserTokenRequiredMixin, ListView):
"""Base list view to all services"""
pass
service = None
template_name = "musician/service_list.html" # TODO move to ServiceListView
def get_queryset(self):
if self.service_class is None or self.service_class.name is None:
raise ImproperlyConfigured(
"ServiceListView requires a definiton of 'service'")
return self.orchestra.retrieve_service_list(self.service_class.name)
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context.update({
'service': self.service_class,
})
return context
class MailView(CustomContextMixin, UserTokenRequiredMixin, TemplateView):
template_name = "musician/mail.html"
class MailView(ServiceListView):
service_class = MailService
def get_queryset(self):
def retrieve_mailbox(value):
mailboxes = value.get('mailboxes')
if len(mailboxes) == 0:
return ''
return mailboxes[0]['id']
# group addresses with the same mailbox
raw_data = self.orchestra.retrieve_service_list(
self.service_class.name)
addresses = []
for key, group in groupby(raw_data, retrieve_mailbox):
aliases = []
data = {}
for thing in group:
aliases.append(thing.pop('name'))
data = thing
data['names'] = aliases
addresses.append(MailService(data))
return addresses
class MailingListsView(ServiceListView):