.
This commit is contained in:
+32
-24
@@ -113,35 +113,43 @@ def rota_detail(request, rota_id):
|
|||||||
available_constraints_rich = {}
|
available_constraints_rich = {}
|
||||||
try:
|
try:
|
||||||
mod = importlib.import_module("rota_generator.shifts")
|
mod = importlib.import_module("rota_generator.shifts")
|
||||||
RotaBuilder = getattr(mod, "RotaBuilder")
|
# Prefer reading metadata from the typed RotaConstraintOptions class
|
||||||
rb = RotaBuilder()
|
RotaConstraintOptions = getattr(mod, "RotaConstraintOptions", None)
|
||||||
opts_model = getattr(rb, "constraint_options_model", None)
|
meta = {}
|
||||||
if opts_model is not None:
|
defaults = {}
|
||||||
defaults = opts_model.model_dump()
|
if RotaConstraintOptions is not None:
|
||||||
# attempt to get field descriptions from pydantic metadata
|
# class-level model_fields avoids instantiating RotaBuilder
|
||||||
meta = {}
|
|
||||||
try:
|
try:
|
||||||
meta = opts_model.__class__.model_fields
|
meta = getattr(RotaConstraintOptions, "model_fields", {}) or {}
|
||||||
except Exception:
|
except Exception:
|
||||||
meta = {}
|
meta = {}
|
||||||
# iterate defaults and build rich info dict
|
try:
|
||||||
for k, v in defaults.items():
|
# instantiate a model to obtain defaults
|
||||||
desc = None
|
defaults = RotaConstraintOptions().model_dump()
|
||||||
if isinstance(meta, dict) and k in meta:
|
except Exception:
|
||||||
info = meta[k]
|
defaults = {}
|
||||||
if hasattr(info, "description") and info.description:
|
else:
|
||||||
desc = info.description
|
# Fallback: try to use RotaBuilder's constraint_options_model if present
|
||||||
else:
|
RotaBuilder = getattr(mod, "RotaBuilder", None)
|
||||||
try:
|
if RotaBuilder is not None:
|
||||||
desc = info.get("description")
|
|
||||||
except Exception:
|
|
||||||
desc = None
|
|
||||||
# include the current value saved on the rota (if any)
|
|
||||||
try:
|
try:
|
||||||
current = (rota.options or {}).get(k, v)
|
rb = RotaBuilder()
|
||||||
|
opts_model = getattr(rb, "constraint_options_model", None)
|
||||||
|
if opts_model is not None:
|
||||||
|
defaults = opts_model.model_dump()
|
||||||
|
meta = getattr(opts_model.__class__, "model_fields", {}) or {}
|
||||||
except Exception:
|
except Exception:
|
||||||
current = v
|
defaults = {}
|
||||||
available_constraints_rich[k] = {"default": v, "description": desc, "current": current}
|
# iterate defaults and build rich info dict
|
||||||
|
for k, v in defaults.items():
|
||||||
|
# Use unified helper to extract a stable string description for the key
|
||||||
|
desc = _get_constraint_description(k)
|
||||||
|
# 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:
|
except Exception:
|
||||||
available_constraints_rich = {k: {"default": v} for k, v in available_constraints.items()}
|
available_constraints_rich = {k: {"default": v} for k, v in available_constraints.items()}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user