This repository has been archived by the owner on Oct 18, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconverter.py
215 lines (179 loc) · 7.49 KB
/
converter.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
"""
Tools for generating forms based on Django models.
"""
import datetime
import decimal
import uuid
from collections import OrderedDict, namedtuple
from pony.orm.core import EntityMeta
from pony.orm.ormtypes import (FloatArray, IntArray, Json, LongStr, LongUnicode, StrArray)
from wtforms import fields as wtf_fields
from wtforms import validators
from wtforms.fields.core import DecimalField, FloatField, IntegerField, StringField
from wtforms.fields.html5 import DateField, DateTimeField, TimeField
from wtforms.fields.simple import TextAreaField
from ._compact import text_type
from .fields import (BooleanSelectField, FloatListField, IntListField,
ModelSelectField, ModelSelectMultipleField,
SelectChoicesField, StrListField, JSONField)
from .utils import get_attr_entity_class, UniqueValidator, ValueRequired
FieldInfo = namedtuple('FieldInfo', ('name', 'field'))
def handle_null_filter(data):
if data == '':
return None
return data
default_converters = OrderedDict((
# Standard types
(text_type, StringField),
(int, IntegerField),
(float, FloatField),
(decimal.Decimal, DecimalField),
# Dates, times
(datetime.date, DateField # WPDateField
),
(datetime.datetime, DateTimeField # WPDateTimeField
),
(datetime.time, TimeField # WPTimeField
),
(bool, BooleanSelectField),
(LongStr, TextAreaField),
(LongUnicode, TextAreaField),
(uuid.UUID, StringField),
(IntArray, IntListField),
(StrArray, StrListField),
(FloatArray, FloatListField),
(Json, JSONField)
))
coerce_defaults = {
int: int,
str: text_type,
decimal: float,
}
class ModelConverter(object):
"""convert entity attribute to Wtf-Field
"""
def __init__(self, additional=None, additional_coerce=None, overrides=None):
self.converters = {}
if additional:
self.converters.update(additional)
self.coerce_settings = dict( coerce_defaults )
if additional_coerce:
self.coerce_settings.update(additional_coerce)
self.overrides = overrides or {}
def set_required_validator(self, attr, kwargs):
if attr.is_required:
kwargs['validators'].append(validators.Required())
else :
kwargs['validators'].append(validators.Optional())
# further
if attr.is_relation:
pass
else:
if (attr.nullable or (attr.default is not None)) :
kwargs['validators'].append(validators.Optional())
else:
kwargs['validators'].append(ValueRequired())
def set_unique_validator(self, entity, attr, kwargs):
if attr.is_unique :
kwargs['validators'].append(UniqueValidator(entity, attr.name))
def add_null_filter(self, attr, kwargs):
if not attr.is_required and attr.nullable:
# Treat empty string as None when converting.
kwargs['filters'].append(handle_null_filter)
def is_filter_allowed(self, field, kwargs):
if issubclass(field, wtf_fields.FormField):
kwargs.pop('filters')
def build_kwargs(self, entity, attr, field_args):
field_args = field_args or {}
kwargs = {
'label': attr.name,
'validators': [],
'filters': [],
'default': attr.default,
#'description': attr.help_text
}
if field_args:
kwargs.update(field_args)
# {"validators": [Email], "only_validators": True }
if not kwargs.pop("only_validators", False):
self.set_required_validator(attr, kwargs)
self.set_unique_validator(entity, attr , kwargs)
self.add_null_filter(attr, kwargs)
return kwargs
def get_custom(self, entity, attr, kwargs):
if attr.name in self.overrides:
self.is_filter_allowed(self.overrides[attr.name], kwargs)
return FieldInfo(attr.name, self.overrides[attr.name](**kwargs))
if hasattr(attr, 'wtf_field'):
self.is_filter_allowed(attr.wtf_field(entity, **kwargs), kwargs)
return FieldInfo(attr.name, attr.wtf_field(entity, **kwargs))
def handle_select_fields(self,attr, kwargs):
choices = kwargs.pop('choices',
getattr(attr, 'kwargs', []))
coerce_fn = None
for converter in default_converters:
if converter in self.coerce_settings:
coerce_fn = self.coerce_settings[converter]
if 'coerce' in kwargs:
coerce_fn = kwargs.pop('coerce',
self.coerce_settings[converter])
allow_blank = kwargs.pop(
'allow_blank', not attr.is_required)
kwargs.update({
'choices': choices,
'coerce': coerce_fn,
'allow_blank': allow_blank})
return FieldInfo(attr.name, SelectChoicesField(**kwargs))
def convert(self, entity: EntityMeta, attr, field_args):
"""return Wtf-Field based on the entity attribute given
Args:
entity (EntityMeta): [The class of the db.entity ]
attr ([type]):
[the attribute instance ]
field_args ([type]): [Arguments ]
Raises:
AttributeError: [description]
Returns:
[type]: [description]
"""
kwargs = self.build_kwargs( entity, attr ,field_args, )
custom = self.get_custom(entity, attr, kwargs)
if custom:
return custom
# converter : { attr_type: fieldInfo }
for converter in self.converters:
if isinstance(attr, converter):
self.is_filter_allowed(
self.converters[converter], kwargs)
return self.converters[converter](entity, attr, **kwargs)
if "choices" in kwargs or 'choices' in attr.kwargs :
self.is_filter_allowed(attr.wtf_field(entity, **kwargs), kwargs)
return self.handle_select_fields(attr, kwargs)
if attr.is_relation :
coerce_fn = None
for converter in default_converters:
if converter in self.coerce_settings:
coerce_fn = self.coerce_settings[converter]
if 'coerce' in kwargs:
coerce_fn = kwargs.pop('coerce',
self.coerce_settings[converter])
allow_blank = kwargs.pop('allow_blank', not attr.is_required)
model = get_attr_entity_class(attr.reverse)
kwargs.update({
# fix bug ,
#'coerce': coerce_fn,
'allow_blank': allow_blank,
'model': model,
})
if attr.is_collection :
return FieldInfo(attr.name,
# label=None, validators=None, model=None, **kwargs
ModelSelectMultipleField(**kwargs))
else:
return FieldInfo(attr.name, ModelSelectField(**kwargs))
try:
converter = default_converters[attr.py_type]
except Exception:
raise AttributeError("There is not possible conversion for '%s' " % type(attr.py_type))
self.is_filter_allowed( converter, kwargs)
return FieldInfo(attr.name, converter(**kwargs))