Commit 9a1dc4e4 authored by Serge S. Koval's avatar Serge S. Koval

Allow widget rendering customization through form_widget_args property

parent 3d957d9f
......@@ -259,6 +259,22 @@ class BaseModelView(BaseView, ActionsMixin):
form_overrides = dict(name=wtf.FileField)
"""
form_widget_args = None
"""
Dictionary of form widget rendering arguments.
Use this to customize how widget is rendered without using custom template.
Example::
class MyModelView(BaseModelView):
form_widget_args = {
'description': {
'rows': 10,
'style': 'color: black'
}
}
"""
# Actions
action_disallowed_list = ObsoleteAttr('action_disallowed_list',
'disallowed_actions',
......@@ -327,6 +343,9 @@ class BaseModelView(BaseView, ActionsMixin):
self._create_form_class = self.get_create_form()
self._edit_form_class = self.get_edit_form()
if self.form_widget_args is None:
self.form_widget_args = {}
# Search
self._search_supported = self.init_search()
......@@ -569,7 +588,7 @@ class BaseModelView(BaseView, ActionsMixin):
def get_list(self, page, sort_field, sort_desc, search, filters):
"""
Return a paginated and sorted list of models from the data source.
Must be implemented in the child class.
:param page:
......@@ -902,6 +921,7 @@ class BaseModelView(BaseView, ActionsMixin):
return self.render(self.create_template,
form=form,
form_widget_args=self.form_widget_args,
return_url=return_url)
@expose('/edit/', methods=('GET', 'POST'))
......@@ -931,6 +951,7 @@ class BaseModelView(BaseView, ActionsMixin):
return self.render(self.edit_template,
form=form,
form_widget_args=self.form_widget_args,
return_url=return_url)
@expose('/delete/', methods=('POST',))
......
......@@ -73,7 +73,7 @@
{% endif %}
{%- endmacro %}
{% macro render_field(form, field, set_focus=False) %}
{% macro render_field(form, field, set_focus=False, kwargs={}) %}
<div class="control-group{{ ' error' if field.errors }}">
<div class="control-label">
{{ field.label.text }}
......@@ -86,9 +86,9 @@
<div class="controls">
<div>
{% if set_focus %}
{{ field(autofocus='autofocus')|safe }}
{{ field(autofocus='autofocus', **kwargs)|safe }}
{% else %}
{{ field()|safe }}
{{ field(**kwargs)|safe }}
{% endif %}
</div>
{% if field.description %}
......@@ -105,11 +105,12 @@
</div>
{% endmacro %}
{% macro render_form_fields(form, set_focus=True) %}
{% macro render_form_fields(form, set_focus=True, widget_args={}) %}
{{ form.hidden_tag() if form.hidden_tag is defined }}
{% for f in form if f.type != 'HiddenField' and f.type != 'CSRFTokenField' %}
{{ render_field(form, f, not loop.index0 and set_focus) }}
{% set kwargs = widget_args.get(f.name, {}) %}
{{ render_field(form, f, not loop.index0 and set_focus, kwargs) }}
{% endfor %}
{% endmacro %}
......@@ -135,9 +136,9 @@
</div>
{% endmacro %}
{% macro render_form(form, cancel_url, extra=None) -%}
{% macro render_form(form, cancel_url, extra=None, widget_args={}) -%}
{% call form_tag() %}
{{ render_form_fields(form) }}
{{ render_form_fields(form, widget_args) }}
{{ render_form_buttons(cancel_url, extra) }}
{% endcall %}
{% endmacro %}
......
......@@ -21,7 +21,7 @@
{% endmacro %}
{% call lib.form_tag() %}
{{ lib.render_form_fields(form) }}
{{ lib.render_form_fields(form, widget_args=form_widget_args) }}
{{ lib.render_form_buttons(return_url, extra()) }}
{% endcall %}
{% endblock %}
......
......@@ -8,7 +8,7 @@
{% block body %}
{% call lib.form_tag() %}
{{ lib.render_form_fields(form) }}
{{ lib.render_form_fields(form, widget_args=form_widget_args) }}
{{ lib.render_form_buttons(return_url) }}
{% endcall %}
{% endblock %}
......
......@@ -291,6 +291,7 @@ def test_form():
# TODO: form_columns
# TODO: form_excluded_columns
# TODO: form_args
# TODO: form_widget_args
pass
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment