This commit is contained in:
Ross
2025-12-09 21:12:05 +00:00
parent 3906ab3c55
commit c94c204d51
2 changed files with 33 additions and 3 deletions
+30 -2
View File
@@ -57,10 +57,38 @@ class RotaSchedule(models.Model):
delta = self.end_date - self.start_date
weeks_to_rota = max(1, int(delta.days // 7))
# Allow passing options directly if they match RotaBuilder kwargs
rb_kwargs = dict(self.options or {})
# Build kwargs for RotaBuilder from any explicit builder args stored in
# `self.options` (kept for backward compatibility), and treat the
# remaining keys as constraint options which should map to
# `RotaConstraintOptions`.
opts = dict(self.options or {})
builder_keys = [
"balance_offset_modifier",
"ltft_balance_offset",
"use_previous_shifts",
"use_shift_balance_extra",
"use_bank_holiday_extra",
"allow_force_assignment_with_leave_conflict",
]
rb_kwargs = {k: opts.pop(k) for k in builder_keys if k in opts}
rb = RotaBuilder(start_date=self.start_date, weeks_to_rota=weeks_to_rota, name=self.name, **rb_kwargs)
# If the rota package exposes a typed RotaConstraintOptions model, use it
# to validate/coerce the remaining `opts` and attach it to the builder.
try:
RotaConstraintOptions = getattr(mod, "RotaConstraintOptions")
if opts:
# validate raw JSON into the typed model
opts_model = RotaConstraintOptions.model_validate(opts)
else:
opts_model = RotaConstraintOptions()
# attach the typed model to the builder (overrides defaults)
setattr(rb, "constraint_options_model", opts_model)
except Exception:
# If the model isn't available or validation fails, keep builder
# defaults and proceed (backwards compatible behaviour).
pass
# Add shifts (expecting list of dicts compatible with SingleShift)
shifts_objs = []
for s in self.shifts or []: