From 5c49a4018a99fdc7b895af1a9dceeff564f5ebfc Mon Sep 17 00:00:00 2001 From: Emmanuel Garette Date: Sat, 19 Jan 2019 10:54:18 +0100 Subject: [PATCH] some improvments --- tiramisu_json_api/api.py | 48 +++++++++++++++++++++++++++++----------- 1 file changed, 35 insertions(+), 13 deletions(-) diff --git a/tiramisu_json_api/api.py b/tiramisu_json_api/api.py index 11bd3b5..a5852d5 100644 --- a/tiramisu_json_api/api.py +++ b/tiramisu_json_api/api.py @@ -11,10 +11,14 @@ from .i18n import _ TYPE = {'boolean': bool, 'number': int, - 'string': str} + 'string': str, + 'password': str, + 'domain': str} + class Option: # fake Option (IntOption, StrOption, ...) + # only usefull for warnings def __init__(self, name, path): @@ -24,9 +28,6 @@ class Option: def __call__(self): return self - def impl_get_display_name(self): - return self.name - def impl_getpath(self): return self.path @@ -252,17 +253,29 @@ class TiramisuOptionValue(_Value): def list(self): return self.schema['enum'] - def set(self, value): - if self.schema['type'] == 'choice': + def _validate(self, type_, value): + if value in [None, undefined]: + return + if type_ == 'choice': if value not in self.schema['enum']: raise Exception('value {} is not in {}'.format(value, self.schema['enum'])) - elif not isinstance(value, TYPE[self.schema['type']]): - raise Exception('value {} is not a valid {} '.format(value, self.schema['type'])) + elif not isinstance(value, TYPE[type_]): + raise Exception('value {} is not a valid {} '.format(value, type_)) + + def set(self, value): + type_ = self.schema['type'] + if self.schema.get('isMulti', False): + if not isinstance(value, list): + raise Exception('value must be a list') + for val in value: + self._validate(type_, val) + else: + self._validate(type_, value) self.config.modify_value(self.path, self.index, value, self.form.get(self.path, {}).get('remote', False)) - self._display_warnings(self.path, value, self.schema['type'], self.schema['name']) + self._display_warnings(self.path, value, type_, self.schema['name']) def reset(self): self.config.delete_value(self.path, @@ -326,12 +339,20 @@ class TiramisuOptionDescription(_Option): self.form) if subfunc == 'property': return TiramisuOptionProperty(self.model.get(self.path, {})) + if subfunc == 'value': + return TiramisuOptionValue(self.config, + self.schema, + self.model, + self.form, + self.temp, + self.path, + self.index) raise APIError(_('please specify a valid sub function ({})').format(subfunc)) def group_type(self): - hidden = self.temp.get(key, {}).get('hidden', None) - model_hidden = not self.model.get(key, {}).get('hidden', False) and \ - self.model.get(key, {}).get('display', True) + hidden = self.temp.get(self.path, {}).get('hidden', None) + model_hidden = not self.model.get(self.path, {}).get('hidden', False) and \ + self.model.get(self.path, {}).get('display', True) if hidden is False or (hidden is None and model_hidden): # FIXME return 'default' @@ -560,7 +581,8 @@ class Config: def get_schema(self, path): root_path = self.root - schema = {'properties': self.schema} + schema = {'properties': self.schema, + 'type': 'object'} if root_path: root = self.root.split('.') subpaths = path.split('.')[len(root):]