Commit 3421123d authored by Thom Wiggers's avatar Thom Wiggers 📐
Browse files

Merge branch 'fix/pizzas-validate-unique' into 'master'

Fix pizzas uniqueness

Closes #668

See merge request thalia/concrexit!872
parents 76a9a86a 906c74a0
......@@ -50,21 +50,26 @@ class PizzaEvent(models.Model):
except PizzaEvent.DoesNotExist:
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):
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.'),
})
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):
return 'Pizzas for ' + str(self.event)
......
......@@ -99,8 +99,14 @@ class PizzaEventTestCase(TestCase):
with self.subTest(msg="two events, within 8 hours, first ended"):
self.assertEqual(second_pizzaevent, PizzaEvent.current())
def test_clean(self):
"""Check if clean method works"""
def test_validate_unique(self):
"""Check if uniqueness validation is correct"""
self.pizzaEvent.start = self.pizzaEvent.start + datetime.timedelta(
minutes=10)
with self.subTest(msg="saving works"):
self.pizzaEvent.validate_unique()
new = PizzaEvent(
event=self.event2,
start=self.pizzaEvent.start + datetime.timedelta(minutes=10),
......@@ -108,9 +114,15 @@ class PizzaEventTestCase(TestCase):
)
with self.subTest(msg="overlapping event"):
with self.assertRaises(ValidationError):
new.clean()
new.validate_unique()
new.end = timezone.now() - datetime.timedelta(hours=100)
def test_clean(self):
"""Check if clean method works"""
new = PizzaEvent(
event=self.event2,
start=self.pizzaEvent.start + datetime.timedelta(minutes=10),
end=timezone.now() - datetime.timedelta(hours=100),
)
with self.subTest(msg="end before start"):
with self.assertRaises(ValidationError):
new.clean()
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