2015-06-17 18:32:05 +02:00
|
|
|
from django.core.exceptions import ValidationError as DjangoValidationError
|
|
|
|
from django.utils.translation import ugettext as _
|
|
|
|
|
2013-03-01 17:13:12 +01:00
|
|
|
from .exceptions import ConfigError, ConfigNotFound
|
2013-09-25 10:01:01 +02:00
|
|
|
from .models import ConfigStore
|
2013-03-01 17:13:12 +01:00
|
|
|
from .signals import config_signal
|
|
|
|
|
2015-06-17 18:32:05 +02:00
|
|
|
INPUT_TYPE_MAPPING = {
|
|
|
|
'string': str,
|
|
|
|
'integer': int,
|
|
|
|
'boolean': bool,
|
|
|
|
'choice': str}
|
|
|
|
|
2013-03-01 17:13:12 +01:00
|
|
|
|
2015-06-17 18:32:05 +02:00
|
|
|
class ConfigHandler:
|
2013-03-01 17:13:12 +01:00
|
|
|
"""
|
2015-06-17 18:32:05 +02:00
|
|
|
A simple object class to wrap the config variables. It is a container
|
2013-03-01 17:13:12 +01:00
|
|
|
object. To get a config variable use x = config[...], to set it use
|
|
|
|
config[...] = x.
|
|
|
|
"""
|
|
|
|
def __getitem__(self, key):
|
2015-06-17 18:32:05 +02:00
|
|
|
"""
|
|
|
|
Returns the value of the config variable. Builds the cache if it
|
|
|
|
does not exist.
|
|
|
|
"""
|
2013-03-01 17:13:12 +01:00
|
|
|
try:
|
|
|
|
return self._cache[key]
|
|
|
|
except KeyError:
|
2015-06-17 18:32:05 +02:00
|
|
|
raise ConfigNotFound(_('The config variable %s was not found.') % key)
|
2013-03-01 17:13:12 +01:00
|
|
|
except AttributeError:
|
|
|
|
self.setup_cache()
|
|
|
|
return self[key]
|
|
|
|
|
2015-06-17 18:32:05 +02:00
|
|
|
def setup_cache(self):
|
|
|
|
"""
|
|
|
|
Creates a cache of all config variables with their current value.
|
|
|
|
"""
|
|
|
|
self._cache = {}
|
|
|
|
for key, config_variable in self.get_config_variables().items():
|
|
|
|
self._cache[key] = config_variable.default_value
|
|
|
|
for config_object in ConfigStore.objects.all():
|
|
|
|
self._cache[config_object.key] = config_object.value
|
|
|
|
|
|
|
|
def __contains__(self, key):
|
|
|
|
try:
|
|
|
|
config[key]
|
|
|
|
except ConfigNotFound:
|
|
|
|
return False
|
|
|
|
else:
|
|
|
|
return True
|
|
|
|
|
2013-03-01 17:13:12 +01:00
|
|
|
def __setitem__(self, key, value):
|
2015-06-17 18:32:05 +02:00
|
|
|
"""
|
|
|
|
Sets the new value. First it validates the input.
|
|
|
|
"""
|
2015-06-16 14:03:42 +02:00
|
|
|
# Check if the variable is defined.
|
2015-06-17 18:32:05 +02:00
|
|
|
try:
|
|
|
|
config_variable = config.get_config_variables()[key]
|
|
|
|
except KeyError:
|
|
|
|
raise ConfigNotFound(_('The config variable %s was not found.') % key)
|
|
|
|
|
|
|
|
# Validate datatype and run validators.
|
|
|
|
expected_type = INPUT_TYPE_MAPPING[config_variable.input_type]
|
|
|
|
if not isinstance(value, expected_type):
|
|
|
|
raise ConfigError(_('Wrong datatype. Expected %s, got %s.') % (expected_type, type(value)))
|
|
|
|
if config_variable.input_type == 'choice' and value not in map(lambda choice: choice['value'], config_variable.choices):
|
|
|
|
raise ConfigError(_('Invalid input. Choice does not match.'))
|
|
|
|
for validator in config_variable.validators:
|
|
|
|
try:
|
|
|
|
validator(value)
|
|
|
|
except DjangoValidationError as e:
|
|
|
|
raise ConfigError(e.messages[0])
|
2015-06-16 14:03:42 +02:00
|
|
|
|
|
|
|
# Save the new value to the database.
|
2013-03-01 17:13:12 +01:00
|
|
|
updated_rows = ConfigStore.objects.filter(key=key).update(value=value)
|
|
|
|
if not updated_rows:
|
|
|
|
ConfigStore.objects.create(key=key, value=value)
|
2013-09-08 10:26:51 +02:00
|
|
|
|
2015-06-16 14:03:42 +02:00
|
|
|
# Update cache.
|
2015-06-17 18:32:05 +02:00
|
|
|
if hasattr(self, '_cache'):
|
|
|
|
self._cache[key] = value
|
2013-03-01 17:13:12 +01:00
|
|
|
|
2015-06-16 14:03:42 +02:00
|
|
|
# Call on_change callback.
|
2015-06-17 18:32:05 +02:00
|
|
|
if config_variable.on_change:
|
|
|
|
config_variable.on_change()
|
2013-09-08 10:26:51 +02:00
|
|
|
|
2015-02-04 13:45:50 +01:00
|
|
|
def items(self):
|
2015-01-24 16:35:50 +01:00
|
|
|
"""
|
2015-02-04 13:45:50 +01:00
|
|
|
Returns key-value pairs of all config variables.
|
2015-01-24 16:35:50 +01:00
|
|
|
"""
|
|
|
|
if not hasattr(self, '_cache'):
|
|
|
|
self.setup_cache()
|
2015-02-04 13:45:50 +01:00
|
|
|
return self._cache.items()
|
2015-01-24 16:35:50 +01:00
|
|
|
|
2015-06-16 14:03:42 +02:00
|
|
|
def get_config_variables(self):
|
|
|
|
"""
|
|
|
|
Returns a dictionary with all ConfigVariable instances of all
|
2015-06-17 18:32:05 +02:00
|
|
|
signal receivers. The key is the name of the config variable.
|
2015-06-16 14:03:42 +02:00
|
|
|
"""
|
|
|
|
result = {}
|
|
|
|
for receiver, config_collection in config_signal.send(sender='get_config_variables'):
|
2015-06-17 18:32:05 +02:00
|
|
|
for config_variable in config_collection:
|
2015-06-16 14:03:42 +02:00
|
|
|
if config_variable.name in result:
|
2015-06-17 18:32:05 +02:00
|
|
|
raise ConfigError(_('Too many values for config variable %s found.') % config_variable.name)
|
2015-06-16 14:03:42 +02:00
|
|
|
result[config_variable.name] = config_variable
|
|
|
|
return result
|
|
|
|
|
2014-01-31 01:54:41 +01:00
|
|
|
def get_all_translatable(self):
|
|
|
|
"""
|
|
|
|
Generator to get all config variables as strings when their values are
|
|
|
|
intended to be translated.
|
|
|
|
"""
|
2015-06-16 14:03:42 +02:00
|
|
|
for config_variable in self.get_config_variables().values():
|
|
|
|
if config_variable.translatable:
|
|
|
|
yield config_variable.name
|
2013-03-01 17:13:12 +01:00
|
|
|
|
|
|
|
config = ConfigHandler()
|
|
|
|
"""
|
|
|
|
Final entry point to get an set config variables. To get a config variable
|
|
|
|
use x = config[...], to set it use config[...] = x.
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
2015-06-17 18:32:05 +02:00
|
|
|
class ConfigVariable:
|
2013-03-01 17:13:12 +01:00
|
|
|
"""
|
2015-06-17 18:32:05 +02:00
|
|
|
A simple object class to wrap new config variables.
|
2013-03-01 17:13:12 +01:00
|
|
|
|
2015-06-17 18:32:05 +02:00
|
|
|
The keyword arguments 'name' and 'default_value' are required.
|
2013-03-01 17:13:12 +01:00
|
|
|
|
2015-06-17 18:32:05 +02:00
|
|
|
The keyword arguments 'input_type', 'label' and 'help_text' are for
|
|
|
|
rendering a HTML form element. If you set 'input_type' to 'choice' you
|
|
|
|
have to provide 'choices', which is a list of dictionaries containing a
|
|
|
|
value and a display_name of every possible choice.
|
2013-03-01 17:13:12 +01:00
|
|
|
|
2015-06-17 18:32:05 +02:00
|
|
|
The keyword arguments 'weight', 'group' and 'subgroup' are for sorting
|
|
|
|
and grouping.
|
2013-03-01 17:13:12 +01:00
|
|
|
|
2015-06-17 18:32:05 +02:00
|
|
|
The keyword argument validators expects an interable of validator
|
|
|
|
functions. Such a function gets the value and raises Django's
|
|
|
|
ValidationError if the value is invalid.
|
2013-03-01 17:13:12 +01:00
|
|
|
|
2015-06-17 18:32:05 +02:00
|
|
|
The keyword argument 'on_change' can be a callback which is called
|
|
|
|
every time, the variable is changed.
|
2013-03-01 17:13:12 +01:00
|
|
|
|
2015-06-17 18:32:05 +02:00
|
|
|
If the argument 'translatable' is set, OpenSlides is able to translate
|
|
|
|
the value during setup of the database if the admin uses the respective
|
|
|
|
command line option.
|
2013-03-01 17:13:12 +01:00
|
|
|
"""
|
2015-06-17 18:32:05 +02:00
|
|
|
def __init__(self, name, default_value, input_type='string', label=None,
|
|
|
|
help_text=None, choices=None, weight=0, group=None,
|
|
|
|
subgroup=None, validators=None, on_change=None, translatable=False):
|
|
|
|
if input_type not in INPUT_TYPE_MAPPING:
|
|
|
|
raise ValueError(_('Invalid value for config attribute input_type.'))
|
|
|
|
if input_type == 'choice' and choices is None:
|
|
|
|
raise ConfigError(_("Either config attribute 'choices' must not be None or "
|
|
|
|
"'input_type' must not be 'choice'."))
|
|
|
|
elif input_type != 'choice' and choices is not None:
|
|
|
|
raise ConfigError(_("Either config attribute 'choices' must be None or "
|
|
|
|
"'input_type' must be 'choice'."))
|
2013-03-01 17:13:12 +01:00
|
|
|
self.name = name
|
|
|
|
self.default_value = default_value
|
2015-06-17 18:32:05 +02:00
|
|
|
self.input_type = input_type
|
|
|
|
self.label = label or name
|
|
|
|
self.help_text = help_text or ''
|
|
|
|
self.choices = choices
|
|
|
|
self.weight = weight
|
|
|
|
self.group = group or _('General')
|
|
|
|
self.subgroup = subgroup
|
|
|
|
self.validators = validators or ()
|
2013-09-08 10:26:51 +02:00
|
|
|
self.on_change = on_change
|
2014-01-31 01:54:41 +01:00
|
|
|
self.translatable = translatable
|
2015-06-17 18:32:05 +02:00
|
|
|
|
|
|
|
@property
|
|
|
|
def data(self):
|
|
|
|
"""
|
|
|
|
Property with all data for OPTIONS requests.
|
|
|
|
"""
|
|
|
|
data = {
|
|
|
|
'key': self.name,
|
|
|
|
'value': config[self.name],
|
|
|
|
'input_type': self.input_type,
|
|
|
|
'label': self.label,
|
|
|
|
'help_text': self.help_text
|
|
|
|
}
|
|
|
|
if self.input_type == 'choice':
|
|
|
|
data['choices'] = self.choices
|
|
|
|
return data
|