data_definition_options() used to exclude relation-type fields from its field list entirely, so a filter/binding could never reference "the linked object" of a row — and even when a relation field's clean name was typed manually (as the earlier "level.parcour" example needed), it silently matched nothing: every column lookup for a filter/repeater field used the field's own name, but a relation is actually stored in a "<field>_id" column (see create_definition.py), so the lookup always missed.
Relation fields now appear in the champ dropdowns (Répéteur's filtre_champ/filtre2_champ, Donnée liée's data_filtre_champ/data_filtre2_champ) labeled with the object they point to (e.g. "parcour (→ parcours)"), and a new _field_column() helper in filter_repeater_rows.py resolves the right "<field>_id" column whenever the field turns out to be a relation — used consistently by the filter comparison itself, the "{{Objet.champ}}" dynamic-value resolver, the repeater's row content ({{champ}}), and the Donnée liée row context. Jauge's own champ/champ_nom pickers (which need an actual displayable value, not an id) still exclude relations, both server- and client-side.
Verified end to end: the dropdown shows the relation field with its target-object label, and filtering "level" rows by the clean relation field name "parcour" (not "parcour_id") against a dynamic {{game.current_parcours}} reference now actually matches, alongside the existing "number" filter. Full suite green (89).
72 lines
3.9 KiB
Python
72 lines
3.9 KiB
Python
import html as html_lib
|
|
|
|
import db
|
|
|
|
from .style_string import _style_string
|
|
from .auto_id import _auto_id
|
|
from .apply_ctx import _apply_ctx
|
|
from .filter_repeater_rows import filter_repeater_rows, _field_column
|
|
from ..element_types.load_template_tree import _load_template_tree
|
|
|
|
|
|
def _render_repeater(el, meta, slug, children_map, ctx=None, parent_flex_direction=None):
|
|
from .render_element_html import render_element_html
|
|
|
|
style = _style_string(el, " overflow:auto;", parent_flex_direction, widget_key=el.get("widget"))
|
|
attrs_raw = el.get("attributes") or {}
|
|
definition_id = attrs_raw.get("_definition_id")
|
|
# data-definition-id sur le Répéteur lui-même : lu côté client (play.html)
|
|
# pour savoir, au clic sur une de ses lignes, à quel objet de données
|
|
# appartient la ligne cliquée (voir déclencheur "clic sur une ligne de
|
|
# Répéteur" et l'action "Ouvrir la ligne cliquée").
|
|
definition_attr = f' data-definition-id="{html_lib.escape(str(definition_id))}"' if definition_id else ""
|
|
template_type_id = attrs_raw.get("_template_type_id")
|
|
template = _apply_ctx(el.get("content") or "", ctx)
|
|
children = (children_map or {}).get(el["id"], [])
|
|
if not definition_id or not slug:
|
|
return f'<div id="{_auto_id(el)}" data-element-id="{el["id"]}" style="{html_lib.escape(style)}"><em>Répéteur : choisis un objet de données dans les propriétés.</em></div>'
|
|
definition = db.get_definition(slug, int(definition_id))
|
|
if not definition:
|
|
return f'<div id="{_auto_id(el)}" data-element-id="{el["id"]}" style="{html_lib.escape(style)}"><em>Objet introuvable — a-t-il été supprimé ?</em></div>'
|
|
# (definition_attr est ajouté au conteneur du Répéteur plus bas, une fois
|
|
# qu'on sait que l'objet est valide.)
|
|
rows = db.list_rows(slug, definition)
|
|
rows = filter_repeater_rows(slug, definition, rows, attrs_raw)
|
|
field_cols = {f["name"]: _field_column(definition, f["name"]) for f in definition["fields"]}
|
|
|
|
# Modèle de ligne = un élément de jeu (conteneur stylisé réutilisable) :
|
|
# on recharge son arbre EN DIRECT à chaque ligne de données (jamais copié
|
|
# en base), et on l'affiche avec les {{champ}} de cette ligne — voir
|
|
# _load_template_tree pour le détail de cette astuce de rendu.
|
|
template_tree = None
|
|
if template_type_id:
|
|
try:
|
|
template_tree = _load_template_tree(slug, int(template_type_id))
|
|
except (TypeError, ValueError):
|
|
template_tree = None
|
|
|
|
items = []
|
|
for row in rows:
|
|
row_ctx = dict(ctx or {})
|
|
row_ctx.update({fname: str(row.get(col, "") or "") for fname, col in field_cols.items()})
|
|
if template_tree:
|
|
inner = "".join(
|
|
render_element_html(c, slug, template_tree["children_map"], row_ctx)
|
|
for c in template_tree["top"]
|
|
)
|
|
elif children:
|
|
inner = "".join(render_element_html(c, slug, children_map, row_ctx) for c in children)
|
|
else:
|
|
line = template
|
|
for fname, val in row_ctx.items():
|
|
line = line.replace("{{" + fname + "}}", val)
|
|
inner = html_lib.escape(line)
|
|
# data-row-id : identifiant STABLE de la vraie ligne de données
|
|
# affichée (par opposition à data-element-id, qui vaut l'id du
|
|
# modèle de ligne et se répète à l'identique sur chaque ligne) —
|
|
# c'est ce qui permet au clic de savoir "quel mail" a été ouvert.
|
|
items.append(f'<div class="repeaterItem" data-row-id="{row["id"]}">{inner}</div>')
|
|
empty_msg = "Aucune ligne ne correspond au filtre réglé." if attrs_raw.get("_filtre_champ") else "Aucune donnée dans cet objet pour l'instant."
|
|
inner_all = "".join(items) if items else f"<em>{empty_msg}</em>"
|
|
return f'<div id="{_auto_id(el)}" data-element-id="{el["id"]}"{definition_attr} style="{html_lib.escape(style)}">{inner_all}</div>'
|