.
This commit is contained in:
@@ -52,11 +52,46 @@ def rota_detail(request, rota_id):
|
||||
except Exception:
|
||||
opt_json = "{}"
|
||||
|
||||
# Build a richer representation of available constraints (default, type, description)
|
||||
available_constraints_rich = {}
|
||||
try:
|
||||
mod = importlib.import_module("rota_generator.shifts")
|
||||
RotaBuilder = getattr(mod, "RotaBuilder")
|
||||
rb = RotaBuilder()
|
||||
opts_model = getattr(rb, "constraint_options_model", None)
|
||||
if opts_model is not None:
|
||||
defaults = opts_model.model_dump()
|
||||
# attempt to get field descriptions from pydantic metadata
|
||||
meta = {}
|
||||
try:
|
||||
meta = opts_model.__class__.model_fields
|
||||
except Exception:
|
||||
meta = {}
|
||||
for k, v in defaults.items():
|
||||
desc = None
|
||||
if isinstance(meta, dict) and k in meta:
|
||||
info = meta[k]
|
||||
if hasattr(info, "description"):
|
||||
desc = info.description
|
||||
else:
|
||||
try:
|
||||
desc = info.get("description")
|
||||
except Exception:
|
||||
desc = None
|
||||
# include the current value saved on the rota (if any)
|
||||
try:
|
||||
current = (rota.options or {}).get(k, v)
|
||||
except Exception:
|
||||
current = v
|
||||
available_constraints_rich[k] = {"default": v, "description": desc, "current": current}
|
||||
except Exception:
|
||||
available_constraints_rich = {k: {"default": v} for k, v in available_constraints.items()}
|
||||
|
||||
options_form = RotaOptionsForm(initial={"options_json": opt_json})
|
||||
return render(
|
||||
request,
|
||||
"rota/rota_detail.html",
|
||||
{"rota": rota, "options_form": options_form, "available_constraints": available_constraints},
|
||||
{"rota": rota, "options_form": options_form, "available_constraints": available_constraints_rich},
|
||||
)
|
||||
|
||||
|
||||
@@ -384,10 +419,45 @@ def rota_option_add(request, rota_id):
|
||||
|
||||
return redirect("rota:rota_detail", rota_id=rota.id)
|
||||
|
||||
# GET: show modal partial
|
||||
# GET: show modal partial. If a `key` query param is supplied, try to
|
||||
# prefill the value and type from the builder defaults to make adding
|
||||
# standard options easy.
|
||||
key_q = request.GET.get("key")
|
||||
initial = {"key": "", "value": "", "type": "string"}
|
||||
if key_q:
|
||||
try:
|
||||
import importlib
|
||||
mod = importlib.import_module("rota_generator.shifts")
|
||||
RotaBuilder = getattr(mod, "RotaBuilder")
|
||||
rb = RotaBuilder()
|
||||
defaults = rb.constraint_options_model.model_dump()
|
||||
if key_q in defaults:
|
||||
d = defaults[key_q]
|
||||
initial["key"] = key_q
|
||||
if isinstance(d, bool):
|
||||
initial["type"] = "bool"
|
||||
initial["value"] = "1" if d else "0"
|
||||
elif isinstance(d, int):
|
||||
initial["type"] = "int"
|
||||
initial["value"] = str(d)
|
||||
elif isinstance(d, float):
|
||||
initial["type"] = "float"
|
||||
initial["value"] = str(d)
|
||||
elif isinstance(d, (list, dict)):
|
||||
initial["type"] = "json"
|
||||
try:
|
||||
initial["value"] = json.dumps(d)
|
||||
except Exception:
|
||||
initial["value"] = ""
|
||||
else:
|
||||
initial["type"] = "string"
|
||||
initial["value"] = str(d)
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
modal_html = render_to_string(
|
||||
"rota/partials/rota_option_modal_single.html",
|
||||
{"form_action": reverse("rota:rota_option_add", args=(rota.id,)), "initial": {"key": "", "value": "", "type": "string"}, "rota": rota},
|
||||
{"form_action": reverse("rota:rota_option_add", args=(rota.id,)), "initial": initial, "rota": rota},
|
||||
request=request,
|
||||
)
|
||||
return HttpResponse(modal_html)
|
||||
|
||||
Reference in New Issue
Block a user