2021-07-08 12:48:51 +00:00
|
|
|
from django.contrib import admin, messages
|
2023-10-24 16:59:02 +00:00
|
|
|
from django.utils.translation import gettext_lazy as _
|
2014-09-30 09:49:07 +00:00
|
|
|
|
2014-10-06 14:57:02 +00:00
|
|
|
from orchestra.admin import ExtendedModelAdmin, ChangePasswordAdminMixin
|
2016-03-31 16:02:50 +00:00
|
|
|
from orchestra.admin.actions import disable, enable
|
2015-07-21 10:44:32 +00:00
|
|
|
from orchestra.contrib.accounts.actions import list_accounts
|
2015-04-05 10:46:24 +00:00
|
|
|
from orchestra.contrib.accounts.admin import SelectAccountAdminMixin
|
|
|
|
from orchestra.contrib.accounts.filters import IsActiveListFilter
|
2014-09-30 09:49:07 +00:00
|
|
|
|
2021-07-08 12:48:51 +00:00
|
|
|
from .actions import set_permission, create_link
|
2014-10-23 15:38:46 +00:00
|
|
|
from .filters import IsMainListFilter
|
2014-11-14 22:19:58 +00:00
|
|
|
from .forms import SystemUserCreationForm, SystemUserChangeForm
|
2014-09-30 09:49:07 +00:00
|
|
|
from .models import SystemUser
|
|
|
|
|
|
|
|
|
2014-10-06 14:57:02 +00:00
|
|
|
class SystemUserAdmin(ChangePasswordAdminMixin, SelectAccountAdminMixin, ExtendedModelAdmin):
|
2014-11-14 15:51:18 +00:00
|
|
|
list_display = (
|
|
|
|
'username', 'account_link', 'shell', 'display_home', 'display_active', 'display_main'
|
|
|
|
)
|
2015-03-26 16:00:30 +00:00
|
|
|
list_filter = (IsActiveListFilter, 'shell', IsMainListFilter)
|
2014-09-30 09:49:07 +00:00
|
|
|
fieldsets = (
|
|
|
|
(None, {
|
2014-09-30 10:20:11 +00:00
|
|
|
'fields': ('username', 'password', 'account_link', 'is_active')
|
2014-09-30 09:49:07 +00:00
|
|
|
}),
|
|
|
|
(_("System"), {
|
2014-11-14 14:38:06 +00:00
|
|
|
'fields': ('shell', ('home', 'directory'), 'groups'),
|
2014-09-30 09:49:07 +00:00
|
|
|
}),
|
|
|
|
)
|
|
|
|
add_fieldsets = (
|
|
|
|
(None, {
|
2014-10-23 15:38:46 +00:00
|
|
|
'fields': ('account_link', 'username', 'password1', 'password2')
|
2014-09-30 09:49:07 +00:00
|
|
|
}),
|
|
|
|
(_("System"), {
|
2014-11-14 14:38:06 +00:00
|
|
|
'fields': ('shell', ('home', 'directory'), 'groups'),
|
2014-09-30 09:49:07 +00:00
|
|
|
}),
|
|
|
|
)
|
2015-03-10 11:46:48 +00:00
|
|
|
search_fields = ('username', 'account__username')
|
2014-09-30 16:39:47 +00:00
|
|
|
readonly_fields = ('account_link',)
|
2014-09-30 09:49:07 +00:00
|
|
|
change_readonly_fields = ('username',)
|
|
|
|
filter_horizontal = ('groups',)
|
|
|
|
filter_by_account_fields = ('groups',)
|
2014-11-14 22:19:58 +00:00
|
|
|
add_form = SystemUserCreationForm
|
|
|
|
form = SystemUserChangeForm
|
2014-09-30 09:49:07 +00:00
|
|
|
ordering = ('-id',)
|
2016-02-09 12:17:42 +00:00
|
|
|
change_view_actions = (set_permission, create_link)
|
2021-07-08 12:48:51 +00:00
|
|
|
actions = (disable, enable, list_accounts) + change_view_actions
|
|
|
|
|
2014-10-23 15:38:46 +00:00
|
|
|
def display_main(self, user):
|
|
|
|
return user.is_main
|
|
|
|
display_main.short_description = _("Main")
|
|
|
|
display_main.boolean = True
|
2021-07-08 12:48:51 +00:00
|
|
|
|
2014-11-14 14:38:06 +00:00
|
|
|
def display_home(self, user):
|
|
|
|
return user.get_home()
|
|
|
|
display_home.short_description = _("Home")
|
|
|
|
display_home.admin_order_field = 'home'
|
2021-07-08 12:48:51 +00:00
|
|
|
|
2014-09-30 09:49:07 +00:00
|
|
|
def get_form(self, request, obj=None, **kwargs):
|
2015-03-26 16:00:30 +00:00
|
|
|
form = super(SystemUserAdmin, self).get_form(request, obj, **kwargs)
|
2014-11-14 22:19:58 +00:00
|
|
|
form.account = self.account
|
2014-09-30 09:49:07 +00:00
|
|
|
if obj:
|
|
|
|
# Has to be done here and not in the form because of strange phenomenon
|
|
|
|
# derived from monkeypatching formfield.widget.render on AccountAdminMinxin,
|
|
|
|
# don't ask.
|
|
|
|
formfield = form.base_fields['groups']
|
2014-10-01 16:42:40 +00:00
|
|
|
formfield.queryset = formfield.queryset.exclude(id=obj.id)
|
2014-09-30 09:49:07 +00:00
|
|
|
return form
|
2021-07-08 12:48:51 +00:00
|
|
|
|
2014-10-23 15:38:46 +00:00
|
|
|
def has_delete_permission(self, request, obj=None):
|
|
|
|
if obj and obj.is_main:
|
2021-07-08 12:48:51 +00:00
|
|
|
self.message_user(request, _(
|
|
|
|
"You have selected one main system user (%(account)s), which can not be deleted.",
|
|
|
|
) % {'account': obj},
|
|
|
|
messages.ERROR,
|
|
|
|
)
|
|
|
|
|
2014-10-23 15:38:46 +00:00
|
|
|
return False
|
2015-03-26 16:00:30 +00:00
|
|
|
return super(SystemUserAdmin, self).has_delete_permission(request, obj)
|
2014-09-30 09:49:07 +00:00
|
|
|
|
2014-11-14 14:38:06 +00:00
|
|
|
|
2014-09-30 09:49:07 +00:00
|
|
|
admin.site.register(SystemUser, SystemUserAdmin)
|