Add ability to override schema node for custom deform fields

This commit is contained in:
Lance Edgar 2017-09-23 16:25:37 -05:00
parent 3361adf08a
commit 7cb4664018

View file

@ -175,7 +175,7 @@ class Form(object):
"""
def __init__(self, fields=None, schema=None, request=None, readonly=False, readonly_fields=[],
model_instance=None, model_class=None, enums={}, labels={}, renderers={}, widgets={},
model_instance=None, model_class=None, nodes={}, enums={}, labels={}, renderers={}, widgets={},
action_url=None, cancel_url=None):
self.fields = list(fields) if fields is not None else None
@ -189,6 +189,7 @@ class Form(object):
self.model_class = type(self.model_instance)
if self.model_class and self.fields is None:
self.fields = self.make_fields()
self.nodes = nodes or {}
self.enums = enums or {}
self.labels = labels or {}
self.renderers = renderers or {}
@ -247,7 +248,10 @@ class Form(object):
# association proxy fields, not sure how other fields will behave
for field in includes:
if field not in schema:
schema.add(colander.SchemaNode(colander.String(), name=field))
node = self.nodes.get(field)
if not node:
node = colander.SchemaNode(colander.String(), name=field, missing='')
schema.add(node)
# apply any label overrides
for key, label in self.labels.items():
@ -276,6 +280,9 @@ class Form(object):
if key in self.readonly_fields:
self.readonly_fields.remove(key)
def set_node(self, key, node):
self.nodes[key] = node
def set_type(self, key, type_):
if type_ == 'datetime':
self.set_renderer(key, self.render_datetime)
@ -307,6 +314,18 @@ class Form(object):
schema = self.make_schema()
schema[key].validator = validator
def set_required(self, key, required):
"""
Set whether or not value is required for a given field.
"""
raise NotImplementedError # TODO
def set_default(self, key, value):
"""
Set the default value for a given field.
"""
raise NotImplementedError # TODO
def render(self, template=None, **kwargs):
if not template:
if self.readonly: