diff --git a/crudkit/ui/fragments.py b/crudkit/ui/fragments.py index 08903de..ef1b260 100644 --- a/crudkit/ui/fragments.py +++ b/crudkit/ui/fragments.py @@ -639,9 +639,14 @@ def _normalize_field_spec(spec, mapper, session, label_specs_model_default): "template": spec.get("template"), "template_name": spec.get("template_name"), "template_ctx": spec.get("template_ctx"), - "label_spec": spec.get("label_spec") + "label_spec": spec.get("label_spec"), } + if "link" in spec: + field["link"] = spec["link"] + if "label_deps" in spec: + field["label_deps"] = spec["label_deps"] + if rel_prop: if field["type"] is None: field["type"] = "select" @@ -970,6 +975,7 @@ def _format_label_from_values(spec: Any, values: dict) -> Optional[str]: return "" if v is None else str(v) def _build_href(spec: Dict[str, Any], row: Dict[str, Any], obj) -> Optional[str]: + print(spec) if not spec: return None params = {} @@ -982,6 +988,7 @@ def _build_href(spec: Dict[str, Any], row: Dict[str, Any], obj) -> Optional[str] params[k] = val else: params[k] = v + print(params) if any(v is None for v in params.values()): return None try: @@ -1061,6 +1068,7 @@ def render_field(field, value): label_attrs=_sanitize_attrs(field.get('label_attrs') or {}), help=field.get('help'), value_label=field.get('value_label'), + link_href=field.get("link_href"), ) @@ -1231,6 +1239,15 @@ def render_form( if vl2 is not None: f["value_label"] = vl2 + link_spec = f.get("link") + if link_spec: + try: + href = _build_href(link_spec, values_map, instance) + except Exception: + href = None + if href: + f["link_href"] = href + # Build rows (supports nested layout with parents) rows_map = _normalize_rows_layout(layout) rows_tree = _assign_fields_to_rows(fields, rows_map) diff --git a/crudkit/ui/templates/field.html b/crudkit/ui/templates/field.html index 0efe44b..aecb234 100644 --- a/crudkit/ui/templates/field.html +++ b/crudkit/ui/templates/field.html @@ -4,7 +4,13 @@ {% if label_attrs %}{% for k,v in label_attrs.items() %} {{k}}{% if v is not sameas true %}="{{ v }}"{% endif %} {% endfor %}{% endif %}> - {{ field_label }} + {% if link_href %} + + {% endif %} + {{ field_label }} + {% if link_href %} + + {% endif %} {% endif %} diff --git a/inventory/routes/entry.py b/inventory/routes/entry.py index 4ab9d10..53350f4 100644 --- a/inventory/routes/entry.py +++ b/inventory/routes/entry.py @@ -23,6 +23,7 @@ def _fields_for_model(model: str): "model", "condition", "notes", + "owner.id", ] fields_spec = [ {"name": "label", "type": "display", "label": "", "row": "label", @@ -43,7 +44,7 @@ def _fields_for_model(model: str): "attrs": {"class": "form-control"}, "label": "Device Type", "label_attrs": {"class": "form-label"}}, {"name": "owner", "row": "status", "label": "Contact", "wrap": {"class": "col"}, "attrs": {"class": "form-control"}, "label_attrs": {"class": "form-label"}, - "label_spec": "{label}"}, + "label_spec": "{label}", "link": {"endpoint": "entry.entry", "params": {"model": "user", "id": "{owner.id}"}}}, {"name": "location", "row": "status", "label": "Location", "wrap": {"class": "col"}, "attrs": {"class": "form-control"}, "label_attrs": {"class": "form-label"}, "label_spec": "{name} - {room_function.description}"}, @@ -84,7 +85,8 @@ def _fields_for_model(model: str): "last_name", "title", "active", - "staff" + "staff", + "supervisor.id" ] fields_spec = [ {"name": "label", "row": "label", "label": "", "type": "display", @@ -100,7 +102,7 @@ def _fields_for_model(model: str): "row": "name", "wrap": {"class": "col-3"}}, {"name": "supervisor", "label": "Supervisor", "label_attrs": {"class": "form-label"}, "label_spec": "{label}", "row": "details", "wrap": {"class": "col-3"}, - "attrs": {"class": "form-control"}}, + "attrs": {"class": "form-control"}, "link": {"endpoint": "entry.entry", "params": {"id": "{supervisor.id}", "model": "user"}}}, {"name": "location", "label": "Room", "label_attrs": {"class": "form-label"}, "label_spec": "{name} - {room_function.description}", "row": "details", "wrap": {"class": "col-3"}, "attrs": {"class": "form-control"}}, @@ -137,9 +139,11 @@ def _fields_for_model(model: str): {"name": "buttons", "label": "", "row": "label", "type": "template", "template": "entry_buttons.html", "wrap": {"class": "col-auto text-end me-2"}, "attrs": {"data-model": model}}, {"name": "contact", "row": "ownership", "wrap": {"class": "col"}, "label": "Contact", - "label_spec": "{label}", "attrs": {"class": "form-control"}, "label_attrs": {"class": "form-label"}}, + "label_spec": "{label}", "attrs": {"class": "form-control"}, "label_attrs": {"class": "form-label"}, + "link": {"endpoint": "entry.entry", "params": {"id": "{contact.id}", "model": "user"}}}, {"name": "work_item", "row": "ownership", "wrap": {"class": "col"}, "label": "Work Item", - "label_spec": "{label}", "attrs": {"class": "form-control"}, "label_attrs": {"class": "form-label"}}, + "label_spec": "{label}", "attrs": {"class": "form-control"}, "label_attrs": {"class": "form-label"}, + "link": {"endpoint": "entry.entry", "params": {"id": "{work_item.id}", "model": "inventory"}}}, {"name": "start_time", "type": "datetime", "attrs": {"class": "form-control"}, "row": "timestamps", "wrap": {"class": "col"}, "label_attrs": {"class": "form-label"}, "label": "Start"}, {"name": "end_time", "type": "datetime", "attrs": {"class": "form-control"}, "row": "timestamps", diff --git a/inventory/templates/crudkit/field.html b/inventory/templates/crudkit/field.html new file mode 100644 index 0000000..8e54343 --- /dev/null +++ b/inventory/templates/crudkit/field.html @@ -0,0 +1,84 @@ +{# show label unless hidden/custom #} + +{% if field_type != 'hidden' and field_label %} + +{% endif %} + +{% if field_type == 'select' %} + + +{% elif field_type == 'textarea' %} + + +{% elif field_type == 'checkbox' %} + + +{% elif field_type == 'hidden' %} + + +{% elif field_type == 'display' %} +