2019-08-02 12:57:53 +00:00
|
|
|
# -*- mode: python; coding: utf-8 -*-
|
|
|
|
# Copyright (C) 2017-2019 by BDE ENS Paris-Saclay
|
|
|
|
# SPDX-License-Identifier: GPL-3.0-or-later
|
2017-06-11 23:34:13 +00:00
|
|
|
|
|
|
|
from django.contrib import admin
|
|
|
|
from django.contrib.auth.admin import UserAdmin as BaseUserAdmin
|
2021-11-14 14:09:04 +00:00
|
|
|
from django.utils import timezone
|
|
|
|
from django.utils.safestring import mark_safe
|
2019-08-08 10:16:40 +00:00
|
|
|
from django.utils.translation import ugettext_lazy as _
|
2017-06-11 23:34:13 +00:00
|
|
|
from reversion.admin import VersionAdmin
|
2019-08-10 08:44:17 +00:00
|
|
|
from med.admin import admin_site
|
2020-09-25 08:27:43 +00:00
|
|
|
|
2021-10-23 12:49:44 +00:00
|
|
|
from .models import User
|
2017-07-04 22:47:05 +00:00
|
|
|
|
2019-08-02 12:57:53 +00:00
|
|
|
|
2019-08-10 14:22:04 +00:00
|
|
|
class IsMemberFilter(admin.SimpleListFilter):
|
|
|
|
title = _('membership status')
|
|
|
|
parameter_name = 'is_member'
|
2019-08-08 11:04:40 +00:00
|
|
|
|
|
|
|
def lookups(self, request, model_admin):
|
|
|
|
return (
|
|
|
|
('Yes', _('Yes')),
|
|
|
|
)
|
|
|
|
|
|
|
|
def queryset(self, request, queryset):
|
2021-11-14 14:09:04 +00:00
|
|
|
if self.parameter_name in request.GET:
|
|
|
|
queryset = queryset.filter(
|
|
|
|
membership__date_start__lte=timezone.now(),
|
|
|
|
membership__date_end__gte=timezone.now(),
|
|
|
|
).distinct()
|
|
|
|
|
2019-08-08 11:04:40 +00:00
|
|
|
return queryset
|
|
|
|
|
|
|
|
|
2017-06-11 23:34:13 +00:00
|
|
|
class UserAdmin(VersionAdmin, BaseUserAdmin):
|
2019-08-08 10:16:40 +00:00
|
|
|
# Customize admin to add more fields
|
2017-06-11 23:34:13 +00:00
|
|
|
fieldsets = (
|
2019-08-02 19:35:30 +00:00
|
|
|
(None, {'fields': ('username', 'password')}),
|
2019-08-08 10:16:40 +00:00
|
|
|
(_('Personal info'), {'fields': ('first_name', 'last_name', 'email',
|
2021-11-14 13:26:41 +00:00
|
|
|
'phone_number', 'address',
|
|
|
|
'comment')}),
|
2019-08-08 10:16:40 +00:00
|
|
|
(_('Permissions'), {'fields': ('is_active', 'is_staff', 'is_superuser',
|
2021-11-14 13:26:41 +00:00
|
|
|
'groups', 'user_permissions')}),
|
2019-08-08 10:16:40 +00:00
|
|
|
(_('Important dates'), {'fields': ('last_login', 'date_joined')}),
|
2017-06-11 23:34:13 +00:00
|
|
|
)
|
2019-08-08 11:04:40 +00:00
|
|
|
list_display = ('username', 'email', 'first_name', 'last_name',
|
2021-11-14 13:26:41 +00:00
|
|
|
'is_member', 'is_staff')
|
2019-08-10 14:22:04 +00:00
|
|
|
list_filter = (IsMemberFilter, 'is_staff', 'is_superuser', 'is_active',
|
2019-08-08 11:04:40 +00:00
|
|
|
'groups')
|
|
|
|
|
2021-11-14 14:09:04 +00:00
|
|
|
def has_add_permission(self, request):
|
|
|
|
# Only add users through Note Kfet login
|
|
|
|
return False
|
2019-08-09 21:12:56 +00:00
|
|
|
|
2019-08-10 14:22:04 +00:00
|
|
|
def is_member(self, obj):
|
2019-08-08 11:04:40 +00:00
|
|
|
"""
|
|
|
|
Get current membership year and check if user is there
|
|
|
|
"""
|
2021-11-14 14:09:04 +00:00
|
|
|
if obj.is_member:
|
|
|
|
return mark_safe(
|
2019-08-08 19:51:37 +00:00
|
|
|
'<img src="/static/admin/img/icon-yes.svg" alt="True">'
|
|
|
|
)
|
|
|
|
else:
|
2021-11-14 14:09:04 +00:00
|
|
|
return mark_safe(
|
|
|
|
'<img src="/static/admin/img/icon-no.svg" alt="False">'
|
2019-08-08 19:51:37 +00:00
|
|
|
)
|
2019-08-08 11:04:40 +00:00
|
|
|
|
2019-08-10 14:22:04 +00:00
|
|
|
is_member.short_description = _('is member')
|
|
|
|
is_member.allow_tags = True
|
2019-08-08 19:51:37 +00:00
|
|
|
|
2019-08-02 12:57:53 +00:00
|
|
|
|
2019-08-10 06:04:46 +00:00
|
|
|
admin_site.register(User, UserAdmin)
|