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):
def validate_unique(self, *args, **kwargs):
""" Check uniqueness"""
super().validate_unique(*args, **kwargs)
if self.since is not None and self.until is not None:
for board in Board.objects.filter(since__year=self.since.year,
until__year=self.until.year):
if board != self:
boards = Board.objects.all()
if self.since is not None:
for board in boards:
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({
'since': _('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):
......
......@@ -168,27 +168,52 @@ class BoardTest(TestCase):
self.testboard.since = None
self.testboard.validate_unique()
def test_unique_periods(self):
Board.objects.create(
name_nl="testbe1",
name_en="testbo1",
def test_create_unique_period1(self):
""" Check uniqueness with since before 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=1990, month=9, day=1),
.replace(year=1990, month=2, day=1),
until=timezone.now().date()
.replace(year=1991, month=9, day=1)
.replace(year=1990, month=9, day=1)
)
with self.assertRaises(ValidationError):
self.testboard.validate_unique()
b.full_clean()
self.testboard.until = None
b.until = b.until.replace(year=1990, month=8, day=31)
b.full_clean()
b.until = None
with self.assertRaises(ValidationError):
self.testboard.validate_unique()
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.since = None
self.testboard.validate_unique()
with self.assertRaises(ValidationError):
b.full_clean()
b.since = b.since.replace(year=1991, month=9, day=2)
b.full_clean()
b.until = None
b.full_clean()
def test_get_absolute_url(self):
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