2019-11-13 10:42:23 +00:00
|
|
|
from django.utils.html import format_html
|
2019-12-03 12:34:39 +00:00
|
|
|
from django.utils.translation import gettext_lazy as _
|
2019-11-13 10:08:19 +00:00
|
|
|
|
2019-11-13 10:42:23 +00:00
|
|
|
|
2019-11-20 09:29:39 +00:00
|
|
|
class OrchestraModel:
|
|
|
|
""" Base class from which all orchestra models will inherit. """
|
2019-11-13 11:26:35 +00:00
|
|
|
api_name = None
|
2019-11-13 10:42:23 +00:00
|
|
|
verbose_name = None
|
|
|
|
fields = ()
|
2019-11-20 09:29:39 +00:00
|
|
|
param_defaults = {}
|
2019-11-13 10:08:19 +00:00
|
|
|
|
2019-11-20 09:29:39 +00:00
|
|
|
def __init__(self, **kwargs):
|
2019-11-13 10:08:19 +00:00
|
|
|
if self.verbose_name is None:
|
2019-11-13 11:26:35 +00:00
|
|
|
self.verbose_name = self.api_name
|
2019-11-13 10:08:19 +00:00
|
|
|
|
2019-11-20 09:29:39 +00:00
|
|
|
for (param, default) in self.param_defaults.items():
|
|
|
|
setattr(self, param, kwargs.get(param, default))
|
2019-11-13 10:08:19 +00:00
|
|
|
|
2019-11-20 09:29:39 +00:00
|
|
|
# def get(self, key):
|
|
|
|
# # retrieve attr of the object and if undefined get raw data
|
|
|
|
# return getattr(self, key, self.data.get(key))
|
2019-11-13 10:08:19 +00:00
|
|
|
|
2019-11-20 09:29:39 +00:00
|
|
|
@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
|
2019-11-20 16:41:15 +00:00
|
|
|
# TODO(@slamora) remove/replace by private variable to ovoid name collisions
|
|
|
|
c.data = data
|
2019-11-20 09:29:39 +00:00
|
|
|
return c
|
2019-11-13 10:42:23 +00:00
|
|
|
|
2019-11-13 11:27:25 +00:00
|
|
|
|
2019-11-20 19:07:35 +00:00
|
|
|
class BillingContact(OrchestraModel):
|
|
|
|
param_defaults = {
|
|
|
|
'name': None,
|
|
|
|
'address': None,
|
|
|
|
'city': None,
|
|
|
|
'zipcode': None,
|
|
|
|
'country': None,
|
|
|
|
'vat': None,
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
class PaymentSource(OrchestraModel):
|
|
|
|
api_name = 'payment-source'
|
|
|
|
param_defaults = {
|
|
|
|
"method": None,
|
|
|
|
"data": [],
|
|
|
|
"is_active": False,
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
class UserAccount(OrchestraModel):
|
|
|
|
api_name = 'accounts'
|
|
|
|
param_defaults = {
|
|
|
|
'username': None,
|
|
|
|
'type': None,
|
|
|
|
'language': None,
|
|
|
|
'short_name': None,
|
|
|
|
'full_name': None,
|
|
|
|
'billing': {},
|
|
|
|
}
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def new_from_json(cls, data, **kwargs):
|
|
|
|
billing = None
|
|
|
|
|
|
|
|
if 'billcontact' in data:
|
|
|
|
billing = BillingContact.new_from_json(data['billcontact'])
|
|
|
|
return super().new_from_json(data=data, billing=billing)
|
|
|
|
|
|
|
|
|
2019-11-20 09:29:39 +00:00
|
|
|
class DatabaseUser(OrchestraModel):
|
|
|
|
api_name = 'databaseusers'
|
|
|
|
fields = ('username',)
|
|
|
|
param_defaults = {
|
|
|
|
'username': None,
|
|
|
|
}
|
2019-11-13 11:27:25 +00:00
|
|
|
|
2019-11-20 09:29:39 +00:00
|
|
|
|
|
|
|
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(OrchestraModel):
|
2019-11-13 11:26:35 +00:00
|
|
|
api_name = 'address'
|
2019-12-03 12:34:39 +00:00
|
|
|
verbose_name = _('Mail addresses')
|
|
|
|
description = _('Litle description of what to be expected in this section to aid the user. Even a link to more help if there is one available.')
|
2019-11-13 10:42:23 +00:00
|
|
|
fields = ('mail_address', 'aliases', 'type', 'type_detail')
|
|
|
|
|
|
|
|
FORWARD = 'forward'
|
|
|
|
MAILBOX = 'mailbox'
|
|
|
|
|
2019-11-13 10:08:19 +00:00
|
|
|
@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}
|
2019-11-13 10:42:23 +00:00
|
|
|
|
|
|
|
|
2019-11-20 09:29:39 +00:00
|
|
|
class MailinglistService(OrchestraModel):
|
2019-11-13 11:26:35 +00:00
|
|
|
api_name = 'mailinglist'
|
2019-11-13 10:42:23 +00:00
|
|
|
verbose_name = 'Mailing list'
|
|
|
|
fields = ('name', 'status', 'address_name', 'admin_email', 'configure')
|
2019-11-20 16:41:15 +00:00
|
|
|
param_defaults = {
|
|
|
|
'name': None,
|
|
|
|
'admin_email': None,
|
|
|
|
}
|
2019-11-13 10:42:23 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def status(self):
|
|
|
|
# TODO(@slamora): where retrieve if the list is active?
|
|
|
|
return 'active'
|
|
|
|
|
|
|
|
@property
|
|
|
|
def address_name(self):
|
|
|
|
return "{}@{}".format(self.data['address_name'], self.data['address_domain']['name'])
|
|
|
|
|
|
|
|
@property
|
|
|
|
def configure(self):
|
|
|
|
# TODO(@slamora): build mailtran absolute URL
|
|
|
|
return format_html('<a href="#TODO">Mailtrain</a>')
|