Skip to content

Commit f247836

Browse files
committed
[WIP] fix model maker
1 parent ab14b86 commit f247836

12 files changed

+673
-35
lines changed

ozonenv/core/ModelMaker.py

Lines changed: 48 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
from typing import List, Any
1010

1111
from json_logic import jsonLogic
12-
from pydantic import create_model
12+
from pydantic import create_model, Field
1313

1414
from ozonenv.core.BaseModels import BasicModel, BaseModel, MainModel, defaultdt
1515
from ozonenv.core.utils import (
@@ -57,13 +57,16 @@ def __init__(self, raw, builder, **kwargs):
5757
self.i18n = kwargs.get("i18n", {})
5858
self.clean = re.compile("<.*?>")
5959
self.resources = kwargs.get("resources", [])
60-
self.defaultValue = self.raw.get("defaultValue")
61-
self._value = None
6260
self.input_type = kwargs.get("input_type", str)
61+
self.input_defaultv = kwargs.get("defaultv", False)
62+
self.defaultValue = self.raw.get("defaultValue", self.input_defaultv)
63+
self._value = None
6364
self.nested = kwargs.get("nested", [])
6465
self.cfg = {}
6566
self.index = 0
6667
self.iindex = 0
68+
self.pfield_cfg = {"default": self.defaultValue}
69+
self.pmodel_cfg = [self.input_type]
6770

6871
@property
6972
def value(self):
@@ -259,7 +262,7 @@ def update_config(self):
259262
self.cfg["datetime"] = False
260263
self.cfg["min"] = False
261264
self.cfg["max"] = False
262-
if self.raw.get("type") == "datetime":
265+
if self.type == "datetime":
263266
enableDateInCfg = "enableDate" in self.raw
264267
self.cfg["time"] = self.raw.get("enableTime", False)
265268
self.cfg["date"] = self.raw.get("enableDate", False)
@@ -279,18 +282,33 @@ def update_config(self):
279282
self.cfg["transform"] = {"type": "datetime"}
280283
self.cfg["min"] = self.raw["widget"]["minDate"]
281284
self.cfg["max"] = self.raw["widget"]["maxDate"]
282-
if self.raw.get("requireDecimal") is True:
283-
self.cfg["mask"] = self.raw.get("displayMask", "decimal")
284-
self.cfg["min"] = self.validate.get("min")
285-
self.cfg["max"] = self.validate.get("max")
286-
self.cfg["delimiter"] = self.raw.get("delimiter", ",")
287-
self.cfg["dp"] = self.raw.get("decimalLimit", 2)
288-
self.cfg["transform"] = {
289-
"type": "float",
290-
"dp": self.cfg["dp"],
291-
"mask": self.cfg["mask"],
292-
"dps": self.cfg["delimiter"],
293-
}
285+
elif self.type == 'number':
286+
self.cfg["min"] = self.validate.get("min", False)
287+
self.cfg["max"] = self.validate.get("max", False)
288+
if self.raw.get("requireDecimal") is True:
289+
self.cfg["mask"] = self.raw.get("displayMask", "decimal")
290+
self.cfg["delimiter"] = self.raw.get("delimiter", ",")
291+
self.cfg["dp"] = self.raw.get("decimalLimit", 2)
292+
self.cfg["transform"] = {
293+
"type": "float",
294+
"dp": self.cfg["dp"],
295+
"mask": self.cfg["mask"],
296+
"dps": self.cfg["delimiter"],
297+
}
298+
299+
def make_pydantic_field(self):
300+
self.pfield_cfg.update({'title': self.label})
301+
if self.cfg["min"]:
302+
self.pfield_cfg.update({'ge': self.cfg["min"]})
303+
if self.cfg["max"]:
304+
self.pfield_cfg.update({'le': self.cfg["max"]})
305+
# if self.cfg["required"]:
306+
# self.pfield_cfg.update({'required': self.cfg["required"]})
307+
self.pmodel_cfg.append(Field(**self.pfield_cfg))
308+
res = tuple(self.pmodel_cfg)
309+
if self.key == "howManySeats":
310+
print(f"howManySeats field config {res}")
311+
return res
294312

295313
def aval_conditional(self):
296314
if self.raw.get("conditional").get("json"):
@@ -979,13 +997,21 @@ def complete_component_field(self, comp, compo_todo):
979997
if self.parent_builder:
980998
builder = self.parent_builder
981999
if comp.get("type") == "select":
982-
field = selectComponent(comp, builder, input_type=compo_todo[0])
1000+
field = selectComponent(
1001+
comp, builder, input_type=compo_todo[0], defaultv=compo_todo[1]
1002+
)
9831003
elif comp.get("type") == "survey":
984-
field = surveyComponent(comp, builder, input_type=compo_todo[0])
1004+
field = surveyComponent(
1005+
comp, builder, input_type=compo_todo[0], defaultv=compo_todo[1]
1006+
)
9851007
elif comp.get("type") == "datetime":
986-
field = Component(comp, builder, input_type=str)
1008+
field = Component(
1009+
comp, builder, input_type=str, defaultv=compo_todo[1]
1010+
)
9871011
else:
988-
field = Component(comp, builder, input_type=compo_todo[0])
1012+
field = Component(
1013+
comp, builder, input_type=compo_todo[0], defaultv=compo_todo[1]
1014+
)
9891015
field.update_config()
9901016
field.parent = self.parent
9911017
if field.required:
@@ -1000,10 +1026,10 @@ def complete_component_field(self, comp, compo_todo):
10001026
if field.transform:
10011027
self.tranform_data_value[field.key] = field.transform.copy()
10021028
if field.limit_values:
1003-
self.fields_limit_value = field.limit_values.copy()
1029+
self.fields_limit_value[field.key] = field.limit_values.copy()
10041030
if field.defaultValue:
10051031
compo_todo[1] = field.defaultValue
1006-
self.components[comp.get("key")] = tuple(compo_todo)
1032+
self.components[comp.get("key")] = field.make_pydantic_field()
10071033
self.complete_component(field)
10081034

10091035
def add_textfield(self, comp):

ozonenv/core/OzonOrm.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -453,6 +453,8 @@ def _getattribute(obj, name):
453453

454454
async def make_local_model(self, mod, version):
455455
jdata = mod.mm.model.model_json_schema()
456+
if mod.name == "test_form_1":
457+
print(jdata)
456458
async with aiofiles.open(
457459
f"/tmp/{mod.name}.json", "w+", encoding="utf-8"
458460
) as mod_file:

run_test.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ poetry run black ozonenv/**/*.py
88
#poetry run flake8 ozonenv/**/*.py
99
pip install --upgrade -e .
1010
echo "run test"
11-
poetry run pytest --cov --cov-report=html -vv
11+
poetry run pytest --cov --cov-report=html -vv -s
1212
docker compose down
13-
rm -rf tests/models
13+
#rm -rf tests/models
1414
echo "make project: Done."

tests/data/test_form_1.0_formio_schema.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -650,7 +650,7 @@
650650
"strictDateValidation": false,
651651
"multiple": false,
652652
"unique": false,
653-
"min": "",
653+
"min": 1,
654654
"max": "",
655655
"step": "any",
656656
"integer": ""

tests/data/test_form_1_formio_schema.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -353,7 +353,7 @@
353353
"id": "eszvmi3"
354354
},
355355
{
356-
"label": "Appointment Date / Time àè",
356+
"label": "Appointment Date / Time",
357357
"tableView": false,
358358
"enableMinDateInput": false,
359359
"datePicker": {
@@ -714,7 +714,7 @@
714714
"strictDateValidation": false,
715715
"multiple": false,
716716
"unique": false,
717-
"min": "",
717+
"min": 1,
718718
"max": "",
719719
"step": "any",
720720
"integer": ""

tests/models/__init__.py

Whitespace-only changes.

tests/models/documento.py

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
# generated by datamodel-codegen:
2+
# filename: documento.json
3+
# timestamp: 2025-05-27T12:07:55+00:00
4+
5+
from __future__ import annotations
6+
7+
from typing import Optional
8+
9+
from pydantic import Field
10+
11+
from ozonenv.core.BaseModels import BasicModel
12+
13+
14+
class Documento(BasicModel):
15+
rec_name: Optional[str] = Field('', title='Codice')
16+
stato: Optional[str] = Field('caricato', title='stato')
17+
anomalia: Optional[bool] = Field(False, title="Criticita'")
18+
active: Optional[bool] = Field(False, title='Attivo')
19+
dtMod: Optional[str] = Field('1970-01-01T00:00:00', title='Ext dt Agg.')
20+
idDg: Optional[str] = Field('', title='idDg')
21+
document_type: Optional[str] = Field('', title='Tipo Documento')
22+
tipi_dettaglio: Optional[list] = Field([], title='Tipi Dettaglio')
23+
24+
25+
@classmethod
26+
def get_version(cls):
27+
return '1970-01-01T00:00:00'
28+
29+
@classmethod
30+
def get_unique_fields(cls):
31+
return ['rec_name']
32+
33+
@classmethod
34+
def computed_fields(cls):
35+
return {}
36+
37+
@classmethod
38+
def no_clone_field_keys(cls):
39+
return ['rec_name']
40+
41+
@classmethod
42+
def tranform_data_value(cls):
43+
return {'dtMod': {'type': 'date'}}
44+
45+
@classmethod
46+
def fields_limit_value(cls):
47+
return {}
48+
49+
@classmethod
50+
def create_task_action(cls):
51+
return []
52+
53+
@classmethod
54+
def fields_properties(cls):
55+
return {}
56+
57+
@classmethod
58+
def default_hidden_fields(cls):
59+
return []
60+
61+
@classmethod
62+
def default_readonly_fields(cls):
63+
return []
64+
65+
@classmethod
66+
def default_required_fields(cls):
67+
return ['rec_name']
68+
69+
@classmethod
70+
def filter_keys(cls):
71+
return ['rec_name', 'stato', 'anomalia', 'active', 'dtMod', 'idDg', 'document_type', 'tipi_dettaglio']
72+
73+
@classmethod
74+
def config_fields(cls):
75+
return {'rec_name': {'ctype': 'textfield', 'disabled': False, 'readonly': False, 'hidden': False, 'required': True, 'unique': False, 'component': 'Component', 'calculateServer': None, 'action_type': False, 'no_clone': False, 'transform': {}, 'datetime': False, 'min': False, 'max': False}, 'stato': {'ctype': 'select', 'disabled': False, 'readonly': False, 'hidden': False, 'required': False, 'unique': False, 'component': 'selectComponent', 'calculateServer': None, 'action_type': False, 'no_clone': False, 'transform': {}, 'datetime': False, 'min': False, 'max': False, 'valueProperty': None, 'selectValues': None, 'defaultValue': 'caricato', 'multiple': False, 'dataSrc': 'values', 'idPath': '', 'resource_id': '', 'values': [{'label': 'Caricato', 'value': 'caricato'}, {'label': 'Nuovo', 'value': 'nuovo'}, {'label': 'Parziale', 'value': 'parziale'}, {'label': 'Completato', 'value': 'completato'}, {'label': 'Errore', 'value': 'errore'}], 'template_label_keys': []}, 'anomalia': {'ctype': 'checkbox', 'disabled': False, 'readonly': False, 'hidden': False, 'required': False, 'unique': False, 'component': 'Component', 'calculateServer': None, 'action_type': False, 'no_clone': False, 'transform': {}, 'datetime': False, 'min': False, 'max': False}, 'active': {'ctype': 'checkbox', 'disabled': False, 'readonly': False, 'hidden': False, 'required': False, 'unique': False, 'component': 'Component', 'calculateServer': None, 'action_type': False, 'no_clone': False, 'transform': {}, 'datetime': False, 'min': False, 'max': False}, 'dtMod': {'ctype': 'datetime', 'disabled': False, 'readonly': False, 'hidden': False, 'required': False, 'unique': False, 'component': 'Component', 'calculateServer': None, 'action_type': False, 'no_clone': False, 'transform': {'type': 'date'}, 'datetime': False, 'min': None, 'max': None, 'time': False, 'date': True}, 'idDg': {'ctype': 'textfield', 'disabled': False, 'readonly': False, 'hidden': False, 'required': False, 'unique': False, 'component': 'Component', 'calculateServer': None, 'action_type': False, 'no_clone': False, 'transform': {}, 'datetime': False, 'min': False, 'max': False}, 'document_type': {'ctype': 'select', 'disabled': False, 'readonly': False, 'hidden': False, 'required': False, 'unique': False, 'component': 'selectComponent', 'calculateServer': None, 'action_type': False, 'no_clone': False, 'transform': {}, 'datetime': False, 'min': False, 'max': False, 'valueProperty': None, 'selectValues': None, 'defaultValue': '', 'multiple': False, 'dataSrc': 'values', 'idPath': '', 'resource_id': '', 'values': [{'label': 'Ordine', 'value': 'ordine'}, {'label': 'Fattura', 'value': 'fattura'}, {'label': 'Incarico', 'value': 'incarico'}, {'label': 'Rda contante', 'value': 'rda_contante'}, {'label': 'Rda Carta Credito', 'value': 'rda_cc'}, {'label': 'Commessa', 'value': 'commessa'}, {'label': 'Rda', 'value': 'rda'}, {'label': 'Reso', 'value': 'reso'}], 'template_label_keys': []}, 'tipi_dettaglio': {'ctype': 'select', 'disabled': True, 'readonly': False, 'hidden': False, 'required': False, 'unique': False, 'component': 'selectComponent', 'calculateServer': None, 'action_type': False, 'no_clone': False, 'transform': {}, 'datetime': False, 'min': False, 'max': False, 'valueProperty': None, 'selectValues': None, 'defaultValue': '', 'multiple': True, 'dataSrc': 'values', 'idPath': '', 'resource_id': '', 'values': [{'label': 'Bene', 'value': 'bene'}, {'label': 'Consumabile', 'value': 'consumabile'}, {'label': 'Servizio', 'value': 'servizio'}], 'template_label_keys': []}}
76+
77+
@classmethod
78+
def all_fields(cls) -> list:
79+
return [{'label': 'Codice', 'tableView': True, 'validate': {'required': True}, 'key': 'rec_name', 'type': 'textfield', 'input': True, 'properties': {}}, {'label': 'stato', 'widget': 'choicesjs', 'tableView': True, 'defaultValue': 'caricato', 'data': {'values': [{'label': 'Caricato', 'value': 'caricato'}, {'label': 'Nuovo', 'value': 'nuovo'}, {'label': 'Parziale', 'value': 'parziale'}, {'label': 'Completato', 'value': 'completato'}, {'label': 'Errore', 'value': 'errore'}]}, 'key': 'stato', 'type': 'select', 'input': True, 'properties': {}, 'validate': {}}, {'label': "Criticita'", 'tableView': True, 'defaultValue': False, 'key': 'anomalia', 'type': 'checkbox', 'input': True, 'properties': {}, 'validate': {}}, {'label': 'Attivo', 'tableView': False, 'defaultValue': False, 'key': 'active', 'logic': [{'name': 'chk user', 'trigger': {'type': 'json', 'json': {'var': 'form.is_admin'}}, 'actions': [{'name': 'display field', 'type': 'property', 'property': {'label': 'Hidden', 'value': 'hidden', 'type': 'boolean'}, 'state': False}]}], 'type': 'checkbox', 'input': True, 'properties': {}, 'validate': {}}, {'label': 'Ext dt Agg.', 'format': 'd/m/Y H:i:S', 'tableView': False, 'enableMinDateInput': False, 'datePicker': {'disableWeekends': False, 'disableWeekdays': False}, 'enableMaxDateInput': False, 'key': 'dtMod', 'type': 'datetime', 'input': True, 'widget': {'type': 'calendar', 'displayInTimezone': 'viewer', 'locale': 'en', 'useLocaleSettings': False, 'allowInput': True, 'mode': 'single', 'enableTime': True, 'noCalendar': False, 'format': 'd/m/Y H:i:S', 'hourIncrement': 1, 'minuteIncrement': 1, 'time_24hr': False, 'minDate': None, 'disableWeekends': False, 'disableWeekdays': False, 'maxDate': None}, 'properties': {}, 'validate': {}}, {'label': 'idDg', 'tableView': False, 'key': 'idDg', 'type': 'textfield', 'input': True, 'properties': {}, 'validate': {}}, {'label': 'Tipo Documento', 'widget': 'choicesjs', 'tableView': True, 'data': {'values': [{'label': 'Ordine', 'value': 'ordine'}, {'label': 'Fattura', 'value': 'fattura'}, {'label': 'Incarico', 'value': 'incarico'}, {'label': 'Rda contante', 'value': 'rda_contante'}, {'label': 'Rda Carta Credito', 'value': 'rda_cc'}, {'label': 'Commessa', 'value': 'commessa'}, {'label': 'Rda', 'value': 'rda'}, {'label': 'Reso', 'value': 'reso'}]}, 'key': 'document_type', 'type': 'select', 'input': True, 'properties': {}, 'validate': {}}, {'label': 'Tipi Dettaglio', 'widget': 'choicesjs', 'disabled': True, 'tableView': True, 'data': {'values': [{'label': 'Bene', 'value': 'bene'}, {'label': 'Consumabile', 'value': 'consumabile'}, {'label': 'Servizio', 'value': 'servizio'}]}, 'key': 'tipi_dettaglio', 'type': 'select', 'multiple': True, 'input': True, 'properties': {}, 'validate': {}}]
80+
81+
@classmethod
82+
def table_columns(cls) -> dict:
83+
return {'rec_name': 'Codice', 'stato': 'stato', 'anomalia': "Criticita'", 'document_type': 'Tipo Documento', 'tipi_dettaglio': 'Tipi Dettaglio'}
84+
85+
@classmethod
86+
def components_ext_data_src(cls):
87+
return []
88+
89+
@classmethod
90+
def get_data_model(cls):
91+
return ""
92+
93+
@classmethod
94+
def conditional(cls) -> {str, dict}:
95+
return {}
96+
97+
@classmethod
98+
def logic(cls) -> {str, list}:
99+
return {'active': [{'name': 'chk user', 'trigger': {'type': 'json', 'json': {'var': 'form.is_admin'}}, 'actions': [{'name': 'display field', 'type': 'property', 'property': {'label': 'Hidden', 'value': 'hidden', 'type': 'boolean'}, 'state': False}]}]}
100+
101+
@classmethod
102+
def conditional(cls) -> {str, dict}:
103+
return {}
104+
105+
@classmethod
106+
def logic(cls) -> {str, list}:
107+
return {}

0 commit comments

Comments
 (0)