From 1565241de664356b981699f4a973881970ca7ead Mon Sep 17 00:00:00 2001 From: Ross Date: Tue, 9 Dec 2025 22:18:46 +0000 Subject: [PATCH] . --- djangorota/rota/views.py | 56 +++++++++++++++++++++++----------------- 1 file changed, 32 insertions(+), 24 deletions(-) diff --git a/djangorota/rota/views.py b/djangorota/rota/views.py index d4869de..7e89360 100644 --- a/djangorota/rota/views.py +++ b/djangorota/rota/views.py @@ -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()}