mirror of
https://gitlab.crans.org/mediatek/med.git
synced 2024-12-25 17:42:25 +00:00
120 lines
4.2 KiB
Python
120 lines
4.2 KiB
Python
# -*- 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
|
|
from django.contrib import messages
|
|
from django.contrib.auth.admin import UserAdmin as BaseUserAdmin
|
|
from django.contrib.auth.forms import PasswordResetForm
|
|
from django.urls import reverse
|
|
from django.utils.html import format_html
|
|
from django.utils.translation import ugettext_lazy as _
|
|
from reversion.admin import VersionAdmin
|
|
|
|
from med.admin import admin_site
|
|
from .forms import UserCreationAdminForm
|
|
from .models import Adhesion, Clef, User
|
|
|
|
|
|
class ClefAdmin(VersionAdmin):
|
|
list_display = ('name', 'owner', 'comment')
|
|
ordering = ('name',)
|
|
search_fields = ('name', 'owner__username', 'comment')
|
|
autocomplete_fields = ('owner',)
|
|
|
|
|
|
class AdhesionAdmin(VersionAdmin):
|
|
list_display = ('starting_in', 'ending_in')
|
|
autocomplete_fields = ('members',)
|
|
|
|
|
|
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
|
|
last_adh_year = Adhesion.objects.all().order_by('starting_in') \
|
|
.reverse().first()
|
|
return last_adh_year.members
|
|
return queryset
|
|
|
|
|
|
class UserAdmin(VersionAdmin, BaseUserAdmin):
|
|
# Customize admin to add more fields
|
|
fieldsets = (
|
|
(None, {'fields': ('username', 'password')}),
|
|
(_('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',
|
|
'maxemprunt', 'is_member', 'is_staff')
|
|
list_filter = (IsMemberFilter, 'is_staff', 'is_superuser', 'is_active',
|
|
'groups')
|
|
|
|
# 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"),
|
|
}),
|
|
)
|
|
|
|
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."))
|
|
|
|
def is_member(self, obj):
|
|
"""
|
|
Get current membership year and check if user is there
|
|
"""
|
|
last_adh_year = Adhesion.objects.all().order_by('starting_in') \
|
|
.reverse().first()
|
|
is_member = last_adh_year and obj in last_adh_year.members.all()
|
|
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')
|
|
)
|
|
|
|
is_member.short_description = _('is member')
|
|
is_member.allow_tags = True
|
|
|
|
|
|
admin_site.register(User, UserAdmin)
|
|
admin_site.register(Adhesion, AdhesionAdmin)
|
|
admin_site.register(Clef, ClefAdmin)
|