django-orchestra/orchestra/apps/orchestration/admin.py

124 lines
3.9 KiB
Python
Raw Normal View History

2014-05-08 16:59:35 +00:00
from django.contrib import admin
from django.utils.html import escape
from django.utils.translation import ugettext_lazy as _
from orchestra.admin.html import monospace_format
2014-07-24 09:53:34 +00:00
from orchestra.admin.utils import admin_link, admin_date, admin_colored
2014-05-08 16:59:35 +00:00
from .models import Server, Route, BackendLog, BackendOperation
STATE_COLORS = {
BackendLog.RECEIVED: 'darkorange',
BackendLog.TIMEOUT: 'red',
BackendLog.STARTED: 'blue',
BackendLog.SUCCESS: 'green',
BackendLog.FAILURE: 'red',
BackendLog.ERROR: 'red',
BackendLog.REVOKED: 'magenta',
}
class RouteAdmin(admin.ModelAdmin):
list_display = [
2014-07-09 16:17:43 +00:00
'id', 'backend', 'host', 'match', 'display_model', 'display_actions',
'is_active'
2014-05-08 16:59:35 +00:00
]
list_editable = ['backend', 'host', 'match', 'is_active']
2014-07-09 16:17:43 +00:00
list_filter = ['host', 'is_active', 'backend']
2014-05-08 16:59:35 +00:00
def display_model(self, route):
try:
2014-07-14 14:56:48 +00:00
return escape(route.backend_class().model)
2014-05-08 16:59:35 +00:00
except KeyError:
return "<span style='color: red;'>NOT AVAILABLE</span>"
display_model.short_description = _("model")
display_model.allow_tags = True
2014-07-09 16:17:43 +00:00
def display_actions(self, route):
try:
2014-07-10 10:03:22 +00:00
return '<br>'.join(route.backend_class().get_actions())
2014-07-09 16:17:43 +00:00
except KeyError:
return "<span style='color: red;'>NOT AVAILABLE</span>"
display_actions.short_description = _("actions")
display_actions.allow_tags = True
2014-05-08 16:59:35 +00:00
class BackendOperationInline(admin.TabularInline):
model = BackendOperation
2014-10-09 17:04:12 +00:00
fields = ('action', 'content_object_link')
readonly_fields = ('action', 'content_object_link')
2014-05-08 16:59:35 +00:00
extra = 0
can_delete = False
class Media:
css = {
'all': ('orchestra/css/hide-inline-id.css',)
}
2014-10-09 17:04:12 +00:00
def content_object_link(self, operation):
2014-05-08 16:59:35 +00:00
try:
2014-10-09 17:04:12 +00:00
return admin_link('content_object')(self, operation)
2014-05-08 16:59:35 +00:00
except:
2014-07-14 14:56:48 +00:00
return _("deleted {0} {1}").format(
escape(operation.content_type), escape(operation.object_id)
)
2014-10-09 17:04:12 +00:00
content_object_link.allow_tags = True
content_object_link.short_description = _("Content_object")
2014-05-08 16:59:35 +00:00
def has_add_permission(self, *args, **kwargs):
return False
2014-09-10 16:53:09 +00:00
def get_queryset(self, request):
queryset = super(BackendOperationInline, self).get_queryset(request)
2014-10-09 17:04:12 +00:00
return queryset.prefetch_related('content_object')
2014-05-08 16:59:35 +00:00
def display_mono(field):
def display(self, log):
return monospace_format(escape(getattr(log, field)))
display.short_description = _(field)
return display
2014-05-08 16:59:35 +00:00
class BackendLogAdmin(admin.ModelAdmin):
list_display = (
2014-07-14 14:56:48 +00:00
'id', 'backend', 'server_link', 'display_state', 'exit_code',
'display_created', 'execution_time',
2014-05-08 16:59:35 +00:00
)
list_display_links = ('id', 'backend')
list_filter = ('state', 'backend')
inlines = [BackendOperationInline]
fields = [
2014-07-25 15:17:50 +00:00
'backend', 'server_link', 'state', 'mono_script', 'mono_stdout',
'mono_stderr', 'mono_traceback', 'exit_code', 'task_id', 'display_created',
'execution_time'
2014-05-08 16:59:35 +00:00
]
2014-07-14 14:56:48 +00:00
readonly_fields = fields
2014-05-08 16:59:35 +00:00
2014-07-21 15:43:36 +00:00
server_link = admin_link('server')
display_created = admin_date('created_at', short_description=_("Created"))
2014-07-24 09:53:34 +00:00
display_state = admin_colored('state', colors=STATE_COLORS)
mono_script = display_mono('script')
mono_stdout = display_mono('stdout')
mono_stderr = display_mono('stderr')
mono_traceback = display_mono('traceback')
2014-05-08 16:59:35 +00:00
2014-07-08 15:19:15 +00:00
def get_queryset(self, request):
2014-05-08 16:59:35 +00:00
""" Order by structured name and imporve performance """
2014-07-08 15:19:15 +00:00
qs = super(BackendLogAdmin, self).get_queryset(request)
2014-09-10 16:53:09 +00:00
return qs.select_related('server').defer('script', 'stdout')
2014-09-26 15:05:20 +00:00
def has_add_permission(self, *args, **kwargs):
return False
2014-05-08 16:59:35 +00:00
class ServerAdmin(admin.ModelAdmin):
list_display = ('name', 'address', 'os')
list_filter = ('os',)
admin.site.register(Server, ServerAdmin)
admin.site.register(BackendLog, BackendLogAdmin)
admin.site.register(Route, RouteAdmin)