.
This commit is contained in:
+32
-24
@@ -113,35 +113,43 @@ def rota_detail(request, rota_id):
|
||||
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 = {}
|
||||
# Prefer reading metadata from the typed RotaConstraintOptions class
|
||||
RotaConstraintOptions = getattr(mod, "RotaConstraintOptions", None)
|
||||
meta = {}
|
||||
defaults = {}
|
||||
if RotaConstraintOptions is not None:
|
||||
# class-level model_fields avoids instantiating RotaBuilder
|
||||
try:
|
||||
meta = opts_model.__class__.model_fields
|
||||
meta = getattr(RotaConstraintOptions, "model_fields", {}) or {}
|
||||
except Exception:
|
||||
meta = {}
|
||||
# iterate defaults and build rich info dict
|
||||
for k, v in defaults.items():
|
||||
desc = None
|
||||
if isinstance(meta, dict) and k in meta:
|
||||
info = meta[k]
|
||||
if hasattr(info, "description") and 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:
|
||||
# instantiate a model to obtain defaults
|
||||
defaults = RotaConstraintOptions().model_dump()
|
||||
except Exception:
|
||||
defaults = {}
|
||||
else:
|
||||
# Fallback: try to use RotaBuilder's constraint_options_model if present
|
||||
RotaBuilder = getattr(mod, "RotaBuilder", None)
|
||||
if RotaBuilder is not None:
|
||||
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:
|
||||
current = v
|
||||
available_constraints_rich[k] = {"default": v, "description": desc, "current": current}
|
||||
defaults = {}
|
||||
# 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:
|
||||
available_constraints_rich = {k: {"default": v} for k, v in available_constraints.items()}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user