django-orchestra/orchestra/plugins/admin.py

93 lines
3.8 KiB
Python
Raw Normal View History

2014-11-12 16:33:40 +00:00
from django.conf.urls import patterns, url
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
from orchestra.utils.functional import cached
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-03-04 21:06:16 +00:00
plugin = getattr(obj, '%s_instance' % self.plugin_field)
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()
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
select_urls = patterns("",
url("/select-plugin/$",
wrap_admin_view(self, self.select_plugin_view),
name='%s_%s_select_plugin' % info),
)
return select_urls + urls
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)
def add_view(self, request, form_url='', extra_context=None):
""" Redirects to select account view if required """
if request.user.is_superuser:
plugin_value = request.GET.get(self.plugin_field) or request.POST.get(self.plugin_field)
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)
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-03-04 21:06:16 +00:00
plugin = getattr(obj, '%s_class' % self.plugin_field)
2014-11-12 16:33:40 +00:00
context = {
2015-03-04 21:06:16 +00:00
'title': _("Change %s") % plugin.verbose_name,
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)
def save_model(self, request, obj, form, change):
if not change:
setattr(obj, self.plugin_field, self.plugin_value)
obj.save()