Skip to content

Commit

Permalink
adding more metadata information
Browse files Browse the repository at this point in the history
  • Loading branch information
phenobarbital committed Sep 23, 2024
1 parent 71e4eff commit 90a90f5
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 10 deletions.
40 changes: 32 additions & 8 deletions datamodel/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -562,21 +562,24 @@ def schema(cls, as_dict=False, locale: Any = None):
else:
ref_info = {}

minimum = field.metadata.get('min', None)
maximum = field.metadata.get('max', None)
secret = field.metadata.get('secret', None)
_metadata = field.metadata.copy()

minimum = _metadata.pop('min', None)
maximum = _metadata.pop('max', None)
secret = _metadata.pop('secret', None)

# custom endpoint for every field:
custom_endpoint = field.metadata.get('endpoint', None)
custom_endpoint = _metadata.pop('endpoint', None)

if field.metadata.get('required', False) or field.metadata.get('primary', False):
required.append(name)

# UI objects:
ui_objects = {
k.replace('_', ':'): v for k, v in field.metadata.items() if k.startswith('ui_')
k.replace('_', ':'): v for k, v in _metadata.items() if k.startswith('ui_')
}
# schema_extra:
schema_extra = field.metadata.get('schema_extra', {})
schema_extra = _metadata.pop('schema_extra', {})
meta_description = cls._get_metadata(
cls,
field,
Expand All @@ -602,14 +605,35 @@ def schema(cls, as_dict=False, locale: Any = None):
fields[name]["endpoint"] = custom_endpoint

if 'write_only' in field.metadata:
fields[name]["writeOnly"] = field.metadata.get('write_only', False)
fields[name]["writeOnly"] = _metadata.pop('write_only', False)

if 'pattern' in field.metadata:
fields[name]["attrs"]["pattern"] = field.metadata['pattern']
fields[name]["attrs"]["pattern"] = _metadata.pop('pattern')

if field.repr is False:
fields[name]["attrs"]["visible"] = False

_meta = {}
_rejected = [
'required',
'nullable',
'primary',
'readonly',
'label',
'validator',
'encoder',
'decoder'
]
for key, val in _metadata.items():
if key not in _rejected:
_meta[key] = val

if _meta:
fields[name]["attrs"] = {
**fields[name]["attrs"],
**_meta
}

if field.default:
d = field.default
if is_callable(d):
Expand Down
2 changes: 1 addition & 1 deletion datamodel/version.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
__title__ = 'python-datamodel'
__description__ = ('simple library based on python +3.8 to use Dataclass-syntax'
'for interacting with Data')
__version__ = '0.7.1'
__version__ = '0.7.2'
__author__ = 'Jesus Lara'
__author_email__ = '[email protected]'
__license__ = 'BSD'
2 changes: 1 addition & 1 deletion examples/json_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class Airport(BaseModel):
required=True, label="airport"
)
city: str = Column(
required=False, label="city"
required=False, label="city", multiple=True
)
country: str = Column(
required=False, label="country"
Expand Down

0 comments on commit 90a90f5

Please sign in to comment.