From e43e560b75ea6da5a494a188fe1c6b0ea930a2cf Mon Sep 17 00:00:00 2001 From: "luca.gobbi" Date: Wed, 6 Nov 2024 13:54:05 +0100 Subject: [PATCH 1/4] added model getter --- core/cat/experimental/form/cat_form.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/core/cat/experimental/form/cat_form.py b/core/cat/experimental/form/cat_form.py index 8c13061e..49fd91d1 100644 --- a/core/cat/experimental/form/cat_form.py +++ b/core/cat/experimental/form/cat_form.py @@ -34,11 +34,15 @@ def __init__(self, cat) -> None: self._errors: List[str] = [] self._missing_fields: List[str] = [] + self.model_class = self.model_getter() @property def cat(self): return self._cat + def model_getter(self): + return self.model_class + def submit(self, form_data) -> str: raise NotImplementedError From af158f15dbe844b7824eebc9faa1fd88035c0c3f Mon Sep 17 00:00:00 2001 From: "luca.gobbi" Date: Wed, 6 Nov 2024 20:58:27 +0100 Subject: [PATCH 2/4] model getter in validate and extract --- core/cat/experimental/form/cat_form.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/core/cat/experimental/form/cat_form.py b/core/cat/experimental/form/cat_form.py index 49fd91d1..3b12d9bf 100644 --- a/core/cat/experimental/form/cat_form.py +++ b/core/cat/experimental/form/cat_form.py @@ -223,7 +223,7 @@ def extraction_prompt(self): # JSON structure # BaseModel.__fields__['my_field'].type_ JSON_structure = "{" - for field_name, field in self.model_class.model_fields.items(): + for field_name, field in self.model_getter().model_fields.items(): if field.description: description = field.description else: @@ -272,7 +272,7 @@ def validate(self, model): # INFO TODO: In this case the optional fields are always ignored # Attempts to create the model object to update the default values and validate it - model = self.model_class(**model).model_dump(mode="json") + model = self.model_getter()(**model).model_dump(mode="json") # If model is valid change state to COMPLETE self._state = CatFormState.COMPLETE From 8f247802a4c95f5bb885535612a18ca7209b6088 Mon Sep 17 00:00:00 2001 From: "luca.gobbi" Date: Thu, 7 Nov 2024 23:03:33 +0100 Subject: [PATCH 3/4] simplified validate and update to allow model getter to be effective --- core/cat/experimental/form/cat_form.py | 17 +++++++---------- 1 file changed, 7 insertions(+), 10 deletions(-) diff --git a/core/cat/experimental/form/cat_form.py b/core/cat/experimental/form/cat_form.py index 3b12d9bf..6a92b103 100644 --- a/core/cat/experimental/form/cat_form.py +++ b/core/cat/experimental/form/cat_form.py @@ -128,7 +128,7 @@ def next(self): # If the state is INCOMPLETE, execute model update # (and change state based on validation result) if self._state == CatFormState.INCOMPLETE: - self._model = self.update() + self.update() # If state is COMPLETE, ask confirm (or execute action directly) if self._state == CatFormState.COMPLETE: @@ -149,12 +149,11 @@ def update(self): json_details = self.sanitize(json_details) # model merge old and new - new_model = self._model | json_details + self._model = self._model | json_details # Validate new_details - new_model = self.validate(new_model) + self.validate() - return new_model def message(self): state_methods = { @@ -264,7 +263,7 @@ def sanitize(self, model): return model # Validate model - def validate(self, model): + def validate(self): self._missing_fields = [] self._errors = [] @@ -272,7 +271,7 @@ def validate(self, model): # INFO TODO: In this case the optional fields are always ignored # Attempts to create the model object to update the default values and validate it - model = self.model_getter()(**model).model_dump(mode="json") + self.model_getter()(**self._model).model_dump(mode="json") # If model is valid change state to COMPLETE self._state = CatFormState.COMPLETE @@ -285,9 +284,7 @@ def validate(self, model): self._missing_fields.append(field_name) else: self._errors.append(f'{field_name}: {error_message["msg"]}') - del model[field_name] + del self._model[field_name] # Set state to INCOMPLETE - self._state = CatFormState.INCOMPLETE - - return model + self._state = CatFormState.INCOMPLETE \ No newline at end of file From e8274a5a3ccd43460bdc23e44f367d6e7a41b65b Mon Sep 17 00:00:00 2001 From: "luca.gobbi" Date: Mon, 11 Nov 2024 08:38:25 +0100 Subject: [PATCH 4/4] fix: removed redundant line --- core/cat/experimental/form/cat_form.py | 1 - 1 file changed, 1 deletion(-) diff --git a/core/cat/experimental/form/cat_form.py b/core/cat/experimental/form/cat_form.py index 6a92b103..79a49df0 100644 --- a/core/cat/experimental/form/cat_form.py +++ b/core/cat/experimental/form/cat_form.py @@ -34,7 +34,6 @@ def __init__(self, cat) -> None: self._errors: List[str] = [] self._missing_fields: List[str] = [] - self.model_class = self.model_getter() @property def cat(self):