django-orchestra-test/orchestra/contrib/miscellaneous/admin.py

140 lines
5.4 KiB
Python
Raw Normal View History

2014-11-13 15:34:00 +00:00
from django import forms
2014-07-16 15:20:16 +00:00
from django.contrib import admin
from django.core.urlresolvers import reverse
from django.db import models
from django.utils.safestring import mark_safe
from django.utils.translation import ugettext_lazy as _
2014-10-24 14:19:34 +00:00
from orchestra.admin import ExtendedModelAdmin
2016-03-31 16:18:38 +00:00
from orchestra.admin.actions import disable, enable
2014-11-13 15:34:00 +00:00
from orchestra.admin.utils import admin_link
2015-04-05 10:46:24 +00:00
from orchestra.contrib.accounts.admin import AccountAdminMixin
2016-03-14 10:40:11 +00:00
from orchestra.contrib.accounts.filters import IsActiveListFilter
from orchestra.plugins import PluginModelAdapter
from orchestra.plugins.admin import SelectPluginAdminMixin
2015-04-27 14:54:17 +00:00
from orchestra.utils.python import import_class
2014-07-16 15:20:16 +00:00
2014-11-13 15:34:00 +00:00
from . import settings
2014-07-16 15:20:16 +00:00
from .models import MiscService, Miscellaneous
2014-11-13 15:34:00 +00:00
class MiscServicePlugin(PluginModelAdapter):
2014-11-12 16:33:40 +00:00
model = MiscService
name_field = 'name'
2016-05-07 10:32:51 +00:00
plugin_field = 'service'
2014-11-12 16:33:40 +00:00
2014-10-24 14:19:34 +00:00
class MiscServiceAdmin(ExtendedModelAdmin):
2014-11-13 15:34:00 +00:00
list_display = (
'name', 'verbose_name', 'num_instances', 'has_identifier', 'has_amount', 'is_active'
)
list_editable = ('is_active',)
2016-03-14 10:40:11 +00:00
list_filter = ('has_identifier', 'has_amount', IsActiveListFilter)
fields = (
'verbose_name', 'name', 'description', 'has_identifier', 'has_amount', 'is_active'
)
2014-10-24 14:19:34 +00:00
prepopulated_fields = {'name': ('verbose_name',)}
change_readonly_fields = ('name',)
2016-03-31 16:18:38 +00:00
actions = (disable, enable)
2014-07-16 15:20:16 +00:00
def num_instances(self, misc):
""" return num slivers as a link to slivers changelist view """
2014-07-22 21:47:01 +00:00
num = misc.instances__count
2014-07-16 15:20:16 +00:00
url = reverse('admin:miscellaneous_miscellaneous_changelist')
2016-02-09 12:17:42 +00:00
url += '?service__name={}'.format(misc.name)
2014-07-16 15:20:16 +00:00
return mark_safe('<a href="{0}">{1}</a>'.format(url, num))
num_instances.short_description = _("Instances")
num_instances.admin_order_field = 'instances__count'
def get_queryset(self, request):
2015-04-07 15:14:49 +00:00
qs = super(MiscServiceAdmin, self).get_queryset(request)
2014-07-16 15:20:16 +00:00
return qs.annotate(models.Count('instances', distinct=True))
2014-11-13 15:34:00 +00:00
def formfield_for_dbfield(self, db_field, **kwargs):
""" Make value input widget bigger """
if db_field.name == 'description':
kwargs['widget'] = forms.Textarea(attrs={'cols': 70, 'rows': 2})
return super(MiscServiceAdmin, self).formfield_for_dbfield(db_field, **kwargs)
2014-07-16 15:20:16 +00:00
2016-05-07 10:32:51 +00:00
class MiscellaneousAdmin(SelectPluginAdminMixin, AccountAdminMixin, ExtendedModelAdmin):
list_display = (
2016-03-14 10:40:11 +00:00
'__str__', 'service_link', 'amount', 'account_link', 'dispaly_active'
)
2014-11-13 15:34:00 +00:00
list_filter = ('service__name', 'is_active')
list_select_related = ('service', 'account')
2016-05-07 10:32:51 +00:00
readonly_fields = ('account_link', 'service_link')
add_fields = ('service', 'account', 'description', 'is_active')
fields = ('service_link', 'account', 'description', 'is_active')
change_readonly_fields = ('identifier', 'service')
2015-04-24 11:39:20 +00:00
search_fields = ('identifier', 'description', 'account__username')
2016-03-31 16:18:38 +00:00
actions = (disable, enable)
2014-11-12 16:33:40 +00:00
plugin_field = 'service'
plugin = MiscServicePlugin
2014-07-18 15:32:27 +00:00
2014-11-13 15:34:00 +00:00
service_link = admin_link('service')
def dispaly_active(self, instance):
return instance.active
dispaly_active.short_description = _("Active")
dispaly_active.boolean = True
dispaly_active.admin_order_field = 'is_active'
2014-11-12 16:33:40 +00:00
def get_service(self, obj):
2014-07-18 15:32:27 +00:00
if obj is None:
return self.plugin.get(self.plugin_value).related_instance
2014-11-12 16:33:40 +00:00
else:
return obj.service
2016-05-07 20:09:05 +00:00
def get_fieldsets(self, request, obj=None):
fieldsets = super().get_fieldsets(request, obj)
fields = list(fieldsets[0][1]['fields'])
2014-11-12 16:33:40 +00:00
service = self.get_service(obj)
2016-05-07 10:32:51 +00:00
if obj:
fields.insert(1, 'account_link')
2014-11-12 16:33:40 +00:00
if service.has_amount:
fields.insert(-1, 'amount')
2014-11-13 15:34:00 +00:00
if service.has_identifier:
2016-05-07 10:32:51 +00:00
fields.insert(2, 'identifier')
2016-05-07 20:09:05 +00:00
fieldsets[0][1]['fields'] = fields
return fieldsets
2014-11-12 16:33:40 +00:00
def get_form(self, request, obj=None, **kwargs):
2016-05-07 10:32:51 +00:00
if obj:
plugin = self.plugin.get(obj.service.name)()
else:
plugin = self.plugin.get(self.plugin_value)()
self.form = plugin.get_form()
self.plugin_instance = plugin
2014-11-12 16:33:40 +00:00
service = self.get_service(obj)
2016-05-07 10:32:51 +00:00
form = super(SelectPluginAdminMixin, self).get_form(request, obj, **kwargs)
2014-11-12 16:33:40 +00:00
def clean_identifier(self, service=service):
2014-11-13 15:34:00 +00:00
identifier = self.cleaned_data['identifier']
2015-04-27 14:54:17 +00:00
validator_path = settings.MISCELLANEOUS_IDENTIFIER_VALIDATORS.get(service.name, None)
if validator_path:
validator = import_class(validator_path)
2014-11-13 15:34:00 +00:00
validator(identifier)
return identifier
2014-11-12 16:33:40 +00:00
form.clean_identifier = clean_identifier
return form
2014-11-13 15:34:00 +00:00
def formfield_for_dbfield(self, db_field, **kwargs):
""" Make value input widget bigger """
if db_field.name == 'description':
kwargs['widget'] = forms.Textarea(attrs={'cols': 70, 'rows': 4})
return super(MiscellaneousAdmin, self).formfield_for_dbfield(db_field, **kwargs)
2015-03-27 19:50:54 +00:00
def save_model(self, request, obj, form, change):
if not change:
plugin = self.plugin
kwargs = {
plugin.name_field: self.plugin_value
}
setattr(obj, self.plugin_field, plugin.model.objects.get(**kwargs))
obj.save()
2014-07-16 15:20:16 +00:00
2014-07-16 15:20:16 +00:00
admin.site.register(MiscService, MiscServiceAdmin)
admin.site.register(Miscellaneous, MiscellaneousAdmin)