Verified Commit 906c74a0 authored by Sébastiaan Versteeg's avatar Sébastiaan Versteeg
Browse files

Move uniqueness check to validate_unique and adjust test

parent 7ca74821
......@@ -50,15 +50,13 @@ class PizzaEvent(models.Model):
except PizzaEvent.DoesNotExist:
return None
def clean(self):
if self.start >= self.end:
raise ValidationError({
'start': _('The start is after the end of this event.'),
'end': _('The end is before the start of this event.'),
})
def validate_unique(self, exclude=None):
super().validate_unique(exclude)
for other in PizzaEvent.objects.filter(
Q(end__gte=self.start, end__lte=self.end) |
Q(start=self.start, start__lte=self.start)):
if other.pk == self.pk:
continue
raise ValidationError({
'start': _(
'This event cannot overlap with {}.').format(other),
......@@ -66,6 +64,13 @@ class PizzaEvent(models.Model):
'This event cannot overlap with {}.').format(other),
})
def clean(self):
if self.start >= self.end:
raise ValidationError({
'start': _('The start is after the end of this event.'),
'end': _('The end is before the start of this event.'),
})
def __str__(self):
return 'Pizzas for ' + str(self.event)
......
......@@ -105,7 +105,7 @@ class PizzaEventTestCase(TestCase):
minutes=10)
with self.subTest(msg="saving works"):
self.pizzaEvent.clean()
self.pizzaEvent.validate_unique()
new = PizzaEvent(
event=self.event2,
......@@ -114,7 +114,7 @@ class PizzaEventTestCase(TestCase):
)
with self.subTest(msg="overlapping event"):
with self.assertRaises(ValidationError):
new.clean()
new.validate_unique()
def test_clean(self):
"""Check if clean method works"""
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment