# Copyright (c) 2013 AnsibleWorks, Inc. # All Rights Reserved. import json import urllib from django.conf.urls import * from django.contrib import admin from django.contrib.admin.util import unquote from django.contrib import messages from django.core.urlresolvers import reverse from django.http import HttpResponseRedirect from django.utils.timezone import now from django.utils.translation import ugettext_lazy as _ from django.utils.html import format_html from ansibleworks.main.models import * from ansibleworks.main.forms import * from django.contrib.auth.models import User from django.contrib.auth.admin import UserAdmin class UserAdmin(UserAdmin): fieldsets = ( (None, {'fields': ('username', 'password')}), (_('Personal info'), {'fields': ('first_name', 'last_name', 'email')}), (_('Permissions'), {'fields': ('is_active', 'is_staff', 'is_superuser')}), (_('Important dates'), {'fields': ('last_login', 'date_joined')}), ) readonly_fields = ('last_login', 'date_joined') list_display = ('username', 'email', 'first_name', 'last_name', 'is_staff') list_filter = ('is_staff', 'is_superuser', 'is_active', 'groups') search_fields = ('username', 'first_name', 'last_name', 'email') ordering = ('username',) try: admin.site.unregister(User) except admin.site.NotRegistered: pass admin.site.register(User, UserAdmin) # FIXME: Hide auth.Group admin class BaseModelAdmin(admin.ModelAdmin): def save_model(self, request, obj, form, change): # Automatically set created_by when saved from the admin. # FIXME: Doesn't handle inline model instances yet. if hasattr(obj, 'created_by') and obj.created_by is None: obj.created_by = request.user return super(BaseModelAdmin, self).save_model(request, obj, form, change) class OrganizationAdmin(BaseModelAdmin): list_display = ('name', 'description', 'active') list_filter = ('active', 'tags') fieldsets = ( (None, {'fields': (('name', 'active'), 'description',)}), (_('Members'), {'fields': ('users', 'admins',)}), (_('Projects'), {'fields': ('projects',)}), (_('Tags'), {'fields': ('tags',)}), (_('Audit'), {'fields': ('created', 'created_by',)}), ) readonly_fields = ('created', 'created_by') filter_horizontal = ('users', 'admins', 'projects') class InventoryHostInline(admin.StackedInline): model = Host extra = 0 fields = ('name', 'description', 'active', 'tags') class InventoryGroupInline(admin.StackedInline): model = Group extra = 0 fields = ('name', 'description', 'active', 'parents', 'hosts', 'tags') filter_horizontal = ('parents', 'hosts') class InventoryAdmin(BaseModelAdmin): list_display = ('name', 'organization', 'description', 'active') list_filter = ('organization', 'active') fieldsets = ( (None, {'fields': (('name', 'active'), 'organization', 'description',)}), (_('Tags'), {'fields': ('tags',)}), (_('Audit'), {'fields': ('created', 'created_by',)}), ) readonly_fields = ('created', 'created_by') inlines = [InventoryHostInline, InventoryGroupInline] #class TagAdmin(BaseModelAdmin): # # list_display = ('name',) #class AuditTrailAdmin(admin.ModelAdmin): # # list_display = ('name', 'description', 'active') # not currently on model, so disabling for now. # filter_horizontal = ('tags',) class JobHostSummaryInline(admin.TabularInline): model = JobHostSummary extra = 0 can_delete = False def has_add_permission(self, request): return False class JobEventInline(admin.StackedInline): model = JobEvent extra = 0 can_delete = False def has_add_permission(self, request): return False def get_event_data_display(self, obj): return format_html('
{0}', json.dumps(obj.event_data, indent=4)) get_event_data_display.short_description = _('Event data') get_event_data_display.allow_tags = True class JobHostSummaryInlineForHost(JobHostSummaryInline): fields = ('job', 'changed', 'dark', 'failures', 'ok', 'processed', 'skipped', 'failed') readonly_fields = ('job', 'changed', 'dark', 'failures', 'ok', 'processed', 'skipped', 'failed') class JobEventInlineForHost(JobEventInline): fields = ('job', 'created', 'event', 'get_event_data_display') readonly_fields = ('job', 'created', 'event', 'get_event_data_display') class HostAdmin(BaseModelAdmin): list_display = ('name', 'inventory', 'description', 'active') list_filter = ('inventory', 'active') fieldsets = ( (None, {'fields': (('name', 'active'), 'inventory', 'description', 'variables', )}), (_('Tags'), {'fields': ('tags',)}), (_('Audit'), {'fields': ('created', 'created_by',)}), ) readonly_fields = ('created', 'created_by') # FIXME: Edit reverse of many to many for groups. inlines = [JobHostSummaryInlineForHost, JobEventInlineForHost] class GroupAdmin(BaseModelAdmin): list_display = ('name', 'description', 'active') fieldsets = ( (None, {'fields': (('name', 'active'), 'inventory', 'description', 'parents', 'variables')}), (_('Tags'), {'fields': ('tags',)}), (_('Audit'), {'fields': ('created', 'created_by',)}), ) readonly_fields = ('created', 'created_by') filter_horizontal = ('parents', 'hosts') class CredentialAdmin(BaseModelAdmin): fieldsets = ( (None, {'fields': (('name', 'active'), ('user', 'team'), 'description')}), (_('Auth Info'), {'fields': (('ssh_username', 'ssh_password'), 'ssh_key_data', 'ssh_key_unlock', ('sudo_username', 'sudo_password'))}), (_('Tags'), {'fields': ('tags',)}), (_('Audit'), {'fields': ('created', 'created_by',)}), ) readonly_fields = ('created', 'created_by') class TeamAdmin(BaseModelAdmin): list_display = ('name', 'description', 'active') filter_horizontal = ('projects', 'users') class ProjectAdmin(BaseModelAdmin): list_display = ('name', 'description', 'active') fieldsets = ( (None, {'fields': (('name', 'active'), 'description', 'local_path', 'get_playbooks_display')}), (_('Tags'), {'fields': ('tags',)}), (_('Audit'), {'fields': ('created', 'created_by',)}), ) readonly_fields = ('created', 'created_by', 'get_playbooks_display') form = ProjectAdminForm def get_playbooks_display(self, obj): return '
{0}', obj.result_stdout or ' ') get_result_stdout_display.short_description = _('Stdout') get_result_stdout_display.allow_tags = True def get_result_traceback_display(self, obj): return format_html('
{0}', obj.result_traceback or ' ') get_result_traceback_display.short_description = _('Traceback') get_result_traceback_display.allow_tags = True admin.site.register(Organization, OrganizationAdmin) admin.site.register(Inventory, InventoryAdmin) #admin.site.register(Tag, TagAdmin) #admin.site.register(AuditTrail, AuditTrailAdmin) admin.site.register(Host, HostAdmin) admin.site.register(Group, GroupAdmin) #admin.site.register(VariableData, VariableDataAdmin) admin.site.register(Team, TeamAdmin) admin.site.register(Project, ProjectAdmin) admin.site.register(Credential, CredentialAdmin) admin.site.register(JobTemplate, JobTemplateAdmin) admin.site.register(Job, JobAdmin)