-
Notifications
You must be signed in to change notification settings - Fork 1
/
model.py
56 lines (39 loc) · 1.64 KB
/
model.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
from django.db.models.fields import NOT_PROVIDED
from django.utils import simplejson as json
from hyson.ordereddict import OrderedDict
from hyson.field_types import FIELD_TYPES
from hyson.utils import dejsonize
def convert(klass):
model_name = klass.__name__
fields = list()
validators = list()
for field in klass._meta._fields():
field_class = field.__class__.__name__
ext_type = FIELD_TYPES[field_class]
field_dict = {
'name': field.name,
'type': ext_type
}
if ext_type == 'date':
field_dict['dateFormat'] = 'timestamp'
if hasattr(field, "default"):
if field.default != NOT_PROVIDED:
field_dict["defaultValue"] = field.default
fields.append(field_dict)
if hasattr(field, "max_length") and field.max_length:
validators.append({'type': 'length', 'field': field.name, 'max': field.max_length})
if hasattr(field, "_get_choices"):
choices = field._get_choices()
if len(choices):
validators.append({'type': 'inclusion', 'field': field.name, 'list': [choice[1] for choice in choices]})
if hasattr(field, "_get_choices") and not field.blank:
validators.append({'type': 'presence', 'field': field.name})
model = OrderedDict()
model['extend'] = 'Ext.data.Model'
model['fields'] = fields
model['validations'] = validators
model_str = """Ext.define('%(model_name)s', %(model)s);""" % {
'model_name': model_name,
'model': json.dumps(model, ensure_ascii=False, indent=" " * 4)
}
return dejsonize(model_str)