def formClassFactory(name, model, baseclass):
''' Generates a new WTForm class based on SQLalchemy model class. Each class contains as attributes
Meta = a class called Meta. Meta.model contains the SQLalchemy ModelClass
data = a dictionary of parameters: form input that gets mapped to the sqlalchemy parameter
errors = a dictionary of errors returned by invalid form validation
validate = a method to validate all elements in this form
parameter_X = a WTForm Field mapped to respective sqlalchemy table column
e.g.
The ModelClass IFUDesign mapped to mangadatadb.ifu_design sql table gets transformed into
WTForm IFUDesignForm, with IFUDesignForm.Meta.model = marvin.db.models.DataModelClasses.IFUDesign
'''
Meta = type('Meta', (object,), {'model': model})
newclass = type(name, (baseclass,), {'Meta': Meta})
return newclass
# build a wtform select field for operators; tested but no longer used ; can't seem to attach operator field to every individual parameter
python类Field()的实例源码
def valid_json_check(form, field):
if type(field.data) != "dict":
try:
json_object = json.loads(field.data)
except ValueError, e:
raise ValidationError('Field must be valid JSON')
def _get_matches(self, keySplits):
''' Get the matches from a set of key splits '''
matches = [path for path in self
if all([keySplits[-1 - ii] == path.split('.')[-1 - ii]
for ii in range(len(keySplits))])]
return matches
# Custom [Better]TagList Field for Search Parameter Box
def __init__(self, opstring='[<>=]', message=None):
self.opstring = opstring
if not message:
message = u'Field must contain at least a valid operand of {0}.'.format(self.opstring)
self.message = message
def create_field(self) -> Field:
raise NotImplementedError