django-orchestra/orchestra/settings.py

114 lines
3.3 KiB
Python
Raw Normal View History

2015-04-26 13:53:00 +00:00
from collections import OrderedDict
2014-05-08 16:59:35 +00:00
from django.conf import settings
2015-04-26 15:52:40 +00:00
from django.core.exceptions import ValidationError
2014-05-08 16:59:35 +00:00
from django.utils.translation import ugettext_lazy as _
2015-04-26 15:52:40 +00:00
from .core import validators
2014-05-08 16:59:35 +00:00
2015-04-26 13:53:00 +00:00
class Setting(object):
"""
Keeps track of the defined settings.
Instances of this class are the native value of the setting.
"""
conf_settings = settings
settings = OrderedDict()
def __str__(self):
return self.name
def __repr__(self):
value = str(self.value)
value = ("'%s'" if isinstance(value, str) else '%s') % value
return '<%s: %s>' % (self.name, value)
2015-04-26 15:52:40 +00:00
def __new__(cls, name, default, help_text="", choices=None, editable=True, multiple=False,
validators=[], types=[], call_init=False):
2015-04-26 13:53:00 +00:00
if call_init:
return super(Setting, cls).__new__(cls)
cls.settings[name] = cls(name, default, help_text=help_text, choices=choices,
2015-04-26 15:52:40 +00:00
editable=editable, multiple=multiple, validators=validators, types=types, call_init=True)
2015-04-26 13:53:00 +00:00
return cls.get_value(name, default)
def __init__(self, *args, **kwargs):
self.name, self.default = args
for name, value in kwargs.items():
setattr(self, name, value)
self.value = self.get_value(self.name, self.default)
2015-04-26 15:52:40 +00:00
self.validate_value(self.value)
2015-04-26 13:53:00 +00:00
self.settings[name] = self
2015-04-26 15:52:40 +00:00
def validate_value(self, value):
validators.all_valid(value, self.validators)
valid_types = list(self.types)
if isinstance(self.default, (list, tuple)):
valid_types.extend([list, tuple])
valid_types.append(type(self.default))
if not isinstance(value, tuple(valid_types)):
raise ValidationError("%s is not a valid type (%s)." %
(type(value).__name__, ', '.join(t.__name__ for t in valid_types))
)
2015-04-26 13:53:00 +00:00
@classmethod
def get_value(cls, name, default):
return getattr(cls.conf_settings, name, default)
ORCHESTRA_BASE_DOMAIN = Setting('ORCHESTRA_BASE_DOMAIN',
'orchestra.lan'
)
2015-04-26 13:53:00 +00:00
2015-04-26 15:52:40 +00:00
ORCHESTRA_SITE_URL = Setting('ORCHESTRA_SITE_URL', 'https://orchestra.%s' % ORCHESTRA_BASE_DOMAIN,
2015-04-26 13:53:00 +00:00
help_text=_("Domain name used when it will not be possible to infere the domain from a request."
"For example in periodic tasks.")
)
2014-05-08 16:59:35 +00:00
2015-04-26 13:53:00 +00:00
ORCHESTRA_SITE_NAME = Setting('ORCHESTRA_SITE_NAME', 'orchestra')
2014-05-08 16:59:35 +00:00
2015-04-26 13:53:00 +00:00
ORCHESTRA_SITE_VERBOSE_NAME = Setting('ORCHESTRA_SITE_VERBOSE_NAME',
_("%s Hosting Management" % ORCHESTRA_SITE_NAME.capitalize())
)
2014-05-08 16:59:35 +00:00
2015-04-26 13:53:00 +00:00
2014-05-08 16:59:35 +00:00
# Service management commands
2015-04-26 13:53:00 +00:00
ORCHESTRA_START_SERVICES = Setting('ORCHESTRA_START_SERVICES', (
'postgresql',
'celeryevcam',
'celeryd',
'celerybeat',
('uwsgi', 'nginx'),
2015-04-26 13:53:00 +00:00
))
2014-05-08 16:59:35 +00:00
2015-04-26 13:53:00 +00:00
ORCHESTRA_RESTART_SERVICES = Setting('ORCHESTRA_RESTART_SERVICES', (
'celeryd',
'celerybeat',
'uwsgi'
2015-04-26 13:53:00 +00:00
))
2014-05-08 16:59:35 +00:00
2015-04-26 13:53:00 +00:00
ORCHESTRA_STOP_SERVICES = Setting('ORCHESTRA_STOP_SERVICES', (
('uwsgi', 'nginx'),
'celerybeat',
'celeryd',
'celeryevcam',
'postgresql'
2015-04-26 13:53:00 +00:00
))
2014-05-13 13:46:40 +00:00
2015-04-26 13:53:00 +00:00
ORCHESTRA_API_ROOT_VIEW = Setting('ORCHESTRA_API_ROOT_VIEW',
'orchestra.api.root.APIRoot'
)
2014-10-24 10:16:46 +00:00
2015-04-26 13:53:00 +00:00
ORCHESTRA_DEFAULT_SUPPORT_FROM_EMAIL = Setting('ORCHESTRA_DEFAULT_SUPPORT_FROM_EMAIL',
'support@{}'.format(ORCHESTRA_BASE_DOMAIN)
)
2015-04-26 13:53:00 +00:00
ORCHESTRA_EDIT_SETTINGS = Setting('ORCHESTRA_EDIT_SETTINGS', True)