2014-05-25 23:35:14 +02:00
|
|
|
from django import forms
|
|
|
|
from django.utils.translation import ugettext as _
|
2014-05-24 21:28:33 +02:00
|
|
|
from django.contrib import admin
|
|
|
|
|
2014-05-24 21:53:49 +02:00
|
|
|
from .models import (
|
|
|
|
AdditionalGroup,
|
2014-05-30 21:46:10 +02:00
|
|
|
DeleteTaskResult,
|
2014-05-24 21:53:49 +02:00
|
|
|
Group,
|
2014-05-30 21:46:10 +02:00
|
|
|
GroupTaskResult,
|
2014-05-24 21:53:49 +02:00
|
|
|
Shadow,
|
|
|
|
User,
|
2014-05-30 21:46:10 +02:00
|
|
|
UserTaskResult,
|
2014-05-24 21:53:49 +02:00
|
|
|
)
|
|
|
|
|
2014-05-25 23:35:14 +02:00
|
|
|
PASSWORD_MISMATCH_ERROR = _("Passwords don't match")
|
|
|
|
|
2014-05-24 21:53:49 +02:00
|
|
|
|
|
|
|
class AdditionalGroupInline(admin.TabularInline):
|
|
|
|
model = AdditionalGroup
|
|
|
|
|
|
|
|
|
|
|
|
class ShadowInline(admin.TabularInline):
|
|
|
|
model = Shadow
|
2014-05-24 23:15:14 +02:00
|
|
|
readonly_fields = ['passwd']
|
|
|
|
can_delete = False
|
2014-05-24 21:53:49 +02:00
|
|
|
|
|
|
|
|
2014-05-25 23:35:14 +02:00
|
|
|
class UserCreationForm(forms.ModelForm):
|
|
|
|
"""
|
|
|
|
A form for creating system users.
|
|
|
|
|
|
|
|
"""
|
|
|
|
password1 = forms.CharField(label=_('Password'),
|
|
|
|
widget=forms.PasswordInput)
|
|
|
|
password2 = forms.CharField(label=_('Password (again)'),
|
|
|
|
widget=forms.PasswordInput)
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = User
|
|
|
|
fields = []
|
|
|
|
|
|
|
|
def clean_password2(self):
|
|
|
|
"""
|
|
|
|
Check that the two password entries match.
|
|
|
|
|
|
|
|
"""
|
|
|
|
password1 = self.cleaned_data.get('password1')
|
|
|
|
password2 = self.cleaned_data.get('password2')
|
|
|
|
if password1 and password2 and password1 != password2:
|
|
|
|
raise forms.ValidationError(PASSWORD_MISMATCH_ERROR)
|
|
|
|
return password2
|
|
|
|
|
|
|
|
def save(self, commit=True):
|
|
|
|
"""
|
|
|
|
Save the provided password in hashed format.
|
|
|
|
|
|
|
|
"""
|
|
|
|
user = User.objects.create_user(
|
|
|
|
password=self.cleaned_data['password1'], commit=commit)
|
|
|
|
return user
|
|
|
|
|
|
|
|
def save_m2m(self):
|
2014-06-01 22:18:16 +02:00
|
|
|
"""
|
|
|
|
No additional groups are created when this form is saved, so this
|
|
|
|
method just does nothing.
|
|
|
|
"""
|
2014-05-25 23:35:14 +02:00
|
|
|
|
|
|
|
|
2014-05-24 21:53:49 +02:00
|
|
|
class UserAdmin(admin.ModelAdmin):
|
2014-12-22 20:07:11 +01:00
|
|
|
actions = ['perform_delete_selected']
|
2014-05-24 23:15:14 +02:00
|
|
|
readonly_fields = ['uid']
|
2014-05-25 23:35:14 +02:00
|
|
|
add_form = UserCreationForm
|
2014-12-22 20:07:11 +01:00
|
|
|
inlines = [AdditionalGroupInline, ShadowInline]
|
2014-05-25 23:35:14 +02:00
|
|
|
|
|
|
|
add_fieldsets = (
|
|
|
|
(None, {
|
|
|
|
'classes': ('wide',),
|
|
|
|
'fields': ('password1', 'password2')}),
|
|
|
|
)
|
|
|
|
|
|
|
|
def get_form(self, request, obj=None, **kwargs):
|
|
|
|
"""
|
|
|
|
Use special form during user creation.
|
|
|
|
|
|
|
|
"""
|
|
|
|
defaults = {}
|
|
|
|
if obj is None:
|
|
|
|
defaults.update({
|
|
|
|
'form': self.add_form,
|
2014-12-17 21:22:37 +01:00
|
|
|
'fields': admin.options.flatten_fieldsets(self.add_fieldsets),
|
2014-05-25 23:35:14 +02:00
|
|
|
})
|
|
|
|
defaults.update(kwargs)
|
|
|
|
return super(UserAdmin, self).get_form(request, obj, **defaults)
|
|
|
|
|
2014-12-22 20:07:11 +01:00
|
|
|
def get_readonly_fields(self, request, obj=None):
|
|
|
|
if obj:
|
|
|
|
return ['uid']
|
|
|
|
return []
|
|
|
|
|
|
|
|
def perform_delete_selected(self, request, queryset):
|
|
|
|
for user in queryset.all():
|
|
|
|
user.delete()
|
|
|
|
perform_delete_selected.short_description = _('Delete selected users')
|
|
|
|
|
|
|
|
def get_actions(self, request):
|
|
|
|
actions = super(UserAdmin, self).get_actions(request)
|
|
|
|
if 'delete_selected' in actions:
|
|
|
|
del actions['delete_selected']
|
|
|
|
return actions
|
2014-05-24 21:53:49 +02:00
|
|
|
|
|
|
|
|
2014-05-30 21:46:10 +02:00
|
|
|
class GroupAdmin(admin.ModelAdmin):
|
2014-12-22 20:07:11 +01:00
|
|
|
actions = ['perform_delete_selected']
|
2014-05-30 21:46:10 +02:00
|
|
|
|
|
|
|
def get_inline_instances(self, request, obj=None):
|
|
|
|
if obj is None:
|
|
|
|
return []
|
|
|
|
return super(GroupAdmin, self).get_inline_instances(request, obj)
|
|
|
|
|
2014-12-22 20:07:11 +01:00
|
|
|
def perform_delete_selected(self, request, queryset):
|
|
|
|
for group in queryset.all():
|
|
|
|
group.delete()
|
|
|
|
perform_delete_selected.short_description = _('Delete selected groups')
|
|
|
|
|
|
|
|
def get_actions(self, request):
|
|
|
|
actions = super(GroupAdmin, self).get_actions(request)
|
|
|
|
if 'delete_selected' in actions:
|
|
|
|
del actions['delete_selected']
|
|
|
|
return actions
|
2014-05-30 21:46:10 +02:00
|
|
|
|
|
|
|
|
2014-12-22 20:07:11 +01:00
|
|
|
class TaskResultAdmin(admin.ModelAdmin):
|
2014-05-30 21:46:10 +02:00
|
|
|
def has_add_permission(self, request, obj=None):
|
|
|
|
return False
|
|
|
|
|
2014-12-22 20:07:11 +01:00
|
|
|
def has_delete_permission(self, request, obj=None):
|
|
|
|
return obj is None or obj.is_finished
|
|
|
|
|
2014-05-30 21:46:10 +02:00
|
|
|
def get_queryset(self, request):
|
2014-12-22 20:07:11 +01:00
|
|
|
qs = super(TaskResultAdmin, self).get_queryset(request)
|
2014-05-30 21:46:10 +02:00
|
|
|
for entry in qs:
|
|
|
|
entry.update_taskstatus()
|
|
|
|
return qs
|
|
|
|
|
|
|
|
|
2014-12-22 20:07:11 +01:00
|
|
|
class DeleteTaskResultAdmin(TaskResultAdmin):
|
|
|
|
readonly_fields = ['task_uuid', 'task_name', 'modeltype', 'modelname',
|
|
|
|
'is_finished', 'is_success', 'state', 'result_body']
|
|
|
|
list_display = ('task_uuid', 'task_name', 'modeltype', 'modelname',
|
|
|
|
'is_finished', 'state')
|
|
|
|
|
|
|
|
|
|
|
|
class GroupTaskResultAdmin(TaskResultAdmin):
|
|
|
|
readonly_fields = [
|
|
|
|
'task_uuid', 'task_name', 'group', 'is_finished', 'is_success',
|
|
|
|
'state', 'result_body'
|
|
|
|
]
|
|
|
|
list_display = ('task_uuid', 'task_name', 'group', 'is_finished', 'state')
|
|
|
|
|
|
|
|
|
|
|
|
class UserTaskResultAdmin(TaskResultAdmin):
|
|
|
|
readonly_fields = [
|
|
|
|
'task_uuid', 'task_name', 'user', 'is_finished', 'is_success', 'state',
|
|
|
|
'result_body'
|
|
|
|
]
|
|
|
|
list_display = ('task_uuid', 'task_name', 'user', 'is_finished', 'state')
|
|
|
|
|
|
|
|
|
2014-05-30 21:46:10 +02:00
|
|
|
admin.site.register(Group, GroupAdmin)
|
2014-05-24 21:53:49 +02:00
|
|
|
admin.site.register(User, UserAdmin)
|
2014-05-30 21:46:10 +02:00
|
|
|
admin.site.register(DeleteTaskResult, DeleteTaskResultAdmin)
|
2014-12-22 20:07:11 +01:00
|
|
|
admin.site.register(GroupTaskResult, GroupTaskResultAdmin)
|
|
|
|
admin.site.register(UserTaskResult, UserTaskResultAdmin)
|