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

Make it possible for a member to be a part of the same committee in different timespans

parent 32edc69d
......@@ -197,12 +197,24 @@ class CommitteeMembership(models.Model):
# check if this member is already in the committee
if self.pk is None:
members = (self.committee.members
.filter(pk=self.member.pk)
until = self.until if self.until else timezone.now().date()
members = (CommitteeMembership.active_memberships
.filter(committee=self.committee, member=self.member)
.count())
if members >= 1:
memberships = (CommitteeMembership.objects.filter(
committee=self.committee,
member=self.member,
since__lte=until,
until__gte=self.since)
.count())
if members >= 1 and until >= timezone.now().date():
raise ValidationError({
'member': _('This member is already in the committee')})
elif memberships > 0:
raise ValidationError({
'member': _('This member is already in the committee for '
'this period')})
def save(self, *args, **kwargs):
"""Save the instance"""
......
......@@ -39,6 +39,32 @@ class CommitteeMembersTest(TestCase):
with self.assertRaises(ValidationError):
m.full_clean()
def test_join_unique2(self):
m = CommitteeMembership(committee=self.testcie,
member=self.testuser,
since=timezone.now().date().replace(
year=2014, month=1))
with self.assertRaises(ValidationError):
m.full_clean()
def test_join_unique_period(self):
m1 = CommitteeMembership(committee=self.testcie,
member=self.testuser,
since=timezone.now().date().replace(
year=2014, month=1),
until=timezone.now().date().replace(
year=2014, month=3))
m1.save()
m2 = CommitteeMembership(committee=self.testcie,
member=self.testuser,
since=timezone.now().date().replace(
year=2014, month=1),
until=timezone.now().date().replace(
year=2014, month=2))
with self.assertRaises(ValidationError):
m2.full_clean()
def test_until_date(self):
m = CommitteeMembership(committee=self.testcie,
member=self.testuser,
......
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