{% macro apply_classes(classes) -%} {# Apply css classes inside an element. Usage:
#} {% for css in classes %}{{ css }} {% endfor %} {%- endmacro %} {% macro apply_dattrs(data_attrs) -%} {# Apply data-attributes inside an element. Usage:
TODO: support dictionary with values #} {% for attr in data_attrs %}data-{{ attr }} {% endfor %} {%- endmacro %} {% macro dictlist_dl(data, filterkeys=[], filtervals=[], classes=[], data_attrs=[], asdict=False) %} {# Make a definition list from a dictionary - the correspondence should be dt: key, dd: value Usage: {{ dictlist_dl({'foo': 'bar'}) }}
foo
bar
#} {% if asdict %}{% set data = data._asdict() %}{% endif %}
{% for k, v in data.items() -%} {% if k not in filterkeys and v not in filtervals %}
{{ k }}
{{ v }}
{% endif %} {% endfor %}
{%- endmacro %} {% macro dict2list(data, type='ul', filterkeys=[], filtervals=[], asdict=False) %} {# Makes a list from a dictionary. Usage: {{ dict2list({'foo': 'bar'}) }} namedtuple support: {{ dict2list(mytuple, asdict=True) }} #} {% if asdict %}{% set data = data._asdict() %}{% endif %} <{{ type }}> {% for k, v in data.items() %} {% if k not in filterkeys and v not in filtervals %}
  • {{ k }}: {{ v }}
  • {% endif %} {% endfor %} {%- endmacro %} {% macro bs3_dictlist_group(data, filterkeys=[], filtervals=[], data_attrs=[], asdict=False) %} {# Makes a bootstrap list group from a set of dictionaries Usage: {{ bs3_dictlist_group({'foo': 'bar'}) }}

    foo

    bar

    #} {% if asdict %}{% set data = data._asdict() %}{% endif %}
    {% for k, v in data.items() %} {% if k not in filterkeys and v not in filtervals %}

    {{ k }}

    {{ v }}

    {% endif %} {% endfor %}
    {%- endmacro %} {% macro bs3_list_group(data, type='ul') %} {# OL/UL based bootstrap list group Usage: {{ bs3_list_group(['foo', 'bar']) }} #} <{{ type }} class="list-group"> {% for val in data %}
  • {{ val }}
  • {% endfor %} {%- endmacro %} {% macro dict2labels(data, filterkeys=[], filtervals=[], aslist=False) %} {# Makes a dict of `name`:`label` into bootstrap labels Usage: {{ dict2labels({'foo': 'danger'}) }} {{ dict2labels({'foo': 'danger'}, aslist=False) }} foo #} {% if aslist %}{% endif %} {%- endmacro %} {% macro list2list(data, type='ul', filtervals=[], icons={}, classes=[], icondir='left') %} {# Makes a OL/UL from a list, with optional icons Usage: {{ list2list(['Toyota', 'V2', '747', 'John Deere'], icons={'Toyota': ['fa', 'fa-car'], 'V2': ['fa', 'fa-rocket']}, icondir='right') }} Returns: #} <{{ type }} class="{{ apply_classes(classes) }}"> {% for item in data %} {% if item and item not in filtervals %}
  • {% if item in icons.keys() %} {% if icondir == 'left' %} {{ item }} {% else %} {{ item }} {% endif %} {% else %} {{ item }} {% endif %}
  • {% endif %} {% endfor %} {%- endmacro %} {% macro dictlist2nav(data, type='ul', filterkeys=[], filtervals=[], classes=[], data_attrs=[]) %} {# Make a list of links with nav element. Format must be a list of dictionaries. Supports *one* level of nesting. #} {% if asdict %}{% set data = data._asdict() %}{% endif %} {%- endmacro %} {% macro dictlist2dropdown(data, name=None, filterkeys=[], filtervals=[], classes=[], data_attrs=[], asdict=False) %} {# Make a dropdown element. Format must be a list of dictionaries. #} {% if asdict %}{% set data = data._asdict() %}{% endif %} {%- endmacro %} {% macro list2dropdown(data, filtervals=[], classes=[], data_attrs=[]) %} {# Make a dropdown element. Format must be a list. Value and text are the same. #} {%- endmacro %} {% macro dictlist2checkboxes(data, fieldset_class='fieldset-group', filterkeys=[], filtervals=[], data_attrs=[], asdict=False) %} {# Make a checkbox group, where keys are input names, and values are labels. Format must be a list of dictionaries. #} {% if asdict %}{% set data = data._asdict() %}{% endif %}
    {% for item in data %} {% for k, v in item.items() %} {% if k not in filterkeys and v not in filtervals %} {% endif %} {% endfor %} {% endfor %}
    {%- endmacro %} {% macro objects2table(objs, classes=[], data_attrs=[], filterkeys=[], filtervals=[], pk_link=None, handle_links=True, field_macros={}, header_macros={}, asdict=False ) -%} {# Create table from a list of objects (class instantiations). Also add links for primary keys if specified. #} {% if asdict %}{% set data = data._asdict() %}{% endif %} {% for obj in objs %} {% if loop.first %} {% set header_keys = header_macros.keys() %} {% for heading, _ in obj.items() %} {# Allow filtering of headings. #} {% if heading not in filterkeys %} {% if heading in header_keys %} {% else %} {% endif %} {% endif %} {% endfor %} {% endif %} {% endfor %} {% for obj in objs %} {% for k, v in obj.items() %} {# Allowing filtering of keys and values. #} {% if k not in filterkeys %} {% if v not in filtervals %} {% else %} {% endif %} {% endif %} {% endfor %} {% endfor %}
    {{ header_macros[heading](heading) }}{{ heading }}
    {# Handle all primary key links, a common occurence #} {% if pk_link and k == 'id' %} {{ v }} {% elif handle_links and v|is_url %} {{ v }} {% elif k in field_macros.keys() %} {# If a field macro is specified by key, call it on this field for arbitrary levels of customization #} {{ field_macros[k](v) }} {% else %} {{ v }} {% endif %}
    {%- endmacro %} {% macro wtform_errors_field(field, errors, bg=True) -%} {% if bg %}
    {% endif %}

    Error(s) for '{{ field }}':

    {% if bg %}
    {% endif %} {%- endmacro %} {% macro wtform_errors(formobj) -%} {# Show a list of form errors based on a given wtform instance. #} {% if formobj.errors %} {% for field, errors in formobj.errors.items() %} {{ wtform_errors_field(field, errors) }} {% endfor %} {% endif %} {%- endmacro %} {% macro wtform_form(formobj, action='.', method='GET', classes=[], data_attrs=[], btn_classes=[], uploads=True, enctype=None, btn_text='Submit', formid=None, reset_btn=True, legend=None, horizontal=False, align='left', questionize=True, linebreaks=True, button_wrapper=True, reset_btn_classes=[], field_macros={} ) -%}
    {% set invalid_fields = ['CSRFTokenField', 'HiddenField'] %}
    {% if legend %} {{ legend }} {% endif %} {% for field in formobj %} {% if field.name in field_macros.keys() %} {{ field_macros[field.name](field) }} {% else %} {% set valid_field = field.type not in invalid_fields %} {% if horizontal %}
    {% endif %} {# Only show labels and descriptions if they are normal fields. #} {% if valid_field %} {% if horizontal %}
    {% endif %} {{ field.label }}{% if field.type == 'BooleanField' and questionize %}?{% endif %} {% if field.description %} {% if horizontal and linebreaks %}
    {% endif %} {{ field.description }} {% endif %} {% if field.flags.required %} {% if horizontal and linebreaks %}
    {% endif %} * Required {% if linebreaks %}
    {% endif %} {% endif %} {% if horizontal %}
    {% endif %} {% endif %} {% if horizontal and valid_field %}
    {% endif %} {% if horizontal %}
    {% endif %} {{ field }} {% if valid_field and linebreaks %}
    {% endif %} {% if field.errors %} {{ wtform_errors_field(field.label, field.errors, bg=False) }} {% endif %} {% if horizontal %}
    {% endif %} {% if valid_field and linebreaks %}
    {% endif %} {% if horizontal and valid_field %}
    {% endif %} {% if horizontal %}

    {% endif %} {% endif %} {% endfor %}
    {% if button_wrapper %}
    {% endif %} {# Add typical form submit button. #} {# Add a reset field values button if specified. #} {% if reset_btn %} {% endif %} {% if button_wrapper %}
    {% endif %}
    {%- endmacro %} {%- macro recurse_dictlist(val, type='ul', classes=[], data_attrs=[]) %} {# Uses the jinja2 recursive looping to recurse over a dictionary and display as a list (ordered or unordered), or display a default value otherwise. #} {% if val is mapping %} <{{ type }} class="{{ apply_classes(classes) }}" {{ apply_dattrs(data_attrs) }}> {% for k, v in val.items() recursive %} {% if v is mapping %}
  • {{ k }}: <{{ type }} class="{{ apply_classes(classes) }}" {{ apply_dattrs(data_attrs) }}> {{ loop(v.items()) }}
  • {% else %}
  • {% if v|islist %} <{{ type }} class="{{ apply_classes(classes) }}" {{ apply_dattrs(data_attrs) }}> {% for item in v %}
  • {% for itemv in v recursive %} {{ recurse_dictlist(itemv, type=type) }} {% endfor %} {% endfor %}
  • {% else %} {{ k }}: {{ v }} {% endif %} {% endif %} {% endfor %} {% else %} {{ val }} {% endif %} {%- endmacro %} {% macro bs3_label(value, label_map) -%} {# Convert a dict of values with corresponding label types given a value, to the appropriate label. Usage: {{ bs3_label('PROD', {'dev': 'info', 'stage': 'warning', 'prod': 'danger'}) }} PROD #} {{ value }} {%- endmacro %}