{# Field-level error message. The id is what `aria-describedby` on the control points at, so the message is read out when focus reaches the field rather than only being visible. Errors also carry an icon + "Error:" prefix so the failure is not signalled by the red colour alone (WCAG 1.4.1), and role="alert" so a message that arrives in an Unpoly fragment swap is announced. #} {% macro field_error(construct, error_id, error) %} {% if error %} {{ icon("cancel", label=construct.t("crud-error-prefix")) }} {{ construct.t(error.key, error.args) }} {% endif %} {% endmacro %} {# Attributes tying a control to its help text and error message. Emitted on the control itself so the association survives however the field is laid out. Help comes first so it is read before the error. #} {% macro error_attrs(error_id, error, help_id=none) %} {%- set ids = ([help_id] if help_id else []) + ([error_id] if error else []) -%} {%- if error %} aria-invalid="true"{% endif -%} {%- if ids %} aria-describedby="{{ ids | join(' ') }}"{% endif -%} {% endmacro %} {# A field's `help=` text: visible, persistent guidance referenced by the control's aria-describedby (pass the same id to `error_attrs`). Unlike a placeholder it stays put while the user types and is read out by screen readers. Callers derive the id as `name ~ '-help' if type.has_help() else none`. A field typed in a set form (a Date with input="text") adds that form, "Format: dd/mm/yyyy", to it, so it is shown and read out with the field. It goes between the label and the control ("above", the default) or after the control and any suffix ("below"), per the field's `help_position` or else CONSTRUCT_FIELD_HELP_POSITION. A widget calls this twice, with at="above" before the control and at="below" after it; only the call that matches renders, so the id is never doubled. at=none renders regardless, for spots that have only one place for it (a checkbox's help, a column header). #} {% macro field_help(construct, help_id, type, at="above") %} {%- set format = type.input_format() -%} {%- if (type.help or format) and (at is none or (type.help_position or construct.field_help_position) == at) -%} {%- if type.help %}{{ construct.t(type.help) }}{% endif -%} {%- if format %}{% if type.help %} {% endif %}{{ construct.t("crud-date-format", {"format": format}) }}{% endif -%} {%- endif -%} {% endmacro %} {# The whole placeholder attribute for a text-like control, or nothing. The field's own `placeholder=` when set; `placeholder=""` switches it off; with none set, the generic "enter " only when CONSTRUCT_DEFAULT_PLACEHOLDER is on (off by default: a placeholder that repeats the label adds nothing, fails contrast and reads as a filled-in value). #} {% macro placeholder_attr(construct, type) -%} {%- if type.placeholder is not none -%} {%- if type.placeholder != "" %}placeholder="{{ construct.t(type.placeholder) }}"{% endif -%} {%- elif construct.default_placeholder -%} placeholder="{{ construct.t("crud-enter-field", {"fieldname": construct.t(type.name) }).lower() }}" {%- endif -%} {%- endmacro %} {# A displayed value, with an empty one left blank rather than printed. `format_value` hands back the stored attribute itself, which is None for a null column and for a `Related` pointing at nothing — and Jinja renders that as the word "None", so an unfilled field reads as though somebody typed it. The test is against none rather than falsiness, because 0, an empty string and False are all values somebody meant. #} {# "Add " / "New " when the record name is known, else the generic "Add new item" / "New item". #} {% macro add_label(construct, recname) -%} {%- if recname %}{{ construct.t_rec("crud-add-rec", recname) }}{% else %}{{ construct.t("crud-add-new-item") }}{% endif -%} {%- endmacro %} {% macro new_label(construct, recname) -%} {%- if recname %}{{ construct.t_rec("crud-new-rec", recname) }}{% else %}{{ construct.t("crud-new-item") }}{% endif -%} {%- endmacro %} {# The record's tab bar: its own form first (labelled by view.details_tab_name, else the entity name, with the icon view.details_tab_icon, none when it is None), then its @tab_action tabs. The edit page and every tab page share it, so the labels stay put as the reader moves between tabs. #} {% macro record_tabs(construct, view, id, tab_actions, active_tab) -%} {%- endmacro %} {% macro shown(value) %}{%- if value is not none %}{{ value }}{% endif -%}{% endmacro %} {# File and image fields (schema.File / schema.Image; see files.py). form_enctype: the attribute a
needs when any field in it takes an upload — without multipart encoding the browser sends only the file name. file_display: a stored file as a link (an image as a thumbnail linking to the full image). The links are signed and expire (files.file_url), and carry up-follow="false": they answer with a file, not a page fragment. #} {% macro form_enctype(fields) -%} {%- for name, type in fields if type.field_type in ('file', 'image') and not type.readonly -%} {%- if loop.first %} enctype="multipart/form-data"{% endif -%} {%- endfor -%} {%- endmacro %} {% macro file_display(construct, stored, surface="list") -%} {%- if stored -%} {%- set url = stored.url -%} {%- if stored.is_image and url -%} {{ stored.name }} {%- else -%} {{ icon("attachment", size="sm") }} {%- endif -%} {%- if not (stored.is_image and surface == "list") -%} {%- set download = stored.download_url -%} {%- if download -%} {{ stored.name }} {%- else -%} {{ stored.name }} {%- endif -%} {%- endif -%} {%- endif -%} {%- endmacro %} {# The editor: the stored file (kept unless something else is chosen), a "Remove" box when the field is optional, and a plain file input to replace it. Works the same with JavaScript off. A file chosen before a re-render (validation error, conflict, resumed session) cannot be put back into the input, so the form says so rather than silently saving without it. #} {% macro file_editor(construct, field, type, data=None, errors=None, skip_label=False, disabled=False) %} {% set state = type.form_state(field, data) %} {% set help_id = field ~ '-help' if type.has_help() else none %} {% set error = errors.get(field) if errors else none %} {% set error_id = field ~ '-error' %} {% set limits_id = field ~ '-limits' %} {% set lost_id = field ~ '-lost' %} {%- set described = ([help_id] if help_id else []) + ([lost_id] if state.lost else []) + [limits_id] + ([error_id] if error else []) -%}
{% if not skip_label %}{{ construct.t(type.name)|ucfirst }}{% endif %} {% if not skip_label %}{{ field_help(construct, help_id, type) }}{% endif %} {% if state.stored %}
{{ construct.t("crud-file-current") }} {{ file_display(construct, state.stored, "form") }}
{% if not type.required %}
{% endif %} {% endif %} {% if state.lost %}

{{ icon("error", size="sm") }} {{ construct.t("crud-file-lost") }}

{% endif %} {{ type.limits_text(construct.t) }} {% if not skip_label %}{{ field_help(construct, help_id, type, at="below") }}{% endif %} {{ field_error(construct, error_id, error) }}
{% endmacro %} {% macro render_field(construct, field, type, view, data=None, ds=None, errors=None) %} {% if type.secret and data and type.readonly %} {# Masked, with no way to change it — and never the stored value, which is exactly what `secret` exists to keep out of the response. #}
{{ construct.t(type.name)|ucfirst }}
{{ icon("lock", size="sm") }} ••••••••
{% elif type.secret and data %}
{# Rendered once out here; the inputs below (skip_label) still point at it. #} {{ field_help(construct, field ~ '-help', type) }}
{{ icon("lock", size="sm") }} ••••••••
{# A JS state flag, not a control the user operates: the visible toggle button drives it. Hidden from assistive technology so it is not announced as a nameless checkbox. #}
{{ icon("lock", size="sm") }} ••••••••
{{ _render_field_internal(construct, field, type, view, data=None, ds=ds, skip_label=True) }}
{{ field_help(construct, field ~ '-help', type, at="below") }}
{% else %} {{ _render_field_internal(construct, field, type, view, data, ds, errors=errors) }} {% endif %} {% endmacro %} {# One row being added to a List field: the empty row the add button copies (index "__INDEX__", no item) or, on a refused form, a row as it was posted (`item`, its values; `errors`, the form's errors). #} {% macro list_new_item(construct, field, type, index, item=none, errors=none) %} {% if type.layout == 'form' %}

{{ new_label(construct, type.record_name) }}

{% if type.template %} {% include type.template %} {% else %}
{% set list_ds = type.list_class() %} {% for f, t in list_ds._schema.items() if t.field_type != 'pk' and t.visible_form %}
{% set input_name = field ~ "-new-" ~ index ~ "-" ~ f %} {% set sub_help_id = input_name ~ '-help' if t.has_help() else none %} {{ field_help(construct, sub_help_id, t) }} {{ field_error(construct, input_name ~ "-error", errors.get(input_name) if errors else none) }} {% if t.field_type == 'bool' %}
{% elif t.field_type == 'set' %}
{% for key, label_key in t.sets.items() %}
{% endfor %}
{% elif t.field_type == 'related' %} {% elif t.field_type == 'enum' %} {% elif t.form_input_type() == 'richtext' %} {% set editor_id = 'editor_' ~ input_name %}
{% else %} {% endif %} {{ field_help(construct, sub_help_id, t, at="below") }}
{% endfor %}
{% endif %}
{% else %}
{% set list_ds = type.list_class() %} {% for f, t in list_ds._schema.items() if t.field_type != 'pk' and t.visible_form %}
{% set input_name = field ~ "-new-" ~ index ~ "-" ~ f %} {% set sub_help_id = input_name ~ '-help' if t.has_help() else none %} {{ field_help(construct, sub_help_id, t) }} {{ field_error(construct, input_name ~ "-error", errors.get(input_name) if errors else none) }} {% if t.field_type == 'bool' %}
{% elif t.field_type == 'set' %}
{% for key, label_key in t.sets.items() %}
{% endfor %}
{% elif t.field_type == 'related' %} {% elif t.field_type == 'enum' %} {% elif t.form_input_type() == 'richtext' %} {% else %} {% endif %} {{ field_help(construct, sub_help_id, t, at="below") }}
{% endfor %}
{% endif %} {% endmacro %} {% macro _render_field_internal(construct, field, type, view, data=None, ds=None, skip_label=False, disabled=False, errors=None) %} {# `readonly` is checked before any of the editors below: a readonly field rendered as a checkbox or a chip picker looks operable, and the read-only form view is built entirely out of this branch. Lists are excluded — the list branch has its own readonly handling for rows and the add form. #} {% set help_id = field ~ '-help' if type.has_help() else none %} {% if type.readonly == True and type.field_type != 'list' %}
{# Not a
{% elif type.field_type in ('file', 'image') %} {{ file_editor(construct, field, type, data, errors, skip_label=skip_label, disabled=disabled) }} {% elif type.field_type == 'bool' %}
{# After the label here: a checkbox's label sits beside it, not above, so there is only one place for it. #} {{ field_help(construct, help_id, type, at=none) }}
{% elif type.field_type == 'set' %}
{% if not skip_label %}{{ construct.t(type.name)|ucfirst }}{% endif %} {% if not skip_label %}{{ field_help(construct, help_id, type) }}{% endif %} {% set values = data[field] if data and data[field] else [] %}
{% for key, label_key in type.sets.items() %}
{% endfor %}
{% if not skip_label %}{{ field_help(construct, help_id, type, at="below") }}{% endif %}
{% elif type.field_type == 'related' %}
{% if not skip_label %}{% endif %} {% if not skip_label %}{{ field_help(construct, help_id, type) }}{% endif %}
{% set value = type.form_value(data) if data else None %}

{% if not skip_label %}{{ field_help(construct, help_id, type, at="below") }}{% endif %}
{% elif type.field_type == 'manyrelated' %}
{# Labels the picker group; the search input inside points at it with aria-labelledby, since a
{% elif type.field_type == 'enum' and type.widget == 'radio' %} {# Every option in view: a native radio group in a fieldset, so it works and reads the same with JavaScript off. An optional field gets a "None" choice, since a radio cannot be unticked. #} {% set error = errors.get(field) if errors else None %} {% set error_id = field ~ '-error' %} {% set value = type.format_value(field, data) if data else None %} {% if value %}{% set current = value.v %}{% elif not data and type.default is not none %}{% set current = type.default.name if type.default.name is defined else type.default %}{% else %}{% set current = none %}{% endif %}
{% if not skip_label %}{{ construct.t(type.name)|ucfirst }}{% endif %} {% if not skip_label %}{{ field_help(construct, help_id, type) }}{% endif %}
{% if type.nullable %}
{% endif %} {% for enum_val in type.enum_class %}
{% endfor %}
{% if not skip_label %}{{ field_help(construct, help_id, type, at="below") }}{% endif %} {{ field_error(construct, error_id, error) }}
{% elif type.field_type == 'color' %} {# A colour input always holds a colour, so an optional field carries a "No colour" box; ticked, it wins over the picker. #} {% set error = errors.get(field) if errors else None %} {% set error_id = field ~ '-error' %} {% set value = type.format_value(field, data) if data else None %} {% set value = value if value is not none else (type.default if not data else none) %}
{% if not skip_label %}{% endif %} {% if not skip_label %}{{ field_help(construct, help_id, type) }}{% endif %}
{% if not type.required %} {% endif %}
{% if not skip_label %}{{ field_help(construct, help_id, type, at="below") }}{% endif %} {{ field_error(construct, error_id, error) }}
{% elif type.field_type == 'enum' %} {% set error = errors.get(field) if errors else None %} {% set error_id = field ~ '-error' %}
{% if not skip_label %}{% endif %} {% if not skip_label %}{{ field_help(construct, help_id, type) }}{% endif %} {% if not skip_label %}{{ field_help(construct, help_id, type, at="below") }}{% endif %} {{ field_error(construct, error_id, error) }}
{% elif type.field_type == 'list' %}
{% set posted = none %} {% if not skip_label %}{{ construct.t(type.name)|ucfirst }}{% endif %} {% if not skip_label %}{{ field_help(construct, help_id, type) }}{% endif %} {% if data and ds %} {% set posted = posted_list(data, field, ds.get_list(data, field), type.list_class._primary_key) %} {% set items = posted.items %} {% if items %} {% if type.layout == 'table' %}
{% if type.columns %} {% for col in type.columns %} {% set t = type.list_class()._schema[col] %} {% endfor %} {% else %} {% for f, t in type.list_class()._schema.items() if t.field_type != 'pk' and t.visible_form %} {% endfor %} {% endif %} {% if not type.readonly %} {% endif %} {% for item in items %} {% set list_ds = type.list_class() %} {% set list_pk_field = list_ds._primary_key %} {% set item_id = item[list_pk_field] %} {% endfor %} {% if not type.readonly %} {% endif %} {% endfor %}
{{ construct.t(t.name)|ucfirst }}{% if not (type.readonly or t.readonly) %}{{ field_help(construct, field ~ '-col-' ~ col ~ '-help', t, at=none) }}{% endif %}{{ construct.t(t.name)|ucfirst }}{% if not (type.readonly or t.readonly) %}{{ field_help(construct, field ~ '-col-' ~ f ~ '-help', t, at=none) }}{% endif %} {% if type.can_delete %}{{ construct.t("crud-action-title") }}{% endif %}
{% if type.readonly or t.readonly %} {% if t.has_display_filter() %} {{ shown(t.display_value(f, item, "inline")) }} {% elif t.field_type == 'bool' %} {% if item[f] %} {{ icon("check", cls="fcu-bool-icon fcu-bool-icon--true", label=construct.t("crud-bool-true")) }} {% else %} {{ icon("cancel", cls="fcu-bool-icon fcu-bool-icon--false", label=construct.t("crud-bool-false")) }} {% endif %} {% elif t.field_type == 'set' %} {% if item[f] %}
{% for v in item[f] %} {% if v in t.sets %}{{ construct.t(t.sets[v]) }}{% endif %} {% endfor %}
{% endif %} {% elif t.field_type == 'related' %} {% set curr = item %} {% for part in t.related_field.split('.') %} {% set curr = curr[part] %} {% endfor %} {{ shown(curr) }} {% elif t.field_type == 'enum' %} {{ shown(t.format_value(f, item)) }} {% elif t.orgmode %} {% set v = t.format_value(f, item) %}
{{ (v if v is not none else '') | orgmode }}
{% elif t.form_input_type() == 'richtext' %} {% set v = t.format_value(f, item) %}
{{ (v if v is not none else '') | markdown }}
{% else %} {{ shown(t.format_value(f, item)) }} {% endif %} {% else %} {% set input_name = field ~ "-" ~ item_id ~ "-" ~ f %} {% set sub_help_id = field ~ '-col-' ~ f ~ '-help' if t.has_help() else none %} {% if t.field_type == 'bool' %} {% elif t.field_type == 'set' %} {% set values = item[f] if item and item[f] else [] %}
{% for key, label_key in t.sets.items() %}
{% endfor %}
{% elif t.field_type == 'related' %} {% elif t.field_type == 'enum' %} {% elif t.form_input_type() == 'richtext' %} {% set editor_id = 'editor_' ~ input_name %} {% set value = item[f] if item[f] is not none else '' %}
{% else %} {% set list_err = t.validate_value(item[f], item) if item else None %} {% if list_err %} {{ construct.t(list_err.key, list_err.args) }} {% endif %} {% endif %} {% endif %}
{% if type.can_delete %} {% endif %}
{% elif type.layout == 'form' %}
{% for item in items %} {% set list_ds = type.list_class() %} {% set list_pk_field = list_ds._primary_key %} {% set item_id = item[list_pk_field] %}

{{ item }}

{% if not type.readonly %}
{% if type.can_delete %} {% endif %}
{% endif %}
{% if type.template %} {% include type.template %} {% else %}
{% set columns_to_render = type.columns if type.columns else list_ds._schema.keys() %} {% for f in columns_to_render if list_ds._schema[f].field_type != 'pk' and (type.columns or list_ds._schema[f].visible_form) %} {% set t = list_ds._schema[f] %} {% set input_name = field ~ "-" ~ item_id ~ "-" ~ f %} {% set sub_help_id = input_name ~ '-help' if t.has_help() else none %}
{# Readonly rows render text, not a control, so there is nothing for a
{% endfor %}
{% endif %}
{% endfor %}
{% endif %} {% else %}

{{ construct.t("crud-no-items-found") }}

{% endif %} {% endif %} {% if not type.readonly and type.can_add %}
{% if posted %}{% for new_item in posted.new %}{{ list_new_item(construct, field, type, loop.index0, new_item, errors) }}{% endfor %}{% endif %}
{% endif %} {% if not skip_label %}{{ field_help(construct, help_id, type, at="below") }}{% endif %}
{% elif type.form_input_type() == 'textarea' %}
{% if not skip_label %}{% endif %} {# Normalised before it reaches the control: a null rendered as "None" inside a textarea is a value the next save would store for real. #} {% set raw = type.format_value(field, data) if data else '' %} {% set value = raw if raw is not none else '' %} {% set column = ds.model[field] if ds else None %} {% set maxlength = column.type.length if column and column.type and column.type.length else '' %} {% set error = errors.get(field) if errors else (type.validate_value(value) if type.validate_value and data else None) %} {% set error_id = field ~ '-error' %} {% if not skip_label %}{{ field_help(construct, help_id, type) }}{% endif %} {% if not skip_label %}{{ field_help(construct, help_id, type, at="below") }}{% endif %} {{ field_error(construct, error_id, error) }}
{% elif type.form_input_type() == 'richtext' %} {% set editor_id = 'editor_' ~ field %} {% set raw = type.format_value(field, data) if data else '' %} {% set value = raw if raw is not none else '' %} {% set column = ds.model[field] if ds else None %} {% set maxlength = column.type.length if column and column.type and column.type.length else '' %}
{% set error = errors.get(field) if errors else (type.validate_value(value) if type.validate_value and data else None) %} {% set error_id = field ~ '-error' %} {# `for` on a
does nothing, so the ProseMirror host is named by aria-labelledby against a plain instead. The no-JS
{% if not skip_label %}{{ field_help(construct, help_id, type, at="below") }}{% endif %} {{ field_error(construct, error_id, error) }}
{% else %}
{% if not skip_label %}{% endif %} {% set value = type.format_value(field,data) if data else None %} {% set maxlength = type.length if type.length else '' %} {% set error = errors.get(field) if errors else (type.validate_value(value) if type.validate_value and data else None) %} {% set error_id = field ~ '-error' %} {% if not skip_label %}{{ field_help(construct, help_id, type) }}{% endif %} {# A unit, currency or time zone beside the control, read with it. #} {% set suffix = type.input_suffix(data) %} {% set suffix_id = field ~ '-suffix' %} {% set described = ((help_id ~ ' ') if help_id else '') ~ (suffix_id if suffix else '') %} {% if suffix %}
{% endif %} {% if suffix %}{{ suffix }}
{% endif %} {% if not skip_label %}{{ field_help(construct, help_id, type, at="below") }}{% endif %} {{ field_error(construct, error_id, error) }}
{% endif %} {% endmacro %} {% macro render_newrel_subfield(construct, input_name, t, value=None, error=None) %} {% set sub_help_id = input_name ~ '-help' if t.has_help() else none %} {# One inline-create panel input for a ManyRelated create="form" field. Supports the types in ManyRelated.INLINE_FORM_TYPES; text renders as a plain textarea (no ProseMirror inside cloned