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

Rename CommitteeMembership to MemberGroupMembership

parent 4ae90c69
......@@ -11,30 +11,30 @@ from django.utils import timezone
from django.utils.translation import ugettext_lazy as _
from activemembers import models
from activemembers.forms import CommitteeMembershipForm
from activemembers.forms import MemberGroupMembershipForm
from utils.translation import TranslatedModelAdmin
from utils.snippets import datetime_to_lectureyear
class CommitteeMembershipInlineFormSet(forms.BaseInlineFormSet):
class MemberGroupMembershipInlineFormSet(forms.BaseInlineFormSet):
"""
Solely here for performance reasons.
Needed because the `__str__()` of `CommitteeMembership` (which is displayed
Needed because the `__str__()` of `MemberGroupMembership` (which is displayed
above each inline form) uses the username, name of the member and name of
the committee.
the group.
"""
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.queryset = self.queryset.select_related(
'member', 'committee').filter(until=None)
'member', 'group').filter(until=None)
class CommitteeMembershipInline(admin.StackedInline):
"""Inline for committee memberships"""
model = models.CommitteeMembership
formset = CommitteeMembershipInlineFormSet
class MemberGroupMembershipInline(admin.StackedInline):
"""Inline for group memberships"""
model = models.MemberGroupMembership
formset = MemberGroupMembershipInlineFormSet
can_delete = False
ordering = ('since',)
extra = 0
......@@ -60,7 +60,7 @@ class CommitteeForm(forms.ModelForm):
@admin.register(models.Committee)
class CommitteeAdmin(TranslatedModelAdmin):
"""Manage the committees"""
inlines = (CommitteeMembershipInline,)
inlines = (MemberGroupMembershipInline,)
form = CommitteeForm
list_display = ('name', 'since', 'until', 'active', 'email')
list_filter = ('until', 'active',)
......@@ -80,13 +80,13 @@ class CommitteeAdmin(TranslatedModelAdmin):
def get_queryset(self, request):
qs = super().get_queryset(request)
return qs.exclude(board__is_board=True)
return qs.exclude(board=None)
@admin.register(models.Board)
class BoardAdmin(TranslatedModelAdmin):
"""Manage the board"""
inlines = (CommitteeMembershipInline,)
inlines = (MemberGroupMembershipInline,)
form = CommitteeForm
exclude = ('is_board',)
filter_horizontal = ('permissions',)
......@@ -108,9 +108,9 @@ class BoardFilter(admin.SimpleListFilter):
def queryset(self, request, queryset):
if self.value() == 'only':
return queryset.filter(committee__board__is_board=True)
return queryset.exclude(group__board=None)
elif self.value() == 'none':
return queryset.exclude(committee__board__is_board=True)
return queryset.filter(group__board=None)
return queryset
......@@ -123,7 +123,7 @@ class LectureYearFilter(admin.SimpleListFilter):
def lookups(self, request, model_admin):
current_year = datetime_to_lectureyear(timezone.now())
first_year = datetime_to_lectureyear(
models.CommitteeMembership.objects.earliest('since').since
models.MemberGroupMembership.objects.earliest('since').since
)
return [(year, '{}-{}'.format(year, year+1))
......@@ -161,14 +161,14 @@ class ActiveMembershipsFilter(admin.SimpleListFilter):
return queryset.filter(until__lt=now)
@admin.register(models.CommitteeMembership)
class CommitteeMembershipAdmin(TranslatedModelAdmin):
"""Manage the committee memberships"""
form = CommitteeMembershipForm
list_display = ('member', 'committee', 'since', 'until', 'chair', 'role')
list_filter = ('committee', BoardFilter, LectureYearFilter,
@admin.register(models.MemberGroupMembership)
class MemberGroupMembershipAdmin(TranslatedModelAdmin):
"""Manage the group memberships"""
form = MemberGroupMembershipForm
list_display = ('member', 'group', 'since', 'until', 'chair', 'role')
list_filter = ('group', BoardFilter, LectureYearFilter,
ActiveMembershipsFilter)
list_select_related = ('member', 'committee',)
list_select_related = ('member', 'group',)
search_fields = ('member__first_name', 'member__last_name',
'member__email')
......@@ -176,8 +176,8 @@ class CommitteeMembershipAdmin(TranslatedModelAdmin):
def changelist_view(self, request, extra_context=None):
self.message_user(request, _('Do not edit existing memberships if the '
'chair of a committee has changed, add a '
'new committeemembership instead.'),
'chair of a group has changed, add a '
'new membership instead.'),
messages.WARNING)
return super().changelist_view(request, extra_context)
......@@ -185,16 +185,16 @@ class CommitteeMembershipAdmin(TranslatedModelAdmin):
response = HttpResponse(content_type='text/csv')
response['Content-Disposition'] = ('attachment;'
'filename='
'"committee_memberships.csv"')
'"group_memberships.csv"')
writer = csv.writer(response)
writer.writerow([
_('First name'),
_('Last name'),
_('Email'),
_('Committee'),
_('Committee member since'),
_('Committee member until'),
_('Chair of the committee'),
_('Group'),
_('Member since'),
_('Member until'),
_('Chair of the group'),
_('Role'),
])
......@@ -203,7 +203,7 @@ class CommitteeMembershipAdmin(TranslatedModelAdmin):
membership.member.first_name,
membership.member.last_name,
membership.member.email,
membership.committee,
membership.group,
membership.since,
membership.until,
membership.chair,
......
......@@ -28,8 +28,8 @@ class CommitteeBackend(object):
return set()
committees = member.committee_set.filter(
Q(committeemembership__until=None) |
Q(committeemembership__until__gte=timezone.now())
Q(membergroupmembership__until=None) |
Q(membergroupmembership__until__gte=timezone.now())
)
perm_cache_name = '_committee_perm_cache'
......
"""The forms defined by the activemembers module"""
from django import forms
from activemembers.models import CommitteeMembership
from activemembers.models import MemberGroupMembership
from members.models import Member
class CommitteeMembershipForm(forms.ModelForm):
"""Custom form for committee memberships that orders the members"""
class MemberGroupMembershipForm(forms.ModelForm):
"""Custom form for group memberships that orders the members"""
member = forms.ModelChoiceField(
queryset=Member.objects.order_by('first_name',
'last_name'))
class Meta:
model = CommitteeMembership
model = MemberGroupMembership
exclude = ()
# Generated by Django 2.0.8 on 2018-09-01 13:45
import datetime
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('members', '0023_auto_20180819_1542'),
('activemembers', '0026_auto_20180203_2151'),
]
operations = [
migrations.RenameModel(old_name='CommitteeMembership', new_name='MemberGroupMembership'),
migrations.RenameField(old_name='committee', new_name='group', model_name='membergroupmembership'),
migrations.AlterModelOptions(
name='membergroupmembership',
options={'verbose_name': 'group membership', 'verbose_name_plural': 'group memberships'},
),
migrations.AlterField(
model_name='membergroupmembership',
name='since',
field=models.DateField(default=datetime.date.today, help_text='The date this member joined in this role', verbose_name='Member since'),
),
migrations.AlterField(
model_name='membergroupmembership',
name='until',
field=models.DateField(blank=True, help_text="A member until this time (can't be in the future).", null=True, verbose_name='Member until'),
),
]
......@@ -30,7 +30,7 @@ class CommitteeManager(models.Manager):
def get_queryset(self):
return (super().get_queryset()
.exclude(board__is_board=True)
.filter(board=None)
.order_by(localize_attr_name('name')))
......@@ -39,7 +39,7 @@ class ActiveCommitteeManager(models.Manager):
def get_queryset(self):
return (super().get_queryset()
.exclude(board__is_board=True)
.filter(board=None)
.exclude(active=False)
.order_by(localize_attr_name('name')))
......@@ -72,7 +72,7 @@ class Committee(models.Model, metaclass=ModelTranslateMeta):
members = models.ManyToManyField(
'members.Member',
through='CommitteeMembership'
through='activemembers.MemberGroupMembership'
)
permissions = models.ManyToManyField(
......@@ -210,8 +210,8 @@ class ActiveMembershipManager(models.Manager):
return super().get_queryset().exclude(until__lt=timezone.now().date())
class CommitteeMembership(models.Model, metaclass=ModelTranslateMeta):
"""Describes a committee membership"""
class MemberGroupMembership(models.Model, metaclass=ModelTranslateMeta):
"""Describes a group membership"""
objects = models.Manager()
active_memberships = ActiveMembershipManager()
......@@ -221,21 +221,21 @@ class CommitteeMembership(models.Model, metaclass=ModelTranslateMeta):
verbose_name=_('Member'),
)
committee = models.ForeignKey(
group = models.ForeignKey(
Committee,
on_delete=models.CASCADE,
verbose_name=_('Committee'),
)
since = models.DateField(
verbose_name=_('Committee member since'),
help_text=_('The date this member joined the committee in this role'),
verbose_name=_('Member since'),
help_text=_('The date this member joined in this role'),
default=datetime.date.today
)
until = models.DateField(
verbose_name=_('Committee member until'),
help_text=_("A member of this committee until this time "
verbose_name=_('Member until'),
help_text=_("A member until this time "
"(can't be in the future)."),
blank=True,
null=True,
......@@ -259,8 +259,8 @@ class CommitteeMembership(models.Model, metaclass=ModelTranslateMeta):
@property
def initial_connected_membership(self):
"""Find the oldest membership directly connected to the current one"""
qs = CommitteeMembership.objects.filter(
committee=self.committee,
qs = MemberGroupMembership.objects.filter(
group=self.group,
member=self.member,
until__lte=self.since,
until__gte=self.since - datetime.timedelta(days=1))
......@@ -282,18 +282,18 @@ class CommitteeMembership(models.Model, metaclass=ModelTranslateMeta):
raise ValidationError(
{'until': _("End date can't be in the future")})
if (self.since and self.committee.since and
self.since < self.committee.since):
if (self.since and self.group.since and
self.since < self.group.since):
raise ValidationError(
{'since': _("Start date can't be before committee start date")}
)
if (self.since and self.committee.until and
self.since > self.committee.until):
if (self.since and self.group.until and
self.since > self.group.until):
raise ValidationError(
{'since': _("Start date can't be after committee end date")})
try:
if self.until and self.committee.board:
if self.until and self.group.board:
raise ValidationError(
{'until': _("End date cannot be set for boards")})
except Board.DoesNotExist:
......@@ -303,8 +303,8 @@ class CommitteeMembership(models.Model, metaclass=ModelTranslateMeta):
super().validate_unique(*args, **kwargs)
# Check if a committee has more than one chair
if self.chair:
chairs = (CommitteeMembership.objects
.filter(committee=self.committee,
chairs = (MemberGroupMembership.objects
.filter(group=self.group,
chair=True))
for chair in chairs:
if chair.pk == self.pk:
......@@ -321,8 +321,8 @@ class CommitteeMembership(models.Model, metaclass=ModelTranslateMeta):
'chair for this time period')})
# check if this member is already in the committee in this period
memberships = (CommitteeMembership.objects
.filter(committee=self.committee,
memberships = (MemberGroupMembership.objects
.filter(group=self.group,
member=self.member))
for mship in memberships:
if mship.pk == self.pk:
......@@ -334,26 +334,26 @@ class CommitteeMembership(models.Model, metaclass=ModelTranslateMeta):
self.since < mship.until and
self.until > mship.since)):
raise ValidationError({
'member': _('This member is already in the committee for '
'member': _('This member is already in the group for '
'this period')})
def save(self, *args, **kwargs):
super().save(*args, **kwargs)
self.member.is_staff = (self.member
.committeemembership_set
.membergroupmembership_set
.exclude(until__lte=timezone.now().date())
.count()) >= 1
self.member.save()
def __str__(self):
return "{} membership of {} since {}, until {}".format(self.member,
self.committee,
self.group,
self.since,
self.until)
class Meta:
verbose_name = _('committee membership')
verbose_name_plural = _('committee memberships')
verbose_name = _('group membership')
verbose_name_plural = _('group memberships')
class Mentorship(models.Model):
......
......@@ -4,7 +4,7 @@ from django.db.utils import IntegrityError
from django.test import TestCase
from django.utils import timezone
from activemembers.models import Committee, CommitteeMembership, Board
from activemembers.models import Committee, MemberGroupMembership, Board
from mailinglists.models import MailingList
from members.models import Member
......@@ -16,8 +16,8 @@ class CommitteeMembersTest(TestCase):
def setUpTestData(cls):
cls.testcie = Committee.objects.get(pk=1)
cls.testuser = Member.objects.get(pk=1)
cls.m = CommitteeMembership.objects.create(
committee=cls.testcie,
cls.m = MemberGroupMembership.objects.create(
group=cls.testcie,
member=cls.testuser,
chair=False)
......@@ -36,48 +36,48 @@ class CommitteeMembersTest(TestCase):
def test_join(self):
testuser2 = Member.objects.get(pk=2)
m = CommitteeMembership(committee=self.testcie,
member=testuser2)
m = MemberGroupMembership(group=self.testcie,
member=testuser2)
m.full_clean()
m.save()
def test_join_unique(self):
m = CommitteeMembership(committee=self.testcie,
member=self.testuser)
m = MemberGroupMembership(group=self.testcie,
member=self.testuser)
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(
m = MemberGroupMembership(group=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(
m1 = MemberGroupMembership(group=self.testcie,
member=self.testuser,
since=timezone.now().date().replace(
year=2014, month=1, day=1),
until=timezone.now().date().replace(
until=timezone.now().date().replace(
year=2014, month=3, day=1))
m1.save()
m2 = CommitteeMembership(committee=self.testcie,
member=self.testuser,
since=timezone.now().date().replace(
m2 = MemberGroupMembership(group=self.testcie,
member=self.testuser,
since=timezone.now().date().replace(
year=2014, month=1, day=1),
until=timezone.now().date().replace(
until=timezone.now().date().replace(
year=2014, month=2, day=1))
with self.assertRaises(ValidationError):
m2.full_clean()
def test_until_date(self):
m = CommitteeMembership(committee=self.testcie,
member=self.testuser,
until=timezone.now().date().replace(year=2000),
chair=False)
m = MemberGroupMembership(group=self.testcie,
member=self.testuser,
until=timezone.now().date().replace(year=2000),
chair=False)
with self.assertRaises(ValidationError):
m.clean()
m.since = timezone.now().date().replace(year=1900)
......@@ -99,19 +99,19 @@ class CommitteeMembersChairTest(TestCase):
cls.testuser2 = Member.objects.get(pk=2)
def setUp(self):
self.m1 = CommitteeMembership(committee=self.testcie,
since=timezone.now().date().replace(
self.m1 = MemberGroupMembership(group=self.testcie,
since=timezone.now().date().replace(
day=1,
year=1900),
member=self.testuser,
chair=True)
member=self.testuser,
chair=True)
self.m1.full_clean()
self.m1.save()
def test_second_chair_fails(self):
m = CommitteeMembership(committee=self.testcie,
member=self.testuser2,
chair=True)
m = MemberGroupMembership(group=self.testcie,
member=self.testuser2,
chair=True)
with self.assertRaises(ValidationError):
m.full_clean()
......@@ -119,9 +119,9 @@ class CommitteeMembersChairTest(TestCase):
self.m1.until = timezone.now().date().replace(day=1, year=1900)
self.m1.save()
m = CommitteeMembership(committee=self.testcie,
member=self.testuser2,
chair=True)
m = MemberGroupMembership(group=self.testcie,
member=self.testuser2,
chair=True)
m.full_clean()
def test_clean_self_chair(self):
......@@ -141,10 +141,10 @@ class PermissionsBackendTest(TestCase):
cls.u3 = Member.objects.get(pk=3)
cls.c1 = Committee.objects.get(pk=1)
cls.c2 = Committee.objects.get(pk=2)
cls.m1 = CommitteeMembership.objects.create(committee=cls.c1,
member=cls.u1)
cls.m2 = CommitteeMembership.objects.create(committee=cls.c2,
member=cls.u2)
cls.m1 = MemberGroupMembership.objects.create(group=cls.c1,
member=cls.u1)
cls.m2 = MemberGroupMembership.objects.create(group=cls.c2,
member=cls.u2)
def test_permissions(self):
self.assertEqual(3, len(self.u1.get_all_permissions()))
......
......@@ -2,7 +2,7 @@ from django.shortcuts import get_object_or_404, render, redirect, reverse
import datetime
from utils.snippets import datetime_to_lectureyear
from utils.translation import localize_attr_name
from .models import Board, Committee, CommitteeMembership
from .models import Board, Committee, MemberGroupMembership
def committee_index(request):
......@@ -30,10 +30,10 @@ def committee_detail(request, pk):
committee = get_object_or_404(Committee, pk=pk)
members = []
memberships = (CommitteeMembership
memberships = (MemberGroupMembership
.active_memberships
.filter(committee=committee)
.prefetch_related('member__committeemembership_set'))
.filter(group=committee)
.prefetch_related('member__membergroupmembership_set'))
for membership in memberships:
member = membership.member
member.chair = membership.chair
......@@ -81,9 +81,9 @@ def board_detail(request, since, until=None):
'until': int(since) + 1}))
board = get_object_or_404(Board, since__year=since, until__year=until)
members = []
memberships = (CommitteeMembership
memberships = (MemberGroupMembership
.objects
.filter(committee=board)
.filter(group=board)
.prefetch_related('member'))
for membership in memberships:
......
......@@ -211,7 +211,7 @@ class EventAdmin(DoNextModelAdmin):
kwargs['queryset'] = (
Committee.active_committees.all() |
Committee.unfiltered_objects
.exclude(board__is_board=False)
.filter(board=None)
.exclude(until__lt=(timezone.now() -
timezone.timedelta(weeks=1)))
)
......
......@@ -7,7 +7,7 @@ from django.test import TestCase, RequestFactory, override_settings
from django.utils import timezone
from freezegun import freeze_time
from activemembers.models import Committee, CommitteeMembership
from activemembers.models import Committee, MemberGroupMembership
from events.admin import (
DoNextModelAdmin,
RegistrationInformationFieldInline,
......@@ -97,9 +97,9 @@ class RegistrationInformationFieldInlineTest(TestCase):
cls.member = Member.objects.filter(last_name="Wiggers").first()
cls.member.is_superuser = True
cls.member.save()
CommitteeMembership.objects.create(
MemberGroupMembership.objects.create(
member=cls.member,
committee=cls.committee
group=cls.committee
)
RegistrationInformationField.objects.create(
......
......@@ -6,7 +6,7 @@ from django.test import TestCase
from django.utils import timezone
from freezegun import freeze_time
from activemembers.models import Committee, CommitteeMembership
from activemembers.models import Committee, MemberGroupMembership
from events import services
from events.exceptions import RegistrationError
from events.models import Event, Registration, RegistrationInformationField
......@@ -160,8 +160,8 @@ class ServicesTest(TestCase):
self._toggle_event_change_perm(True)
self.assertFalse(services.is_organiser(self.member, self.event))
membership = CommitteeMembership.objects.create(
member=self.member, committee=self.committee)
membership = MemberGroupMembership.objects.create(
member=self.member, group=self.committee)
self.assertTrue(services.is_organiser(self.member, self.event))
self.assertFalse(services.is_organiser(self.member, None))
self._toggle_event_change_perm(False)
......
......@@ -5,7 +5,7 @@ from django.core import mail
from django.test import Client, TestCase
from django.utils import timezone
from activemembers.models import Committee, CommitteeMembership
from activemembers.models import Committee, MemberGroupMembership
from events.models import (Event, Registration,
RegistrationInformationField,
BooleanRegistrationInformation,
......@@ -70,9 +70,9 @@ class AdminTest(TestCase):