From 731d331cd127b1d7b39eebe3ff38a1e0a674098e Mon Sep 17 00:00:00 2001 From: "me@jeffersonbledsoe.com" Date: Wed, 17 Jan 2024 17:39:04 +0000 Subject: [PATCH] Overhaul how we're passing settings back and forth --- .../volto/formsupport/configure.zcml | 2 +- .../restapi/deserializer/__init__.py | 80 +++++++++++++------ .../formsupport/restapi/serializer/blocks.py | 25 +++--- .../volto/formsupport/validation.zcml | 22 ----- .../volto/formsupport/validation/__init__.py | 34 +++----- .../custom_validators/CharactersValidator.py | 18 +++-- .../validation/custom_validators/__init__.py | 1 + .../formsupport/validation/definition.py | 27 +++++++ .../formsupport/validation/validation.zcml | 10 +++ 9 files changed, 126 insertions(+), 93 deletions(-) delete mode 100644 src/collective/volto/formsupport/validation.zcml create mode 100644 src/collective/volto/formsupport/validation/definition.py create mode 100644 src/collective/volto/formsupport/validation/validation.zcml diff --git a/src/collective/volto/formsupport/configure.zcml b/src/collective/volto/formsupport/configure.zcml index dc6f6ed7..2258e98b 100644 --- a/src/collective/volto/formsupport/configure.zcml +++ b/src/collective/volto/formsupport/configure.zcml @@ -22,7 +22,7 @@ - + - - - - - - - - - - diff --git a/src/collective/volto/formsupport/validation/__init__.py b/src/collective/volto/formsupport/validation/__init__.py index 3a78ea85..d37d1060 100644 --- a/src/collective/volto/formsupport/validation/__init__.py +++ b/src/collective/volto/formsupport/validation/__init__.py @@ -5,8 +5,10 @@ from zope.schema.interfaces import IVocabularyFactory from zope.schema.vocabulary import SimpleVocabulary +from collective.volto.formsupport.validation.custom_validators import custom_validators +from collective.volto.formsupport.validation.definition import ValidationDefinition + try: - from Products.validation import validation from Products.validation.validators.BaseValidators import baseValidators except ImportError: # Products.validation is optional validation = None @@ -35,41 +37,25 @@ def delete_setting(setting): return settings -class ValidationDefinition: - def __init__(self, validator): - self._name = validator.name - self._settings = vars(validator) - - def __call__(self, value, **kwargs): - """Allow using the class directly as a validator""" - return self.validate(value, **kwargs) - - @property - def settings(self): - return self._settings - - def validate(self, value, **kwargs): - if value is None: - # Let the system for required take care of None values - return - res = validation(self._name, value, **kwargs) - if res != 1: - return res - - def _update_validators(): """ Add Products.validation validators to the available list of validators Code taken from collective.easyform . Could lookup based on `IValidator` instead of re-registering? """ - if validation and baseValidators: + if baseValidators: for validator in baseValidators: provideUtility( ValidationDefinition(validator), provides=IFieldValidator, name=validator.name, ) + for validator in custom_validators: + provideUtility( + ValidationDefinition(validator), + provides=IFieldValidator, + name=validator.name, + ) _update_validators() diff --git a/src/collective/volto/formsupport/validation/custom_validators/CharactersValidator.py b/src/collective/volto/formsupport/validation/custom_validators/CharactersValidator.py index 1f42be8b..e42a9dca 100644 --- a/src/collective/volto/formsupport/validation/custom_validators/CharactersValidator.py +++ b/src/collective/volto/formsupport/validation/custom_validators/CharactersValidator.py @@ -1,12 +1,12 @@ from Products.validation.interfaces.IValidator import IValidator from zope.interface import implementer -from collective.volto.formsupport.validation import ValidationDefinition +from collective.volto.formsupport.validation.definition import ValidationDefinition # TODO: Tidy up code structure so we don't need to be a definition @implementer(IValidator) -class CharactersValidator(ValidationDefinition): +class CharactersValidator(): def __init__(self, name, title="", description="", characters=0, _internal_type=""): self.name = name self.title = title or name @@ -25,18 +25,20 @@ def settings(self): def __call__(self, value="", *args, **kwargs): if self._internal_type == "max": - if (not value or len(value) > self.characters): + if not value or len(value) > self.characters: # TODO: i18n msg = f"Validation failed({self.name}): is more than {self.characters}" return msg elif self._internal_type == "min": - if (not value or len(value) < self.characters): + if not value or len(value) < self.characters: # TODO: i18n - msg = f"Validation failed({self.name}): is less than {self.characters}", + msg = ( + f"Validation failed({self.name}): is less than {self.characters}", + ) return msg else: # TODO: i18n - msg = f"Validation failed({self.name}): Unknown characters validator type", + msg = ( + f"Validation failed({self.name}): Unknown characters validator type", + ) return msg - - diff --git a/src/collective/volto/formsupport/validation/custom_validators/__init__.py b/src/collective/volto/formsupport/validation/custom_validators/__init__.py index e2f1d8b2..cc919f67 100644 --- a/src/collective/volto/formsupport/validation/custom_validators/__init__.py +++ b/src/collective/volto/formsupport/validation/custom_validators/__init__.py @@ -4,3 +4,4 @@ maxCharacters = CharactersValidator("maxCharacters", _internal_type="max") minCharacters = CharactersValidator("minCharacters", _internal_type="min") +custom_validators = [maxCharacters, minCharacters] diff --git a/src/collective/volto/formsupport/validation/definition.py b/src/collective/volto/formsupport/validation/definition.py new file mode 100644 index 00000000..c250aca8 --- /dev/null +++ b/src/collective/volto/formsupport/validation/definition.py @@ -0,0 +1,27 @@ +from Products.validation import validation + + +class ValidationDefinition: + def __init__(self, validator): + self._name = validator.name + self._settings = vars(validator) + + def __call__(self, value, **kwargs): + """Allow using the class directly as a validator""" + return self.validate(value, **kwargs) + + @property + def settings(self): + return self._settings + + @settings.setter + def settings(self, value): + self._value = value + + def validate(self, value, **kwargs): + if value is None: + # Let the system for required take care of None values + return + res = validation(self._name, value, **kwargs) + if res != 1: + return res diff --git a/src/collective/volto/formsupport/validation/validation.zcml b/src/collective/volto/formsupport/validation/validation.zcml new file mode 100644 index 00000000..ed430f42 --- /dev/null +++ b/src/collective/volto/formsupport/validation/validation.zcml @@ -0,0 +1,10 @@ + + + + + + +