1
0
mirror of https://gitlab.crans.org/mediatek/med.git synced 2024-11-27 01:27:10 +00:00
med/users/admin.py

112 lines
4.0 KiB
Python
Raw Normal View History

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
from django.contrib import admin
2019-08-09 21:12:56 +00:00
from django.contrib import messages
from django.contrib.auth.admin import UserAdmin as BaseUserAdmin
2019-08-09 21:12:56 +00:00
from django.contrib.auth.forms import PasswordResetForm
2019-08-10 08:44:17 +00:00
from django.urls import reverse
2019-08-08 19:51:37 +00:00
from django.utils.html import format_html
2019-08-08 10:16:40 +00:00
from django.utils.translation import ugettext_lazy as _
from reversion.admin import VersionAdmin
2019-08-10 08:44:17 +00:00
from med.admin import admin_site
2019-08-09 20:52:47 +00:00
from .forms import UserCreationAdminForm
2020-02-09 16:55:51 +00:00
from .models import Adhesion, User
2017-07-03 18:06:21 +00:00
2019-08-02 12:57:53 +00:00
class AdhesionAdmin(VersionAdmin):
2019-08-10 14:13:32 +00:00
list_display = ('starting_in', 'ending_in')
autocomplete_fields = ('members',)
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'
def lookups(self, request, model_admin):
return (
('Yes', _('Yes')),
)
def queryset(self, request, queryset):
value = self.value()
if value == 'Yes':
# Get current membership year and list all members
2019-08-10 14:13:32 +00:00
last_adh_year = Adhesion.objects.all().order_by('starting_in') \
2019-08-08 13:35:25 +00:00
.reverse().first()
2019-08-10 14:22:04 +00:00
return last_adh_year.members
return queryset
class UserAdmin(VersionAdmin, BaseUserAdmin):
2019-08-08 10:16:40 +00:00
# Customize admin to add more fields
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',
'telephone', 'address', 'comment')}),
(_('Permissions'), {'fields': ('is_active', 'is_staff', 'is_superuser',
'groups', 'user_permissions',
'maxemprunt')}),
(_('Important dates'), {'fields': ('last_login', 'date_joined')}),
)
list_display = ('username', 'email', 'first_name', 'last_name',
2019-08-10 14:22:04 +00:00
'maxemprunt', 'is_member', 'is_staff')
list_filter = (IsMemberFilter, 'is_staff', 'is_superuser', 'is_active',
'groups')
2019-08-09 20:52:47 +00:00
# Customize required initial fields
add_form_template = 'admin/change_form.html'
add_form = UserCreationAdminForm
add_fieldsets = (
(None, {
'classes': ('wide',),
'fields': ("username", "email", "first_name", "last_name",
"address", "telephone"),
}),
)
2019-08-09 21:12:56 +00:00
def save_model(self, request, obj, form, change):
"""
On creation, send a password init mail
"""
super().save_model(request, obj, form, change)
if not change:
# Virtually fill the password reset form
password_reset = PasswordResetForm(data={'email': obj.email})
if password_reset.is_valid():
password_reset.save(request=request,
use_https=request.is_secure())
messages.success(request, _("An email to set the password"
" was sent."))
else:
messages.error(request, _("The email is invalid."))
2019-08-10 14:22:04 +00:00
def is_member(self, obj):
"""
Get current membership year and check if user is there
"""
2019-08-10 14:13:32 +00:00
last_adh_year = Adhesion.objects.all().order_by('starting_in') \
2019-08-08 13:35:25 +00:00
.reverse().first()
2019-08-10 14:22:04 +00:00
is_member = last_adh_year and obj in last_adh_year.members.all()
2019-08-08 19:51:37 +00:00
if is_member:
return format_html(
'<img src="/static/admin/img/icon-yes.svg" alt="True">'
)
else:
return format_html(
'<img src="/static/admin/img/icon-no.svg" alt="False"> '
'<a class="button" href="{}">{}</a>',
reverse('users:adherer', args=[obj.pk]),
_('Adhere')
)
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)
admin_site.register(Adhesion, AdhesionAdmin)