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

Change uniqueness of boards to no overlap in period

parent 3164fc5f
...@@ -148,19 +148,20 @@ class Board(Committee): ...@@ -148,19 +148,20 @@ class Board(Committee):
def validate_unique(self, *args, **kwargs): def validate_unique(self, *args, **kwargs):
""" Check uniqueness""" """ Check uniqueness"""
super().validate_unique(*args, **kwargs) super().validate_unique(*args, **kwargs)
if self.since is not None and self.until is not None: boards = Board.objects.all()
for board in Board.objects.filter(since__year=self.since.year, if self.since is not None:
until__year=self.until.year): for board in boards:
if board != self: if board.pk == self.pk:
continue
if ((board.until is None and (
self.until is None or self.until >= board.since)) or
(self.until is None and self.since <= board.until) or
(self.until and board.until and
self.since <= board.until and
self.until >= board.since)):
raise ValidationError({ raise ValidationError({
'since': _('A board already exists for those years'), 'since': _('A board already exists for those years'),
'until': _('A board already exists for those years')}) 'until': _('A board already exists for those years')})
elif self.since is not None:
for board in Board.objects.filter(since__lte=self.since,
until__gte=self.since):
if board != self:
raise ValidationError({
'since': _('A board already exists for those years')})
class ActiveMembershipManager(models.Manager): class ActiveMembershipManager(models.Manager):
......
...@@ -168,27 +168,52 @@ class BoardTest(TestCase): ...@@ -168,27 +168,52 @@ class BoardTest(TestCase):
self.testboard.since = None self.testboard.since = None
self.testboard.validate_unique() self.testboard.validate_unique()
def test_unique_periods(self): def test_create_unique_period1(self):
Board.objects.create( """ Check uniqueness with since before period of testboard """
name_nl="testbe1", b = Board(
name_en="testbo1", name_nl="testbe",
name_en="testbo",
contact_email="test@test.com",
description_nl="descnl", description_nl="descnl",
description_en="descen", description_en="descen",
since=timezone.now().date() since=timezone.now().date()
.replace(year=1990, month=9, day=1), .replace(year=1990, month=2, day=1),
until=timezone.now().date() until=timezone.now().date()
.replace(year=1991, month=9, day=1) .replace(year=1990, month=9, day=1)
) )
with self.assertRaises(ValidationError): with self.assertRaises(ValidationError):
self.testboard.validate_unique() b.full_clean()
b.until = b.until.replace(year=1990, month=8, day=31)
b.full_clean()
b.until = None
with self.assertRaises(ValidationError):
b.full_clean()
def test_create_unique_period2(self):
""" Check uniqueness with until after period of testboard """
b = Board(
name_nl="testbe",
name_en="testbo",
contact_email="test@test.com",
description_nl="descnl",
description_en="descen",
since=timezone.now().date()
.replace(year=1991, month=8, day=1),
until=timezone.now().date()
.replace(year=1992, month=9, day=1)
)
self.testboard.until = None
with self.assertRaises(ValidationError): with self.assertRaises(ValidationError):
self.testboard.validate_unique() b.full_clean()
self.testboard.since = None b.since = b.since.replace(year=1991, month=9, day=2)
self.testboard.validate_unique() b.full_clean()
b.until = None
b.full_clean()
def test_get_absolute_url(self): def test_get_absolute_url(self):
self.testboard.get_absolute_url() self.testboard.get_absolute_url()
Supports Markdown
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