From e69c04ee522648ab578f6b122a9ef90632aa46a9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marek=20Such=C3=A1nek?= Date: Tue, 26 Nov 2024 10:33:56 +0100 Subject: [PATCH] feat(docworker): Add value question validations --- .../dsw/document_worker/consts.py | 2 +- .../dsw/document_worker/model/context.py | 55 ++++++++++++++++++- packages/dsw-tdk/dsw/tdk/consts.py | 2 +- packages/dsw-tdk/dsw/tdk/core.py | 1 + 4 files changed, 56 insertions(+), 4 deletions(-) diff --git a/packages/dsw-document-worker/dsw/document_worker/consts.py b/packages/dsw-document-worker/dsw/document_worker/consts.py index b1411cc6..31ec2e8c 100644 --- a/packages/dsw-document-worker/dsw/document_worker/consts.py +++ b/packages/dsw-document-worker/dsw/document_worker/consts.py @@ -1,7 +1,7 @@ CMD_CHANNEL = 'doc_worker' CMD_COMPONENT = 'doc_worker' COMPONENT_NAME = 'Document Worker' -CURRENT_METAMODEL = 15 +CURRENT_METAMODEL = 16 DEFAULT_ENCODING = 'utf-8' EXIT_SUCCESS = 0 NULL_UUID = '00000000-0000-0000-0000-000000000000' diff --git a/packages/dsw-document-worker/dsw/document_worker/model/context.py b/packages/dsw-document-worker/dsw/document_worker/model/context.py index e3888839..06da8b53 100644 --- a/packages/dsw-document-worker/dsw/document_worker/model/context.py +++ b/packages/dsw-document-worker/dsw/document_worker/model/context.py @@ -792,6 +792,53 @@ def cross_references(self) -> list[CrossReference]: return [r for r in self.references if isinstance(r, CrossReference)] +class ValueQuestionValidation: + SHORT_TYPE: dict[str, str] = { + 'MinLengthQuestionValidation': 'min-length', + 'MaxLengthQuestionValidation': 'max-length', + 'RegexQuestionValidation': 'regex', + 'OrcidQuestionValidation': 'orcid', + 'DoiQuestionValidation': 'doi', + 'MinNumberQuestionValidation': 'min', + 'MaxNumberQuestionValidation': 'max', + 'FromDateQuestionValidation': 'from-date', + 'ToDateQuestionValidation': 'to-date', + 'FromDateTimeQuestionValidation': 'from-datetime', + 'ToDateTimeQuestionValidation': 'to-datetime', + 'FromTimeQuestionValidation': 'from-time', + 'ToTimeQuestionValidation': 'to-time', + 'DomainQuestionValidation': 'domain', + } + VALUE_TYPE: dict[str, type | None] = { + 'MinLengthQuestionValidation': int, + 'MaxLengthQuestionValidation': int, + 'RegexQuestionValidation': str, + 'OrcidQuestionValidation': None, + 'DoiQuestionValidation': None, + 'MinNumberQuestionValidation': float, + 'MaxNumberQuestionValidation': float, + 'FromDateQuestionValidation': str, + 'ToDateQuestionValidation': str, + 'FromDateTimeQuestionValidation': str, + 'ToDateTimeQuestionValidation': str, + 'FromTimeQuestionValidation': str, + 'ToTimeQuestionValidation': str, + 'DomainQuestionValidation': str, + } + + def __init__(self, validation_type: str, value: str | int | float | None = None): + self.type = self.SHORT_TYPE.get(validation_type, 'unknown') + self.full_type = validation_type + self.value = value + + @staticmethod + def load(data: dict, **options): + return ValueQuestionValidation( + validation_type=data['type'], + value=data.get('value', None), + ) + + class ValueQuestion(Question): def __init__(self, uuid, title, text, tag_uuids, reference_uuids, @@ -800,6 +847,7 @@ def __init__(self, uuid, title, text, tag_uuids, reference_uuids, reference_uuids, expert_uuids, required_phase_uuid, annotations) self.value_type = value_type # type: str + self.validations = list() # type: list[ValueQuestionValidation] @property def a(self): @@ -846,7 +894,7 @@ def _resolve_links(self, ctx): @staticmethod def load(data: dict, **options): - return ValueQuestion( + question = ValueQuestion( uuid=data['uuid'], title=data['title'], text=data['text'], @@ -857,6 +905,9 @@ def load(data: dict, **options): value_type=data['valueType'], annotations=_load_annotations(data['annotations']), ) + question.validations = [ValueQuestionValidation.load(d, **options) + for d in data['validations']] + return question class OptionsQuestion(Question): @@ -1765,7 +1816,7 @@ def load(data: dict, **options): class DocumentContext: """Document Context smart representation""" - METAMODEL_VERSION = 15 + METAMODEL_VERSION = 16 def __init__(self, ctx, **options): self.metamodel_version = int(ctx.get('metamodelVersion', '0')) diff --git a/packages/dsw-tdk/dsw/tdk/consts.py b/packages/dsw-tdk/dsw/tdk/consts.py index c2f1808c..c10a9c03 100644 --- a/packages/dsw-tdk/dsw/tdk/consts.py +++ b/packages/dsw-tdk/dsw/tdk/consts.py @@ -4,7 +4,7 @@ APP = 'dsw-tdk' VERSION = '4.12.0' -METAMODEL_VERSION = 15 +METAMODEL_VERSION = 16 REGEX_SEMVER = re.compile(r'^[0-9]+\.[0-9]+\.[0-9]+$') REGEX_ORGANIZATION_ID = re.compile(r'^(?![.])(?!.*[.]$)[a-zA-Z0-9.]+$') diff --git a/packages/dsw-tdk/dsw/tdk/core.py b/packages/dsw-tdk/dsw/tdk/core.py index 49574d48..5f4f661a 100644 --- a/packages/dsw-tdk/dsw/tdk/core.py +++ b/packages/dsw-tdk/dsw/tdk/core.py @@ -50,6 +50,7 @@ def __init__(self, message: str, hint: str): 13: (4, 3, 0), 14: (4, 10, 0), 15: (4, 12, 0), + 16: (4, 13, 0), }