From 98c5482f5b564c644a7733a638bc6bee2ebaf193 Mon Sep 17 00:00:00 2001 From: Santiago Lamora Date: Wed, 20 Nov 2019 10:29:39 +0100 Subject: [PATCH] Refactor and rename Service --> OrchestraModel class. --- musician/models.py | 59 ++++++++++++++++++---- musician/templates/musician/databases.html | 29 ++++++++++- musician/views.py | 3 +- 3 files changed, 79 insertions(+), 12 deletions(-) diff --git a/musician/models.py b/musician/models.py index c55acbd..3904aa3 100644 --- a/musician/models.py +++ b/musician/models.py @@ -1,28 +1,69 @@ from django.utils.html import format_html -class Service: +class OrchestraModel: + """ Base class from which all orchestra models will inherit. """ api_name = None verbose_name = None fields = () + param_defaults = {} - def __init__(self, data={}): + def __init__(self, **kwargs): if self.verbose_name is None: self.verbose_name = self.api_name - self.data = data + for (param, default) in self.param_defaults.items(): + setattr(self, param, kwargs.get(param, default)) - def get(self, key): - # retrieve attr of the object and if undefined get raw data - return getattr(self, key, self.data.get(key)) + # def get(self, key): + # # retrieve attr of the object and if undefined get raw data + # return getattr(self, key, self.data.get(key)) + + @classmethod + def new_from_json(cls, data, **kwargs): + """ Create a new instance based on a JSON dict. Any kwargs should be + supplied by the inherited, calling class. + Args: + data: A JSON dict, as converted from the JSON in the orchestra API. + """ + + json_data = data.copy() + if kwargs: + for key, val in kwargs.items(): + json_data[key] = val + + c = cls(**json_data) + c._json = data + return c -class DatabaseService(Service): +class DatabaseUser(OrchestraModel): + api_name = 'databaseusers' + fields = ('username',) + param_defaults = { + 'username': None, + } + + +class DatabaseService(OrchestraModel): api_name = 'database' fields = ('name', 'type', 'users') + param_defaults = { + "id": None, + "name": None, + "type": None, + "users": None, + } + + @classmethod + def new_from_json(cls, data, **kwargs): + users = None + if 'users' in data: + users = [DatabaseUser.new_from_json(user_data) for user_data in data['users']] + return super().new_from_json(data=data, users=users) -class MailService(Service): +class MailService(OrchestraModel): api_name = 'address' verbose_name = 'Mail' fields = ('mail_address', 'aliases', 'type', 'type_detail') @@ -54,7 +95,7 @@ class MailService(Service): return {'usage': 0, 'total': 213} -class MailinglistService(Service): +class MailinglistService(OrchestraModel): api_name = 'mailinglist' verbose_name = 'Mailing list' fields = ('name', 'status', 'address_name', 'admin_email', 'configure') diff --git a/musician/templates/musician/databases.html b/musician/templates/musician/databases.html index 927ff1c..96911db 100644 --- a/musician/templates/musician/databases.html +++ b/musician/templates/musician/databases.html @@ -3,7 +3,32 @@ {% block content %} -

Section title

-

Little description of what to be expected...

+

{{ service.verbose_name }}

+

{{ service.description }}

+ + + + {% for field_name in service.fields %} + + {% endfor %} + + + + {% for resource in object_list %} + + + + + + {% endfor %} + + {% include "musician/components/table_paginator.html" %} +
{{ field_name }}
{{ resource.name }}{{ resource.type }} + {% for user in resource.users %} + {{ user.username }} + {% empty %} + No users for this database. + {% endfor %} +
{% endblock %} diff --git a/musician/views.py b/musician/views.py index 9945b8a..f794351 100644 --- a/musician/views.py +++ b/musician/views.py @@ -45,7 +45,7 @@ class ServiceListView(CustomContextMixin, ExtendedPaginationMixin, UserTokenRequ "ServiceListView requires a definiton of 'service'") json_qs = self.orchestra.retrieve_service_list(self.service_class.api_name) - return [self.service_class(data) for data in json_qs] + return [self.service_class.new_from_json(data) for data in json_qs] def get_context_data(self, **kwargs): context = super().get_context_data(**kwargs) @@ -89,6 +89,7 @@ class MailingListsView(ServiceListView): class DatabasesView(ServiceListView): + template_name = "musician/databases.html" service_class = DatabaseService