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

136 lines
5.0 KiB
Python
Raw Normal View History

2014-07-10 15:19:06 +00:00
from django.contrib import admin, messages
2014-07-08 15:19:15 +00:00
from django.contrib.contenttypes import generic
from django.utils.functional import cached_property
from django.utils.translation import ugettext_lazy as _
2014-07-10 15:19:06 +00:00
from orchestra.admin import ExtendedModelAdmin
2014-07-08 15:19:15 +00:00
from orchestra.admin.filters import UsedContentTypeFilter
2014-07-22 21:47:01 +00:00
from orchestra.admin.utils import insertattr, get_modeladmin, admin_link, admin_date
2014-07-10 10:03:22 +00:00
from orchestra.core import services
2014-07-09 16:17:43 +00:00
from orchestra.utils import running_syncdb
2014-07-08 15:19:15 +00:00
from .forms import ResourceForm
2014-07-09 16:17:43 +00:00
from .models import Resource, ResourceData, MonitorData
2014-07-08 15:19:15 +00:00
2014-07-10 15:19:06 +00:00
class ResourceAdmin(ExtendedModelAdmin):
2014-07-08 15:19:15 +00:00
list_display = (
2014-07-24 09:53:34 +00:00
'id', 'verbose_name', 'content_type', 'period', 'ondemand',
'default_allocation', 'unit', 'disable_trigger', 'crontab',
2014-07-08 15:19:15 +00:00
)
list_filter = (UsedContentTypeFilter, 'period', 'ondemand', 'disable_trigger')
2014-07-10 15:19:06 +00:00
fieldsets = (
(None, {
'fields': ('name', 'content_type', 'period'),
}),
(_("Configuration"), {
2014-07-16 15:20:16 +00:00
'fields': ('verbose_name', 'unit', 'scale', 'ondemand',
'default_allocation', 'disable_trigger', 'is_active'),
2014-07-10 15:19:06 +00:00
}),
(_("Monitoring"), {
'fields': ('monitors', 'crontab'),
}),
)
change_readonly_fields = ('name', 'content_type', 'period')
def add_view(self, request, **kwargs):
""" Warning user if the node is not fully configured """
2014-07-17 16:09:24 +00:00
if request.method == 'POST':
2014-07-10 15:19:06 +00:00
messages.warning(request, _(
2014-07-17 16:09:24 +00:00
"Restarting orchestra and celerybeat is required to fully apply changes. "
"Remember that new allocated values will be applied when objects are saved."
2014-07-10 15:19:06 +00:00
))
return super(ResourceAdmin, self).add_view(request, **kwargs)
2014-07-10 10:03:22 +00:00
2014-07-08 15:19:15 +00:00
def save_model(self, request, obj, form, change):
super(ResourceAdmin, self).save_model(request, obj, form, change)
model = obj.content_type.model_class()
2014-07-18 15:32:27 +00:00
modeladmin = type(get_modeladmin(model))
2014-07-08 15:19:15 +00:00
resources = obj.content_type.resource_set.filter(is_active=True)
inlines = []
for inline in modeladmin.inlines:
2014-07-09 16:17:43 +00:00
if inline.model is ResourceData:
2014-07-08 15:19:15 +00:00
inline = resource_inline_factory(resources)
inlines.append(inline)
modeladmin.inlines = inlines
2014-07-10 10:03:22 +00:00
def formfield_for_dbfield(self, db_field, **kwargs):
""" filter service content_types """
if db_field.name == 'content_type':
2014-07-21 12:20:04 +00:00
models = [ model._meta.model_name for model in services.get() ]
2014-07-10 10:03:22 +00:00
kwargs['queryset'] = db_field.rel.to.objects.filter(model__in=models)
return super(ResourceAdmin, self).formfield_for_dbfield(db_field, **kwargs)
2014-07-08 15:19:15 +00:00
2014-07-09 16:17:43 +00:00
class ResourceDataAdmin(admin.ModelAdmin):
2014-07-16 15:20:16 +00:00
list_display = (
'id', 'resource', 'used', 'allocated', 'last_update', 'content_object_link'
)
2014-07-08 15:19:15 +00:00
list_filter = ('resource',)
2014-07-16 15:20:16 +00:00
readonly_fields = ('content_object_link',)
2014-07-21 15:43:36 +00:00
content_object_link = admin_link('content_object')
2014-07-08 15:19:15 +00:00
class MonitorDataAdmin(admin.ModelAdmin):
2014-07-16 15:20:16 +00:00
list_display = ('id', 'monitor', 'date', 'value', 'content_object_link')
2014-07-08 15:19:15 +00:00
list_filter = ('monitor',)
2014-07-16 15:20:16 +00:00
readonly_fields = ('content_object_link',)
2014-07-11 21:09:17 +00:00
2014-07-21 15:43:36 +00:00
content_object_link = admin_link('content_object')
2014-07-08 15:19:15 +00:00
admin.site.register(Resource, ResourceAdmin)
2014-07-09 16:17:43 +00:00
admin.site.register(ResourceData, ResourceDataAdmin)
2014-07-08 15:19:15 +00:00
admin.site.register(MonitorData, MonitorDataAdmin)
# Mokey-patching
def resource_inline_factory(resources):
class ResourceInlineFormSet(generic.BaseGenericInlineFormSet):
2014-07-22 21:47:01 +00:00
def total_form_count(self, resources=resources):
2014-07-08 15:19:15 +00:00
return len(resources)
@cached_property
2014-07-22 21:47:01 +00:00
def forms(self, resources=resources):
2014-07-08 15:19:15 +00:00
forms = []
2014-07-22 21:47:01 +00:00
resources_copy = list(resources)
for i, data in enumerate(self.queryset):
forms.append(self._construct_form(i, resource=data.resource))
resources_copy.remove(data.resource)
for i, resource in enumerate(resources_copy, len(self.queryset)):
2014-07-08 15:19:15 +00:00
forms.append(self._construct_form(i, resource=resource))
return forms
class ResourceInline(generic.GenericTabularInline):
2014-07-09 16:17:43 +00:00
model = ResourceData
2014-07-08 15:19:15 +00:00
verbose_name_plural = _("resources")
form = ResourceForm
formset = ResourceInlineFormSet
2014-07-10 17:34:23 +00:00
can_delete = False
2014-07-16 15:20:16 +00:00
fields = (
'verbose_name', 'used', 'display_last_update', 'allocated', 'unit'
)
readonly_fields = ('used', 'display_last_update')
2014-07-08 15:19:15 +00:00
class Media:
css = {
'all': ('orchestra/css/hide-inline-id.css',)
}
2014-07-22 21:47:01 +00:00
display_last_update = admin_date('last_update', default=_("Never"))
2014-07-08 15:19:15 +00:00
def has_add_permission(self, *args, **kwargs):
""" Hidde add another """
return False
return ResourceInline
2014-07-09 16:17:43 +00:00
if not running_syncdb():
2014-07-08 15:19:15 +00:00
# not run during syncdb
for resources in Resource.group_by_content_type():
inline = resource_inline_factory(resources)
model = resources[0].content_type.model_class()
insertattr(model, 'inlines', inline)