Skip to content

Commit 93b8bd8

Browse files
committed
support for ui:* attributes in models for JSON-schema export
1 parent e1f608e commit 93b8bd8

File tree

4 files changed

+18
-7
lines changed

4 files changed

+18
-7
lines changed

datamodel/base.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -431,6 +431,13 @@ def schema(cls, as_dict=False):
431431
if field.metadata.get('required', False):
432432
required.append(name)
433433

434+
# UI objects:
435+
ui_objects = {
436+
k.replace('_', ':'): v for k, v in field.metadata.items() if k.startswith('ui_')
437+
}
438+
# schema_extra:
439+
schema_extra = field.metadata.get('schema_extra', {})
440+
434441
fields[name] = {
435442
"type": type_info,
436443
"nullable": field.metadata.get('nullable', False),
@@ -440,7 +447,9 @@ def schema(cls, as_dict=False):
440447
"format": field.metadata.get('format', None),
441448
},
442449
"readOnly": field.metadata.get('readonly', False),
443-
"writeOnly": False
450+
"writeOnly": False,
451+
**ui_objects,
452+
**schema_extra
444453
}
445454

446455
if 'pattern' in field.metadata:

datamodel/fields.pyx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -157,8 +157,8 @@ class Field(ff):
157157
# args["metadata"] = self._meta
158158
self.metadata = (
159159
_EMPTY_METADATA
160-
if metadata is None else
161-
MappingProxyType(metadata)
160+
if self._meta is None else
161+
MappingProxyType(self._meta)
162162
)
163163
self.default_factory = MISSING
164164
if default is None:

datamodel/version.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
"""AsyncDB Meta information."""
1+
"""DataModel Meta information."""
22

33
__title__ = 'python-datamodel'
44
__description__ = ('simple library based on python +3.8 to use Dataclass-syntax'
55
'for interacting with Data')
6-
__version__ = '0.6.4'
6+
__version__ = '0.6.5'
77
__author__ = 'Jesus Lara'
88
__author_email__ = '[email protected]'
99
__license__ = 'BSD'

examples/schema.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,9 @@ class User(BaseModel):
2626
first_name: Text = Column(required=True, max=254, label="First Name", pattern="^\w*$")
2727
last_name: str = Column(required=True, max=254, label="Last Name")
2828
email: str = Column(required=False, max=254, label="User's Email")
29-
password: str = Column(required=False, max=16, secret=True, widget='/properties/password')
29+
password: str = Column(
30+
required=False, max=16, secret=True, ui_widget='/properties/password'
31+
)
3032
last_login: datetime = Column(required=False, format='YYYY-MM-DD', readonly=True)
3133
username: str = Column(required=False)
3234
user_type: UserType = Column(required=False)
@@ -37,7 +39,7 @@ class User(BaseModel):
3739
title: str = Column(equired=False, max=90)
3840
registration_key: str = Column(equired=False, max=512, repr=False, readonly=True)
3941
reset_pwd_key: str = Column(equired=False, max=512, repr=False, readonly=True)
40-
avatar: Text = Column(max=512, repr=False)
42+
avatar: Text = Column(max=512, repr=False, ui_widget='ImageUploader', ui_help='User Avatar, Hint: please upload a PNG file.')
4143
associate_id: str = Column(required=False, repr=False)
4244
group_id: list[int] = Column(required=False)
4345
groups: list = Column(required=False)

0 commit comments

Comments
 (0)