From 98dcfc5d20cc2982cf9e6c61cae81f91f3740fc9 Mon Sep 17 00:00:00 2001 From: Konrad Mohrfeldt <konrad.mohrfeldt@farbdev.org> Date: Tue, 22 Mar 2022 13:48:38 +0100 Subject: [PATCH] fix: raise ValidationError on invalid data This gives the calling code better control over the handling of such errors and makes it easy to differentiate between errors and valid return types. --- program/models.py | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/program/models.py b/program/models.py index 6a4762df..2d8fc1f9 100644 --- a/program/models.py +++ b/program/models.py @@ -22,6 +22,7 @@ from datetime import datetime, time, timedelta from dateutil.relativedelta import relativedelta from dateutil.rrule import rrule +from rest_framework.exceptions import ValidationError from versatileimagefield.fields import PPOIField, VersatileImageField from django.contrib.auth.models import User @@ -673,17 +674,26 @@ class Schedule(models.Model): conflicts = Schedule.make_conflicts(sdl, schedule_pk, show_pk) if schedule.rrule.freq > 0 and schedule.first_date == schedule.last_date: - return {"detail": _("Start and until dates mustn't be the same")} + raise ValidationError( + _("Start and until dates mustn't be the same"), + code="no-same-day-start-and-end", + ) if schedule.last_date < schedule.first_date: - return {"detail": _("Until date mustn't be before start")} + raise ValidationError( + _("Until date mustn't be before start"), + code="no-start-after-end", + ) num_conflicts = len( [pr for pr in conflicts["projected"] if len(pr["collisions"]) > 0] ) if len(solutions) != num_conflicts: - return {"detail": _("Numbers of conflicts and solutions don't match.")} + raise ValidationError( + _("Numbers of conflicts and solutions don't match."), + code="one-solution-per-conflict", + ) # Projected timeslots to create create = [] -- GitLab