.
This commit is contained in:
@@ -334,9 +334,32 @@ class ShiftForm(forms.Form):
|
||||
help_text="Maximum allowed difference in allocated shifts for this shift (leave blank for default)",
|
||||
)
|
||||
|
||||
# Allow entering shift-specific constraints as JSON. This should be a
|
||||
# list of constraint dicts compatible with the scheduler's SingleShift
|
||||
# `constraints` field. Example: `[{"name": "night", "options": {...}}]`
|
||||
constraints = forms.CharField(
|
||||
required=False,
|
||||
widget=forms.Textarea(attrs={"rows": 4, "class": "textarea"}),
|
||||
help_text="Optional JSON list of constraint objects for this shift (e.g. [{'name':'night','options':{}}])",
|
||||
)
|
||||
|
||||
def clean_sites(self):
|
||||
val = self.cleaned_data["sites"]
|
||||
sites = [s.strip() for s in val.split(",") if s.strip()]
|
||||
if not sites:
|
||||
raise forms.ValidationError("At least one site is required")
|
||||
return sites
|
||||
|
||||
def clean_constraints(self):
|
||||
val = self.cleaned_data.get("constraints")
|
||||
if not val:
|
||||
return []
|
||||
try:
|
||||
parsed = json.loads(val)
|
||||
if not isinstance(parsed, list):
|
||||
raise forms.ValidationError("Constraints must be a JSON list of constraint objects")
|
||||
return parsed
|
||||
except forms.ValidationError:
|
||||
raise
|
||||
except Exception as exc:
|
||||
raise forms.ValidationError(f"Invalid JSON for constraints: {exc}")
|
||||
|
||||
Reference in New Issue
Block a user