OpenSlides/openslides/core/config.py

282 lines
10 KiB
Python
Raw Normal View History

2017-08-22 14:17:20 +02:00
from typing import (
Any,
Callable,
Dict,
Iterable,
List,
Optional,
TypeVar,
Union,
)
from django.core.exceptions import ValidationError as DjangoValidationError
from django.utils.translation import ugettext as _
2017-08-22 14:17:20 +02:00
from mypy_extensions import TypedDict
2017-08-22 14:17:20 +02:00
from ..utils.collection import CollectionElement
from .exceptions import ConfigError, ConfigNotFound
from .models import ConfigStore
INPUT_TYPE_MAPPING = {
'string': str,
2016-01-09 12:51:26 +01:00
'text': str,
2017-01-06 10:00:31 +01:00
'markupText': str,
'integer': int,
'boolean': bool,
'choice': str,
'comments': list,
'colorpicker': str,
2016-10-12 17:28:18 +02:00
'datetimepicker': int,
'majorityMethod': str,
2017-03-31 13:48:41 +02:00
'logo': dict,
}
class ConfigHandler:
"""
A simple object class to wrap the config variables. It is a container
object. To get a config variable use x = config[...], to set it use
config[...] = x.
"""
2017-08-22 14:17:20 +02:00
def __init__(self) -> None:
# Dict, that keeps all ConfigVariable objects. Has to be set at statup.
2017-08-22 14:17:20 +02:00
# See the ready() method in openslides.core.apps.
self.config_variables = {} # type: Dict[str, ConfigVariable]
# Index to get the database id from a given config key
self.key_to_id = {} # type: Dict[str, int]
2017-08-22 14:17:20 +02:00
def __getitem__(self, key: str) -> Any:
"""
2017-08-22 14:17:20 +02:00
Returns the value of the config variable.
"""
2017-08-22 14:17:20 +02:00
# Build the key_to_id dict
self.save_default_values()
if not self.exists(key):
raise ConfigNotFound(_('The config variable {} was not found.').format(key))
2017-08-22 14:17:20 +02:00
return CollectionElement.from_values(
self.get_collection_string(),
self.key_to_id[key]).get_full_data()['value']
2017-08-22 14:17:20 +02:00
def exists(self, key: str) -> bool:
"""
2017-08-22 14:17:20 +02:00
Returns True, if the config varialbe was defined.
"""
try:
self.config_variables[key]
except KeyError:
return False
else:
return True
2017-08-22 14:17:20 +02:00
# TODO: Remove the any by using right types in INPUT_TYPE_MAPPING
def __setitem__(self, key: str, value: Any) -> None:
"""
Sets the new value. First it validates the input.
"""
2015-06-16 14:03:42 +02:00
# Check if the variable is defined.
try:
config_variable = self.config_variables[key]
except KeyError:
raise ConfigNotFound(_('The config variable {} was not found.').format(key))
# Validate datatype and run validators.
expected_type = INPUT_TYPE_MAPPING[config_variable.input_type]
# Try to convert value into the expected datatype
try:
value = expected_type(value)
except ValueError:
2015-11-18 00:15:18 +01:00
raise ConfigError(_('Wrong datatype. Expected %(expected_type)s, got %(got_type)s.') % {
'expected_type': expected_type, 'got_type': type(value)})
if config_variable.input_type == 'choice':
# Choices can be a callable. In this case call it at this place
if callable(config_variable.choices):
choices = config_variable.choices()
else:
choices = config_variable.choices
2017-08-22 14:17:20 +02:00
if choices is None or value not in map(lambda choice: choice['value'], choices):
raise ConfigError(_('Invalid input. Choice does not match.'))
2017-08-22 14:17:20 +02:00
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
if config_variable.input_type == 'comments':
if not isinstance(value, list):
raise ConfigError(_('motions_comments has to be a list.'))
for comment in value:
if not isinstance(comment, dict):
raise ConfigError(_('Each element in motions_comments has to be a dict.'))
if comment.get('name') is None or comment.get('public') is None:
raise ConfigError(_('A name and a public property have to be given.'))
if not isinstance(comment['name'], str):
raise ConfigError(_('name has to be string.'))
if not isinstance(comment['public'], bool):
raise ConfigError(_('public property has to be bool.'))
2017-03-31 13:48:41 +02:00
if config_variable.input_type == 'logo':
if not isinstance(value, dict):
raise ConfigError(_('logo has to be a dict.'))
whitelist = (
'path',
'display_name',
)
for required_entry in whitelist:
if required_entry not in value:
raise ConfigError(_('{} has to be given.'.format(required_entry)))
if not isinstance(value[required_entry], str):
raise ConfigError(_('{} has to be a string.'.format(required_entry)))
2015-06-16 14:03:42 +02:00
# Save the new value to the database.
2017-08-22 14:17:20 +02:00
db_value = ConfigStore.objects.get(key=key)
2016-10-01 12:58:49 +02:00
db_value.value = value
db_value.save(information={'changed_config': key})
2015-06-16 14:03:42 +02:00
# Call on_change callback.
if config_variable.on_change:
config_variable.on_change()
2017-08-22 14:17:20 +02:00
def update_config_variables(self, items: Iterable['ConfigVariable']) -> None:
"""
Updates the config_variables dict.
"""
2017-08-22 14:17:20 +02:00
# build an index from variable name to the variable
item_index = dict((variable.name, variable) for variable in items)
# Check that all ConfigVariables are unique. So no key from items can
# be in already in self.config_variables
2017-08-22 14:17:20 +02:00
intersection = set(item_index.keys()).intersection(self.config_variables.keys())
if intersection:
raise ConfigError(_('Too many values for config variables {} found.').format(intersection))
2017-08-22 14:17:20 +02:00
self.config_variables.update(item_index)
2017-08-22 14:17:20 +02:00
def save_default_values(self) -> None:
2015-06-16 14:03:42 +02:00
"""
2017-08-22 14:17:20 +02:00
Saves the default values to the database.
2017-08-22 14:17:20 +02:00
Does also build the dictonary key_to_id.
2015-06-16 14:03:42 +02:00
2017-08-22 14:17:20 +02:00
Does nothing on a second run.
"""
2017-08-22 14:17:20 +02:00
if not self.key_to_id:
for item in self.config_variables.values():
try:
db_value = ConfigStore.objects.get(key=item.name)
except ConfigStore.DoesNotExist:
db_value = ConfigStore()
db_value.key = item.name
db_value.value = item.default_value
db_value.save(skip_autoupdate=True)
self.key_to_id[item.name] = db_value.pk
def get_collection_string(self) -> str:
"""
Returns the collection_string from the CollectionStore.
"""
return ConfigStore.get_collection_string()
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.
"""
2017-08-22 14:17:20 +02:00
T = TypeVar('T')
ChoiceType = Optional[List[Dict[str, str]]]
ChoiceCallableType = Union[ChoiceType, Callable[[], ChoiceType]]
ValidatorsType = List[Callable[[T], None]]
OnChangeType = Callable[[], None]
ConfigVariableDict = TypedDict('ConfigVariableDict', {
'key': str,
'default_value': Any,
'value': Any,
'input_type': str,
'label': str,
'help_text': str,
'choices': ChoiceType,
})
class ConfigVariable:
"""
A simple object class to wrap new config variables.
The keyword arguments 'name' and 'default_value' are required.
The keyword arguments 'input_type', 'label', 'help_text' and 'hidden'
are for rendering a HTML form element. The 'input_type is also used for
validation. 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.
The keyword arguments 'weight', 'group' and 'subgroup' are for sorting
and grouping.
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.
The keyword argument 'on_change' can be a callback which is called
every time, the variable is changed.
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.
"""
2017-08-22 14:17:20 +02:00
def __init__(self, name: str, default_value: T, input_type: str='string',
label: str=None, help_text: str=None, choices: ChoiceCallableType=None,
hidden: bool=False, weight: int=0, group: str=None, subgroup: str=None,
validators: ValidatorsType=None, on_change: OnChangeType=None) -> None:
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'."))
self.name = name
self.default_value = default_value
self.input_type = input_type
self.label = label or name
self.help_text = help_text or ''
self.choices = choices
self.hidden = hidden
self.weight = weight
self.group = group or _('General')
self.subgroup = subgroup
self.validators = validators or ()
self.on_change = on_change
@property
2017-08-22 14:17:20 +02:00
def data(self) -> ConfigVariableDict:
"""
Property with all data for OPTIONS requests.
"""
2017-08-22 14:17:20 +02:00
return ConfigVariableDict(
key=self.name,
default_value=self.default_value,
value=config[self.name],
input_type=self.input_type,
label=self.label,
help_text=self.help_text,
choices=self.choices() if callable(self.choices) else self.choices
)
def is_hidden(self) -> bool:
"""
Returns True if the config variable is hidden so it can be removed
from response of OPTIONS request.
"""
return self.hidden