{% macro form_field(field, field_name, parent_id) %} {%- set ns=namespace(minlength=False, maxlength=False, pattern=False) %} {%- set required=field.is_required() %} {%- if field.metadata %} {%- for meta in field.metadata %} {%- if meta.min_length %}{% set ns.minlength=meta.min_length %}{% endif %} {%- if meta.max_length %}{% set ns.maxlength=meta.max_length %}{% endif %} {%- if meta.pattern %}{% set ns.pattern=meta.pattern %}{% endif %} {%- endfor %} {%- endif %} {%- set closing_tag=False %} {%- set input_type=None %} {% if field.annotation.__args__ %} {% set annotation = field.annotation.__args__[0] %} {% else %} {% set annotation = field.annotation %} {% endif %} {% set autocomplete=field.json_schema_extra.autocomplete %} {%- if field.json_schema_extra and field.json_schema_extra.get('input_type', None) == 'textarea' %} {%- set element="textarea" %} {%- set closing_tag=True %} {%- set cls="form-textarea form-input" %} {%- elif annotation.__members__ %} {%- set element="select" %} {%- set closing_tag=True %} {%- set cls="form-select form-input" %} {%- elif annotation.__name__ == "bool" %} {%- set element="input" %} {%- set input_type="checkbox" %} {%- set checked=field.default is true %} {%- set cls="form-checkbox form-input" %} {%- elif annotation.__name__ == "datetime" %} {%- set element="input" %} {%- set input_type="datetime-local" %} {%- set cls="form-datetime form-input" %} {%- else %} {%- set element="input" %} {%- set cls="form-input" %} {%- endif -%}
{% if ns.minlength %}min length: {{ ns.minlength }}{% endif %} {% if ns.maxlength %}max length: {{ ns.maxlength }}{% endif %} {% if ns.pattern %}matching: {{ ns.pattern }}{% endif %} {% if required %}required{% endif %}
{%- if field.json_schema_extra.input_type == "tokens" %} {{ token_input(field, field_name, parent_id) }} {%- elif field.json_schema_extra.input_type == "model_list" %}
{{ add_subform_button(parent_id, field_name, 0, field.json_schema_extra.model_name) }}
{% else %} <{{element}} id="{{ parent_id }}-{{ field_name }}" name="{{ field_name }}" class="{{ cls }}{% if not required %} optional{% endif %}" {%- if input_type %} type="{{ input_type }}"{% endif %} {%- if ns.minlength %} minlength="{{ ns.minlength }}"{% endif %} {%- if ns.maxlength %} maxlength="{{ ns.maxlength }}"{% endif %} {%- if ns.pattern %} pattern="{{ ns.pattern }}"{% endif %} {%- if required %} required{% endif %} {%- if checked %} checked{% endif %} {%- if autocomplete %} hx-get="/autocomplete/{{ autocomplete }}" hx-target="#{{ parent_id }}-{{ field_name }}-autocomplete" hx-trigger="input changed delay:250ms" list="{{ parent_id }}-{{ field_name }}-autocomplete" {%- endif %} > {%- if element=="select" %} {% for member in field.annotation.__members__.values() %} {% endfor %} {%- endif %} {%- if closing_tag %}{% endif %} {% if autocomplete %}{% endif %} {% endif %}
{% if field.description or (annotation.__members__ and annotation.__annotations__) %}
{%- if annotation.__members__ and annotation.__annotations__ %}
Show/Hide Option Description {%- for key, val in annotation.__annotations__.items() %} {%- endfor %}
Option Description
{{ key }} {{ val.__metadata__[-1] }}
{%- endif %} {% if field.description %}

{{ field.description }}

{% endif %}
{% endif %} {% endmacro %} {% macro token_input(field, field_name, parent_id) %}
{% endmacro %} {% macro add_subform_button(parent_id, field_name, idx, model_name) %} + {% endmacro %} {% macro inner_model_form(id, model, exclude = []) %} {% for field_name, field in model.model_fields.items() if field_name not in exclude %} {% if not field.json_schema_extra or field.json_schema_extra and field.json_schema_extra.get('input_type', None) != 'none' %}
{{ form_field(field, field_name, id) }}
{% else %} {% endif %} {% endfor %} {% endmacro %} {% macro model_form(id, model, exclude = []) %}
{{ inner_model_form(id, model, exclude) }}
{% endmacro %}