Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1# (c) 2018-2020
2# MPIB <https://www.mpib-berlin.mpg.de/>,
3# MPI-CBS <https://www.cbs.mpg.de/>,
4# MPIP <http://www.psych.mpg.de/>
5#
6# This file is part of Castellum.
7#
8# Castellum is free software; you can redistribute it and/or modify it
9# under the terms of the GNU Affero General Public License as published
10# by the Free Software Foundation; either version 3 of the License, or
11# (at your option) any later version.
12#
13# Castellum is distributed in the hope that it will be useful, but
14# WITHOUT ANY WARRANTY; without even the implied warranty of
15# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16# Affero General Public License for more details.
17#
18# You should have received a copy of the GNU Affero General Public
19# License along with Castellum. If not, see
20# <http://www.gnu.org/licenses/>.
22from django.contrib import admin
23from django.contrib.auth.admin import UserAdmin as BaseUserAdmin
24from django.contrib.auth.models import Group
25from django.shortcuts import render
26from django.utils import formats
27from django.utils import timezone
28from django.utils.translation import gettext_lazy as _
30from .forms import UserCreationAdminForm
31from .models import User
34def expiration_date_repr(obj):
35 if not obj.expiration_date:
36 return
38 formatted_date = formats.date_format(obj.expiration_date, 'SHORT_DATE_FORMAT')
40 if obj.expiration_date < timezone.now():
41 expire = _('expired')
42 else:
43 expires_in = obj.expiration_date - timezone.now()
44 expire = _('%i days left') % expires_in.days
46 return '%s (%s)' % (formatted_date, expire)
49expiration_date_repr.admin_order_field = 'expiration_date'
50expiration_date_repr.short_description = _('expiration date')
53@admin.register(User)
54class UserAdmin(BaseUserAdmin):
55 add_form = UserCreationAdminForm
56 list_display = BaseUserAdmin.list_display + (expiration_date_repr,)
57 actions = ['list_permissions']
58 fieldsets = (
59 (None, {
60 'fields': ('username', 'password')
61 }),
62 (_('personal info'), {
63 'fields': ('first_name', 'last_name', 'email', 'language')
64 }),
65 (_('permissions'), {
66 'fields': ('is_active', 'is_staff', 'is_superuser', 'groups', 'user_permissions')
67 }),
68 (_('important dates'), {
69 'fields': ('last_login', 'date_joined', 'expiration_date', 'logout_timeout')
70 }),
71 )
73 def list_permissions(self, request, queryset):
74 return render(request, 'admin/castellum_auth/user_permissions.html', {
75 'opts': User._meta,
76 'title': _('Permission list'),
77 'users': queryset.all(),
78 'groups': Group.objects.all(),
79 })
80 list_permissions.short_description = _('List all permissions')