We planned to upgrade GitLab and Mattermost to the latest version this Friday morning. Expect some downtime!

Commit ddf986d4 authored by Joost Rijneveld's avatar Joost Rijneveld

members: refactor and test filters

parent a868eb8e
......@@ -6,6 +6,8 @@ from django.utils import timezone
from members.models import (Member, Membership,
gen_stats_member_type, gen_stats_year)
from members.views import filter_users
from utils.snippets import datetime_to_lectureyear
......@@ -59,6 +61,54 @@ class MemberBirthdayTest(TestCase):
self._assert_thom('1992-12-31', '1995-01-01')
class MembershipFilterTest(TestCase):
@classmethod
def setUpTestData(cls):
# Add 10 members with default membership
users = [get_user_model()(id=i, username=i) for i in range(7)]
get_user_model().objects.bulk_create(users)
members = [Member(user_id=i) for i in range(7)]
Member.objects.bulk_create(members)
Membership(user_id=0, type='honorary',
until=date.today() + timedelta(days=1)).save()
Membership(user_id=1, type='supporter',
until=date.today() + timedelta(days=1)).save()
Membership(user_id=2, type='member',
until=date.today() + timedelta(days=1)).save()
Membership(user_id=3, type='member',
until=date.today() + timedelta(days=1)).save()
Membership(user_id=3, type='member',
until=date.today() - timedelta(days=365*10)).save()
Membership(user_id=4, type='supporter',
until=date.today() + timedelta(days=1)).save()
Membership(user_id=4, type='member',
until=date.today() - timedelta(days=365*10)).save()
Membership(user_id=5, type='member',
until=date.today() - timedelta(days=365*10)).save()
# user_id=6 has no memberships at all
def test_honorary(self):
members = filter_users('honor', '', [date.today().year])
self.assertEqual(len(members), 1)
self.assertEqual(members[0].user.id, 0)
def test_ex(self):
members = filter_users('ex', '', [date.today().year])
self.assertEqual(len(members), 3)
for member in members:
self.assertIn(member.user.id, {4, 5, 6})
# TODO more tests for other cases
class StatisticsTest(TestCase):
@classmethod
......
......@@ -18,40 +18,21 @@ from . import models
from .forms import MemberForm
@login_required
def index(request):
query_filter = '' if request.GET.get(
'filter') is None else request.GET.get('filter')
keywords = '' if request.GET.get('keywords') is None else request.GET.get(
'keywords').split()
page = request.GET.get('page')
page = 1 if page is None or not page.isdigit() else int(page)
start_year = date.today().year - 4
# If language is English show one year less
# since the width is smaller than needed for the translations to fit
if request.LANGUAGE_CODE == 'en':
start_year += 1
year_range = reversed(range(start_year, date.today().year + 1))
def filter_users(tab, keywords, year_range):
memberships_query = Q(until__gt=datetime.now()) | Q(until=None)
members_query = ~Q(id=None)
if query_filter and query_filter.isdigit() and not (
query_filter == 'ex' or
query_filter == 'honor' or
query_filter == 'old'):
members_query &= Q(starting_year=int(query_filter))
elif query_filter == 'old':
members_query &= Q(starting_year__lt=start_year)
elif query_filter == 'ex':
# Filter out all current members and current honorary members
if tab and tab.isdigit():
members_query &= Q(starting_year=int(tab))
elif tab == 'old':
members_query &= Q(starting_year__lt=year_range[-1])
elif tab == 'ex':
# Filter out all current active memberships
memberships_query &= Q(type='member') | Q(type='honorary')
memberships = models.Membership.objects.filter(memberships_query)
members_query &= ~Q(user__in=memberships.values('user'))
# Members_query contains users that are not currently (honorary)member
elif query_filter == 'honor':
elif tab == 'honor':
memberships_query = Q(until__gt=datetime.now().date()) | Q(until=None)
memberships_query &= Q(type='honorary')
......@@ -62,7 +43,7 @@ def index(request):
Q(user__last_name__icontains=key) |
Q(user__username__icontains=key))
if query_filter == 'ex':
if tab == 'ex':
memberships_query = Q(type='member') | Q(type='honorary')
memberships = models.Membership.objects.filter(memberships_query)
all_memberships = models.Membership.objects.all()
......@@ -73,8 +54,29 @@ def index(request):
else:
memberships = models.Membership.objects.filter(memberships_query)
members_query &= Q(user__in=memberships.values('user'))
members = models.Member.objects.filter(members_query).order_by(
'-starting_year', 'user__first_name')
return (models.Member.objects.filter(members_query)
.order_by('-starting_year',
'user__first_name'))
@login_required
def index(request):
query_filter = '' if request.GET.get(
'filter') is None else request.GET.get('filter')
keywords = '' if request.GET.get('keywords') is None else request.GET.get(
'keywords').split()
page = request.GET.get('page')
page = 1 if page is None or not page.isdigit() else int(page)
start_year = date.today().year - 4
# If language is English show one year less
# since the width is smaller than needed for the translations to fit
if request.LANGUAGE_CODE == 'en':
start_year += 1
year_range = list(reversed(range(start_year, date.today().year + 1)))
members = filter_users(query_filter, keywords, year_range)
paginator = Paginator(members, 24)
......
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