Commit 521a1e08 authored by Joost Rijneveld's avatar Joost Rijneveld
Browse files

Merge branch '70-not-yet-18' into 'master'

Add two age filters in the members admin page

Closes #70

See merge request !66
parents 7046acf8 e90d47ee
......@@ -7,6 +7,8 @@ from django.contrib.auth.models import User
from django.contrib.auth.forms import UserCreationForm as BaseUserCreationForm
from django.utils.translation import ugettext_lazy as _
import datetime
from . import models
......@@ -43,6 +45,34 @@ class MembershipTypeListFilter(admin.SimpleListFilter):
return queryset.filter(pk__in=users)
class AgeListFilter(admin.SimpleListFilter):
title = _('Age')
parameter_name = 'birthday'
def lookups(self, request, model_admin):
return models.Member.AGE_TYPES
def queryset(self, request, queryset):
print(self.value())
if not self.value():
return queryset
users = set()
for user in queryset:
try:
today = datetime.date.today()
eightteen_years_ago = today.replace(year=today.year - 18)
if (user.member.birthday <= eightteen_years_ago
and self.value() == '18+'):
users.add(user.pk)
if (user.member.birthday > eightteen_years_ago
and self.value() == '18-'):
users.add(user.pk)
except models.Member.DoesNotExist:
# The superuser does not have a .member object attached.
pass
return queryset.filter(pk__in=users)
class UserCreationForm(BaseUserCreationForm):
class Meta(BaseUserCreationForm.Meta):
fields = ('username', 'first_name', 'last_name')
......@@ -55,7 +85,8 @@ class UserAdmin(BaseUserAdmin):
# FIXME include proper filter for expiration
# https://docs.djangoproject.com/en/1.9/ref/contrib/admin/#django.contrib.admin.ModelAdmin.list_filter
list_filter = (MembershipTypeListFilter,
'is_superuser',)
'is_superuser',
AgeListFilter,)
add_fieldsets = (
(None, {
......
......@@ -196,6 +196,11 @@ class Member(models.Model):
null=True
)
AGE_TYPES = (
('18+', _('18+')),
('18-', _('Not yet 18')),
)
show_birthday = models.BooleanField(
verbose_name=_('Display birthday'),
help_text=_(
......
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