django-orchestra-test/orchestra/plugins/admin.py

127 lines
5 KiB
Python
Raw Normal View History

2015-04-09 14:32:10 +00:00
import re
2015-05-19 13:27:04 +00:00
from django.conf.urls import url
2014-11-12 16:33:40 +00:00
from django.contrib.admin.utils import unquote
from django.shortcuts import render, redirect
from django.utils.translation import ugettext_lazy as _
from orchestra.admin.utils import wrap_admin_view
class SelectPluginAdminMixin(object):
plugin = None
plugin_field = None
2015-03-04 21:06:16 +00:00
plugin_title = None
2014-11-12 16:33:40 +00:00
def get_form(self, request, obj=None, **kwargs):
if obj:
2015-10-05 12:09:11 +00:00
try:
plugin = getattr(obj, '%s_instance' % self.plugin_field)
except KeyError:
plugin_name = getattr(obj, self.plugin_field)
raise KeyError(_("Plugin '%s' is not available.") % plugin_name)
else:
self.form = getattr(plugin, 'get_change_form', plugin.get_form)()
2014-11-12 16:33:40 +00:00
else:
plugin = self.plugin.get(self.plugin_value)()
2015-03-04 21:06:16 +00:00
self.form = plugin.get_form()
self.plugin_instance = plugin
return super(SelectPluginAdminMixin, self).get_form(request, obj, **kwargs)
2014-11-12 16:33:40 +00:00
2015-03-04 21:06:16 +00:00
def get_fields(self, request, obj=None):
""" Try to maintain original field ordering """
fields = super(SelectPluginAdminMixin, self).get_fields(request, obj)
2015-03-04 21:06:16 +00:00
head_fields = list(self.get_readonly_fields(request, obj))
head, tail = [], []
for field in fields:
if field in head_fields:
head.append(field)
else:
tail.append(field)
return head + tail
2014-11-12 16:33:40 +00:00
def get_urls(self):
""" Hooks select account url """
urls = super(SelectPluginAdminMixin, self).get_urls()
opts = self.model._meta
info = opts.app_label, opts.model_name
2015-05-19 13:27:04 +00:00
select_urls = [
2016-04-28 10:34:54 +00:00
url("select-plugin/$",
2014-11-12 16:33:40 +00:00
wrap_admin_view(self, self.select_plugin_view),
name='%s_%s_select_plugin' % info),
2015-05-19 13:27:04 +00:00
]
return select_urls + urls
2014-11-12 16:33:40 +00:00
def select_plugin_view(self, request):
opts = self.model._meta
context = {
2015-03-04 21:06:16 +00:00
'plugin_title': self.plugin_title or 'Plugins',
2014-11-12 16:33:40 +00:00
'opts': opts,
'app_label': opts.app_label,
'field': self.plugin_field,
'field_name': opts.get_field_by_name(self.plugin_field)[0].verbose_name,
'plugin': self.plugin,
'plugins': self.plugin.get_plugins(),
}
template = 'admin/plugins/select_plugin.html'
return render(request, template, context)
2015-04-09 14:32:10 +00:00
def get_plugin_value(self, request):
plugin_value = request.GET.get(self.plugin_field) or request.POST.get(self.plugin_field)
if not plugin_value and request.method == 'POST':
# HACK baceuse django add_preserved_filters removes extising queryargs
value = re.search(r"%s=([^&^']+)[&']" % self.plugin_field,
request.META.get('HTTP_REFERER', ''))
2015-04-09 14:32:10 +00:00
if value:
plugin_value = value.groups()[0]
return plugin_value
2014-11-12 16:33:40 +00:00
def add_view(self, request, form_url='', extra_context=None):
""" Redirects to select account view if required """
if request.user.is_superuser:
2015-04-09 14:32:10 +00:00
plugin_value = self.get_plugin_value(request)
2014-11-12 16:33:40 +00:00
if plugin_value or len(self.plugin.get_plugins()) == 1:
self.plugin_value = plugin_value
if not plugin_value:
self.plugin_value = self.plugin.get_plugins()[0].get_name()
plugin = self.plugin.get(self.plugin_value)
2014-11-12 16:33:40 +00:00
context = {
2015-03-04 21:06:16 +00:00
'title': _("Add new %s") % plugin.verbose_name,
2014-11-12 16:33:40 +00:00
}
context.update(extra_context or {})
return super(SelectPluginAdminMixin, self).add_view(
request, form_url=form_url, extra_context=context)
2014-11-12 16:33:40 +00:00
return redirect('./select-plugin/?%s' % request.META['QUERY_STRING'])
def change_view(self, request, object_id, form_url='', extra_context=None):
obj = self.get_object(request, unquote(object_id))
2015-10-05 12:09:11 +00:00
try:
verbose = getattr(obj, '%s_class' % self.plugin_field).verbose_name
except KeyError:
raise KeyError(_("Plugin '%s' is not available.") % getattr(obj, self.plugin_field))
2014-11-12 16:33:40 +00:00
context = {
2015-10-05 12:09:11 +00:00
'title': _("Change %s") % verbose,
2014-11-12 16:33:40 +00:00
}
context.update(extra_context or {})
return super(SelectPluginAdminMixin, self).change_view(
request, object_id, form_url=form_url, extra_context=context)
2014-11-12 16:33:40 +00:00
def save_model(self, request, obj, form, change):
if not change:
setattr(obj, self.plugin_field, self.plugin_value)
obj.save()
2015-10-05 12:09:11 +00:00
def display_plugin_field(field_name):
def inner(modeladmin, obj, field_name=field_name):
try:
2015-10-05 13:31:08 +00:00
getattr(obj, '%s_class' % field_name)
2015-10-05 12:09:11 +00:00
except KeyError:
value = getattr(obj, field_name)
return "<span style='color:red;' title='Not available'>%s</span>" % value
return getattr(obj, 'get_%s_display' % field_name)()
inner.short_description = field_name
inner.admin_order_field = field_name
inner.allow_tags = True
return inner