2014-11-14 14:38:06 +00:00
|
|
|
import textwrap
|
|
|
|
|
|
|
|
from django import forms
|
2014-09-30 09:49:07 +00:00
|
|
|
from django.conf.urls import patterns, url
|
|
|
|
from django.core.urlresolvers import reverse
|
|
|
|
from django.contrib import admin
|
|
|
|
from django.contrib.admin.util import unquote
|
2014-10-06 14:57:02 +00:00
|
|
|
from django.contrib.auth.admin import UserAdmin
|
2014-09-30 09:49:07 +00:00
|
|
|
from django.utils.translation import ugettext, ugettext_lazy as _
|
2014-10-23 15:38:46 +00:00
|
|
|
from django.utils.safestring import mark_safe
|
2014-09-30 09:49:07 +00:00
|
|
|
|
2014-10-06 14:57:02 +00:00
|
|
|
from orchestra.admin import ExtendedModelAdmin, ChangePasswordAdminMixin
|
2014-09-30 09:49:07 +00:00
|
|
|
from orchestra.admin.utils import wrap_admin_view
|
2014-09-30 16:39:47 +00:00
|
|
|
from orchestra.apps.accounts.admin import SelectAccountAdminMixin
|
2014-10-06 14:57:02 +00:00
|
|
|
from orchestra.forms import UserCreationForm, UserChangeForm
|
2014-09-30 09:49:07 +00:00
|
|
|
|
2014-11-14 14:38:06 +00:00
|
|
|
from . import settings
|
2014-11-11 16:59:13 +00:00
|
|
|
from .actions import grant_permission
|
2014-10-23 15:38:46 +00:00
|
|
|
from .filters import IsMainListFilter
|
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 14:38:06 +00:00
|
|
|
list_display = ('username', 'account_link', 'shell', 'display_home', 'display_active', 'display_main')
|
2014-10-23 15:38:46 +00:00
|
|
|
list_filter = ('is_active', '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
|
|
|
}),
|
|
|
|
)
|
|
|
|
search_fields = ['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',)
|
|
|
|
add_form = UserCreationForm
|
|
|
|
form = UserChangeForm
|
|
|
|
ordering = ('-id',)
|
2014-11-11 16:59:13 +00:00
|
|
|
actions = (grant_permission,)
|
|
|
|
change_view_actions = actions
|
2014-09-30 09:49:07 +00:00
|
|
|
|
2014-10-23 15:38:46 +00:00
|
|
|
def display_active(self, user):
|
|
|
|
return user.active
|
|
|
|
display_active.short_description = _("Active")
|
|
|
|
display_active.admin_order_field = 'is_active'
|
|
|
|
display_active.boolean = True
|
|
|
|
|
|
|
|
def display_main(self, user):
|
|
|
|
return user.is_main
|
|
|
|
display_main.short_description = _("Main")
|
|
|
|
display_main.boolean = True
|
|
|
|
|
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'
|
|
|
|
|
2014-09-30 09:49:07 +00:00
|
|
|
def get_form(self, request, obj=None, **kwargs):
|
|
|
|
form = super(SystemUserAdmin, self).get_form(request, obj=obj, **kwargs)
|
2014-11-14 14:38:06 +00:00
|
|
|
duplicate = lambda n: (n, n)
|
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-11-14 14:38:06 +00:00
|
|
|
username = obj.username
|
|
|
|
choices=(
|
|
|
|
duplicate(self.account.main_systemuser.get_home()),
|
|
|
|
duplicate(obj.get_home()),
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
username = '<username>'
|
|
|
|
choices=(
|
|
|
|
duplicate(self.account.main_systemuser.get_home()),
|
|
|
|
duplicate(SystemUser(username=username).get_home()),
|
|
|
|
)
|
|
|
|
form.base_fields['home'].widget = forms.Select(choices=choices)
|
|
|
|
if obj and (obj.is_main or obj.has_shell):
|
|
|
|
# hidde home option for shell users
|
|
|
|
form.base_fields['home'].widget = forms.HiddenInput()
|
|
|
|
form.base_fields['directory'].widget = forms.HiddenInput()
|
|
|
|
else:
|
|
|
|
# Some javascript for hidde home/directory inputs when convinient
|
|
|
|
form.base_fields['shell'].widget.attrs = {
|
|
|
|
'onChange': textwrap.dedent("""\
|
|
|
|
field = $(".form-row.field-home.field-directory");
|
|
|
|
if ($.inArray(this.value, %s) < 0) {
|
|
|
|
field.addClass("hidden");
|
|
|
|
} else {
|
|
|
|
field.removeClass("hidden");
|
|
|
|
};""" % str(list(settings.SYSTEMUSERS_DISABLED_SHELLS)))
|
|
|
|
}
|
|
|
|
form.base_fields['home'].widget.attrs = {
|
|
|
|
'onChange': textwrap.dedent("""\
|
|
|
|
field = $(".field-box.field-directory");
|
|
|
|
if (this.value.search("%s") > 0) {
|
|
|
|
field.addClass("hidden");
|
|
|
|
} else {
|
|
|
|
field.removeClass("hidden");
|
|
|
|
};""" % username)
|
|
|
|
}
|
2014-09-30 09:49:07 +00:00
|
|
|
return form
|
2014-10-23 15:38:46 +00:00
|
|
|
|
|
|
|
def has_delete_permission(self, request, obj=None):
|
|
|
|
if obj and obj.is_main:
|
|
|
|
return False
|
|
|
|
return super(SystemUserAdmin, self).has_delete_permission(request, obj=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)
|