From 40d2251844972ec95f9ddf6d688ed3cb4e1da317 Mon Sep 17 00:00:00 2001 From: Lance Edgar Date: Wed, 22 Nov 2017 11:21:28 -0600 Subject: [PATCH] Add custom `FieldList` class for forms2 field list --- tailbone/forms2/core.py | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/tailbone/forms2/core.py b/tailbone/forms2/core.py index 0702b73e..7c2a8918 100644 --- a/tailbone/forms2/core.py +++ b/tailbone/forms2/core.py @@ -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)