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,21 +50,26 @@ class PizzaEvent(models.Model): ...@@ -50,21 +50,26 @@ class PizzaEvent(models.Model):
except PizzaEvent.DoesNotExist: except PizzaEvent.DoesNotExist:
return None return None
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),
'end': _(
'This event cannot overlap with {}.').format(other),
})
def clean(self): def clean(self):
if self.start >= self.end: if self.start >= self.end:
raise ValidationError({ raise ValidationError({
'start': _('The start is after the end of this event.'), 'start': _('The start is after the end of this event.'),
'end': _('The end is before the start of this event.'), 'end': _('The end is before the start of this event.'),
}) })
for other in PizzaEvent.objects.filter(
Q(end__gte=self.start, end__lte=self.end) |
Q(start=self.start, start__lte=self.start)):
raise ValidationError({
'start': _(
'This event cannot overlap with {}.').format(other),
'end': _(
'This event cannot overlap with {}.').format(other),
})
def __str__(self): def __str__(self):
return 'Pizzas for ' + str(self.event) return 'Pizzas for ' + str(self.event)
......
...@@ -105,7 +105,7 @@ class PizzaEventTestCase(TestCase): ...@@ -105,7 +105,7 @@ class PizzaEventTestCase(TestCase):
minutes=10) minutes=10)
with self.subTest(msg="saving works"): with self.subTest(msg="saving works"):
self.pizzaEvent.clean() self.pizzaEvent.validate_unique()
new = PizzaEvent( new = PizzaEvent(
event=self.event2, event=self.event2,
...@@ -114,7 +114,7 @@ class PizzaEventTestCase(TestCase): ...@@ -114,7 +114,7 @@ class PizzaEventTestCase(TestCase):
) )
with self.subTest(msg="overlapping event"): with self.subTest(msg="overlapping event"):
with self.assertRaises(ValidationError): with self.assertRaises(ValidationError):
new.clean() new.validate_unique()
def test_clean(self): def test_clean(self):
"""Check if clean method works""" """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