Add custom FieldList class for forms2 field list

This commit is contained in:
Lance Edgar 2017-11-22 11:21:28 -06:00
parent 4c189f2fcc
commit 40d2251844

View file

@ -259,7 +259,7 @@ class Form(object):
model_instance=None, model_class=None, nodes={}, enums={}, labels={}, renderers={},
widgets={}, defaults={}, validators={}, required={}, action_url=None, cancel_url=None):
self.fields = list(fields) if fields is not None else None
self.fields = FieldList(fields) if fields is not None else None
self.schema = schema
self.request = request
self.readonly = readonly
@ -291,9 +291,9 @@ class Form(object):
mapper = orm.class_mapper(self.model_class)
# first add primary column fields
fields = [prop.key for prop in mapper.iterate_properties
if not prop.key.startswith('_')
and prop.key != 'versions']
fields = FieldList([prop.key for prop in mapper.iterate_properties
if not prop.key.startswith('_')
and prop.key != 'versions'])
# then add association proxy fields
for key, desc in sa.inspect(self.model_class).all_orm_descriptors.items():
@ -574,3 +574,17 @@ class Form(object):
def validate(self, *args, **kwargs):
form = self.make_deform_form()
return form.validate(*args, **kwargs)
class FieldList(list):
"""
Convenience wrapper for a form's field list.
"""
def insert_before(self, field, newfield):
i = self.index(field)
self.insert(i, newfield)
def insert_after(self, field, newfield):
i = self.index(field)
self.insert(i + 1, newfield)