Add preliminairy working JavaScript to render polymorphic inlines

This commit is contained in:
Diederik van der Boor
2016-08-09 01:20:40 +02:00
parent 1f0ddd8436
commit 7330a4f099
7 changed files with 552 additions and 1 deletions
+32
View File
@@ -3,8 +3,13 @@ Rendering utils for admin forms;
This makes sure that admin fieldsets/layout settings are exported to the template.
"""
import json
import django
from django.contrib.admin.helpers import InlineAdminFormSet, InlineAdminForm, AdminField
from django.utils.encoding import force_text
from django.utils.text import capfirst
from django.utils.translation import ugettext
from polymorphic.formsets import BasePolymorphicModelFormSet
@@ -14,6 +19,9 @@ class PolymorphicInlineAdminForm(InlineAdminForm):
Expose the admin configuration for a form
"""
def polymorphic_ctype_field(self):
return AdminField(self.form, 'polymorphic_ctype', False)
class PolymorphicInlineAdminFormSet(InlineAdminFormSet):
"""
@@ -71,6 +79,30 @@ class PolymorphicInlineAdminFormSet(InlineAdminFormSet):
fields.update(child_inline.get_prepopulated_fields(self.request, self.obj))
return fields
# The polymorphic template follows the same method like all other inlines do in Django 1.10.
# This method is added for compatibility with older Django versions.
def inline_formset_data(self):
"""
A JavaScript data structure for the JavaScript code
"""
verbose_name = self.opts.verbose_name
return json.dumps({
'name': '#%s' % self.formset.prefix,
'options': {
'prefix': self.formset.prefix,
'addText': ugettext('Add another %(verbose_name)s') % {
'verbose_name': capfirst(verbose_name),
},
'childTypes': [
{
'type': model._meta.model_name,
'name': force_text(model._meta.verbose_name)
} for model in self.formset.child_forms.keys()
],
'deleteText': ugettext('Remove'),
}
})
class PolymorphicInlineSupportMixin(object):
"""
+14
View File
@@ -22,8 +22,22 @@ class PolymorphicInlineModelAdmin(InlineModelAdmin):
* Permissions are only checked on the base model.
* The child inlines can't override the base model fields, only this parent inline can do that.
"""
formset = BasePolymorphicInlineFormSet
#: The extra media to add for the polymorphic inlines effect.
#: This can be redefined for subclasses.
polymorphic_media = Media(
js=(
'polymorphic/js/jquery.django-inlines.js',
),
css={
'all': (
'polymorphic/css/polymorphic_inlines.css',
)
}
)
#: The extra forms to show
#: By default there are no 'extra' forms as the desired type is unknown.
#: Instead, add each new item using JavaScript that first offers a type-selection.