2012-06-08 00:08:47 +08:00
|
|
|
from __future__ import unicode_literals
|
|
|
|
|
2008-08-18 23:49:58 +08:00
|
|
|
from django import forms
|
2011-03-03 09:54:33 +08:00
|
|
|
from django.contrib.admin.util import (flatten_fieldsets, lookup_field,
|
|
|
|
display_for_field, label_for_field, help_text_for_field)
|
2011-08-11 22:07:39 +08:00
|
|
|
from django.contrib.admin.templatetags.admin_static import static
|
2008-08-27 02:07:36 +08:00
|
|
|
from django.contrib.contenttypes.models import ContentType
|
2009-12-23 02:29:00 +08:00
|
|
|
from django.core.exceptions import ObjectDoesNotExist
|
|
|
|
from django.db.models.fields.related import ManyToManyRel
|
|
|
|
from django.forms.util import flatatt
|
2010-04-11 16:35:04 +08:00
|
|
|
from django.template.defaultfilters import capfirst
|
2012-07-21 16:00:10 +08:00
|
|
|
from django.utils.encoding import force_text, smart_text
|
2012-07-03 07:31:14 +08:00
|
|
|
from django.utils.html import conditional_escape, format_html
|
2009-12-23 02:29:00 +08:00
|
|
|
from django.utils.safestring import mark_safe
|
2012-07-20 20:22:00 +08:00
|
|
|
from django.utils import six
|
2009-03-24 04:22:56 +08:00
|
|
|
from django.utils.translation import ugettext_lazy as _
|
2012-02-05 18:26:05 +08:00
|
|
|
from django.conf import settings
|
2009-03-24 04:22:56 +08:00
|
|
|
|
2009-12-23 02:29:00 +08:00
|
|
|
|
2009-03-24 04:22:56 +08:00
|
|
|
ACTION_CHECKBOX_NAME = '_selected_action'
|
|
|
|
|
|
|
|
class ActionForm(forms.Form):
|
|
|
|
action = forms.ChoiceField(label=_('Action:'))
|
2010-01-26 23:03:23 +08:00
|
|
|
select_across = forms.BooleanField(label='', required=False, initial=0,
|
|
|
|
widget=forms.HiddenInput({'class': 'select-across'}))
|
2009-03-24 04:22:56 +08:00
|
|
|
|
|
|
|
checkbox = forms.CheckboxInput({'class': 'action-select'}, lambda value: False)
|
2008-08-18 23:49:58 +08:00
|
|
|
|
|
|
|
class AdminForm(object):
|
2009-12-23 02:29:00 +08:00
|
|
|
def __init__(self, form, fieldsets, prepopulated_fields, readonly_fields=None, model_admin=None):
|
2009-04-11 15:41:59 +08:00
|
|
|
self.form, self.fieldsets = form, normalize_fieldsets(fieldsets)
|
2008-08-18 23:49:58 +08:00
|
|
|
self.prepopulated_fields = [{
|
|
|
|
'field': form[field_name],
|
|
|
|
'dependencies': [form[f] for f in dependencies]
|
|
|
|
} for field_name, dependencies in prepopulated_fields.items()]
|
2009-12-23 02:29:00 +08:00
|
|
|
self.model_admin = model_admin
|
|
|
|
if readonly_fields is None:
|
|
|
|
readonly_fields = ()
|
|
|
|
self.readonly_fields = readonly_fields
|
2008-08-18 23:49:58 +08:00
|
|
|
|
|
|
|
def __iter__(self):
|
|
|
|
for name, options in self.fieldsets:
|
2009-12-23 02:29:00 +08:00
|
|
|
yield Fieldset(self.form, name,
|
|
|
|
readonly_fields=self.readonly_fields,
|
|
|
|
model_admin=self.model_admin,
|
|
|
|
**options
|
|
|
|
)
|
2008-08-18 23:49:58 +08:00
|
|
|
|
|
|
|
def first_field(self):
|
2008-09-01 04:30:55 +08:00
|
|
|
try:
|
|
|
|
fieldset_name, fieldset_options = self.fieldsets[0]
|
|
|
|
field_name = fieldset_options['fields'][0]
|
2012-07-20 20:22:00 +08:00
|
|
|
if not isinstance(field_name, six.string_types):
|
2008-09-01 04:30:55 +08:00
|
|
|
field_name = field_name[0]
|
|
|
|
return self.form[field_name]
|
|
|
|
except (KeyError, IndexError):
|
|
|
|
pass
|
2008-08-30 00:07:19 +08:00
|
|
|
try:
|
2012-05-11 02:14:04 +08:00
|
|
|
return next(iter(self.form))
|
2008-08-30 00:07:19 +08:00
|
|
|
except StopIteration:
|
|
|
|
return None
|
2008-08-18 23:49:58 +08:00
|
|
|
|
|
|
|
def _media(self):
|
|
|
|
media = self.form.media
|
|
|
|
for fs in self:
|
|
|
|
media = media + fs.media
|
|
|
|
return media
|
|
|
|
media = property(_media)
|
|
|
|
|
|
|
|
class Fieldset(object):
|
2009-12-23 02:29:00 +08:00
|
|
|
def __init__(self, form, name=None, readonly_fields=(), fields=(), classes=(),
|
|
|
|
description=None, model_admin=None):
|
2008-08-18 23:49:58 +08:00
|
|
|
self.form = form
|
|
|
|
self.name, self.fields = name, fields
|
2012-06-08 00:08:47 +08:00
|
|
|
self.classes = ' '.join(classes)
|
2008-08-18 23:49:58 +08:00
|
|
|
self.description = description
|
2009-12-23 02:29:00 +08:00
|
|
|
self.model_admin = model_admin
|
|
|
|
self.readonly_fields = readonly_fields
|
2008-08-18 23:49:58 +08:00
|
|
|
|
|
|
|
def _media(self):
|
|
|
|
if 'collapse' in self.classes:
|
2012-02-05 18:26:05 +08:00
|
|
|
extra = '' if settings.DEBUG else '.min'
|
|
|
|
js = ['jquery%s.js' % extra,
|
|
|
|
'jquery.init.js',
|
|
|
|
'collapse%s.js' % extra]
|
2011-08-11 22:07:39 +08:00
|
|
|
return forms.Media(js=[static('admin/js/%s' % url) for url in js])
|
2008-08-18 23:49:58 +08:00
|
|
|
return forms.Media()
|
|
|
|
media = property(_media)
|
|
|
|
|
|
|
|
def __iter__(self):
|
|
|
|
for field in self.fields:
|
2009-12-23 02:29:00 +08:00
|
|
|
yield Fieldline(self.form, field, self.readonly_fields, model_admin=self.model_admin)
|
2008-08-18 23:49:58 +08:00
|
|
|
|
|
|
|
class Fieldline(object):
|
2009-12-23 02:29:00 +08:00
|
|
|
def __init__(self, form, field, readonly_fields=None, model_admin=None):
|
2008-08-18 23:49:58 +08:00
|
|
|
self.form = form # A django.forms.Form instance
|
2012-08-08 23:59:31 +08:00
|
|
|
if not hasattr(field, "__iter__") or isinstance(field, six.text_type):
|
2008-08-18 23:49:58 +08:00
|
|
|
self.fields = [field]
|
|
|
|
else:
|
|
|
|
self.fields = field
|
2009-12-23 02:29:00 +08:00
|
|
|
self.model_admin = model_admin
|
|
|
|
if readonly_fields is None:
|
|
|
|
readonly_fields = ()
|
|
|
|
self.readonly_fields = readonly_fields
|
2008-08-18 23:49:58 +08:00
|
|
|
|
|
|
|
def __iter__(self):
|
|
|
|
for i, field in enumerate(self.fields):
|
2009-12-23 02:29:00 +08:00
|
|
|
if field in self.readonly_fields:
|
|
|
|
yield AdminReadonlyField(self.form, field, is_first=(i == 0),
|
|
|
|
model_admin=self.model_admin)
|
|
|
|
else:
|
|
|
|
yield AdminField(self.form, field, is_first=(i == 0))
|
2008-08-18 23:49:58 +08:00
|
|
|
|
|
|
|
def errors(self):
|
2012-06-08 00:08:47 +08:00
|
|
|
return mark_safe('\n'.join([self.form[f].errors.as_ul() for f in self.fields if f not in self.readonly_fields]).strip('\n'))
|
2008-08-18 23:49:58 +08:00
|
|
|
|
|
|
|
class AdminField(object):
|
|
|
|
def __init__(self, form, field, is_first):
|
|
|
|
self.field = form[field] # A django.forms.BoundField instance
|
|
|
|
self.is_first = is_first # Whether this field is first on the line
|
|
|
|
self.is_checkbox = isinstance(self.field.field.widget, forms.CheckboxInput)
|
|
|
|
|
|
|
|
def label_tag(self):
|
|
|
|
classes = []
|
2012-07-21 16:00:10 +08:00
|
|
|
contents = conditional_escape(force_text(self.field.label))
|
2008-08-18 23:49:58 +08:00
|
|
|
if self.is_checkbox:
|
2012-06-08 00:08:47 +08:00
|
|
|
classes.append('vCheckboxLabel')
|
2008-08-18 23:49:58 +08:00
|
|
|
else:
|
2012-06-08 00:08:47 +08:00
|
|
|
contents += ':'
|
2008-08-18 23:49:58 +08:00
|
|
|
if self.field.field.required:
|
2012-06-08 00:08:47 +08:00
|
|
|
classes.append('required')
|
2008-08-18 23:49:58 +08:00
|
|
|
if not self.is_first:
|
2012-06-08 00:08:47 +08:00
|
|
|
classes.append('inline')
|
|
|
|
attrs = classes and {'class': ' '.join(classes)} or {}
|
2012-02-04 20:48:21 +08:00
|
|
|
return self.field.label_tag(contents=mark_safe(contents), attrs=attrs)
|
2008-08-18 23:49:58 +08:00
|
|
|
|
2010-12-21 23:07:09 +08:00
|
|
|
def errors(self):
|
|
|
|
return mark_safe(self.field.errors.as_ul())
|
|
|
|
|
2009-12-23 02:29:00 +08:00
|
|
|
class AdminReadonlyField(object):
|
|
|
|
def __init__(self, form, field, is_first, model_admin=None):
|
2010-04-05 23:37:43 +08:00
|
|
|
label = label_for_field(field, form._meta.model, model_admin)
|
|
|
|
# Make self.field look a little bit like a field. This means that
|
|
|
|
# {{ field.name }} must be a useful class name to identify the field.
|
|
|
|
# For convenience, store other field-related data here too.
|
2010-04-11 16:35:04 +08:00
|
|
|
if callable(field):
|
|
|
|
class_name = field.__name__ != '<lambda>' and field.__name__ or ''
|
|
|
|
else:
|
|
|
|
class_name = field
|
2010-04-05 23:37:43 +08:00
|
|
|
self.field = {
|
2010-04-11 16:35:04 +08:00
|
|
|
'name': class_name,
|
2010-04-05 23:37:43 +08:00
|
|
|
'label': label,
|
|
|
|
'field': field,
|
2011-02-19 20:55:09 +08:00
|
|
|
'help_text': help_text_for_field(class_name, form._meta.model)
|
2010-04-05 23:37:43 +08:00
|
|
|
}
|
2009-12-23 02:29:00 +08:00
|
|
|
self.form = form
|
|
|
|
self.model_admin = model_admin
|
|
|
|
self.is_first = is_first
|
|
|
|
self.is_checkbox = False
|
|
|
|
self.is_readonly = True
|
|
|
|
|
|
|
|
def label_tag(self):
|
|
|
|
attrs = {}
|
|
|
|
if not self.is_first:
|
|
|
|
attrs["class"] = "inline"
|
2010-04-11 16:35:04 +08:00
|
|
|
label = self.field['label']
|
2012-07-03 07:31:14 +08:00
|
|
|
return format_html('<label{0}>{1}:</label>',
|
|
|
|
flatatt(attrs),
|
2012-07-21 16:00:10 +08:00
|
|
|
capfirst(force_text(label)))
|
2009-12-23 02:29:00 +08:00
|
|
|
|
|
|
|
def contents(self):
|
|
|
|
from django.contrib.admin.templatetags.admin_list import _boolean_icon
|
|
|
|
from django.contrib.admin.views.main import EMPTY_CHANGELIST_VALUE
|
2010-04-05 23:37:43 +08:00
|
|
|
field, obj, model_admin = self.field['field'], self.form.instance, self.model_admin
|
2009-12-23 02:29:00 +08:00
|
|
|
try:
|
|
|
|
f, attr, value = lookup_field(field, obj, model_admin)
|
2010-03-21 22:23:25 +08:00
|
|
|
except (AttributeError, ValueError, ObjectDoesNotExist):
|
2009-12-23 02:29:00 +08:00
|
|
|
result_repr = EMPTY_CHANGELIST_VALUE
|
|
|
|
else:
|
|
|
|
if f is None:
|
|
|
|
boolean = getattr(attr, "boolean", False)
|
|
|
|
if boolean:
|
|
|
|
result_repr = _boolean_icon(value)
|
|
|
|
else:
|
2012-07-21 16:00:10 +08:00
|
|
|
result_repr = smart_text(value)
|
2009-12-23 02:29:00 +08:00
|
|
|
if getattr(attr, "allow_tags", False):
|
|
|
|
result_repr = mark_safe(result_repr)
|
|
|
|
else:
|
2012-11-26 06:13:22 +08:00
|
|
|
if isinstance(f.rel, ManyToManyRel) and value is not None:
|
2012-07-20 20:48:51 +08:00
|
|
|
result_repr = ", ".join(map(six.text_type, value.all()))
|
2009-12-23 02:29:00 +08:00
|
|
|
else:
|
|
|
|
result_repr = display_for_field(value, f)
|
|
|
|
return conditional_escape(result_repr)
|
|
|
|
|
2008-08-18 23:49:58 +08:00
|
|
|
class InlineAdminFormSet(object):
|
|
|
|
"""
|
|
|
|
A wrapper around an inline formset for use in the admin system.
|
|
|
|
"""
|
2011-04-22 20:02:25 +08:00
|
|
|
def __init__(self, inline, formset, fieldsets, prepopulated_fields=None,
|
|
|
|
readonly_fields=None, model_admin=None):
|
2008-08-18 23:49:58 +08:00
|
|
|
self.opts = inline
|
|
|
|
self.formset = formset
|
|
|
|
self.fieldsets = fieldsets
|
2009-12-23 02:29:00 +08:00
|
|
|
self.model_admin = model_admin
|
|
|
|
if readonly_fields is None:
|
|
|
|
readonly_fields = ()
|
|
|
|
self.readonly_fields = readonly_fields
|
2011-04-22 20:02:25 +08:00
|
|
|
if prepopulated_fields is None:
|
|
|
|
prepopulated_fields = {}
|
|
|
|
self.prepopulated_fields = prepopulated_fields
|
2008-08-18 23:49:58 +08:00
|
|
|
|
|
|
|
def __iter__(self):
|
|
|
|
for form, original in zip(self.formset.initial_forms, self.formset.get_queryset()):
|
2009-12-23 02:29:00 +08:00
|
|
|
yield InlineAdminForm(self.formset, form, self.fieldsets,
|
2011-04-22 20:02:25 +08:00
|
|
|
self.prepopulated_fields, original, self.readonly_fields,
|
2011-02-26 09:44:41 +08:00
|
|
|
model_admin=self.opts)
|
2008-08-18 23:49:58 +08:00
|
|
|
for form in self.formset.extra_forms:
|
2009-12-23 02:29:00 +08:00
|
|
|
yield InlineAdminForm(self.formset, form, self.fieldsets,
|
2011-04-22 20:02:25 +08:00
|
|
|
self.prepopulated_fields, None, self.readonly_fields,
|
2011-02-26 09:44:41 +08:00
|
|
|
model_admin=self.opts)
|
2010-01-26 23:02:53 +08:00
|
|
|
yield InlineAdminForm(self.formset, self.formset.empty_form,
|
2011-04-22 20:02:25 +08:00
|
|
|
self.fieldsets, self.prepopulated_fields, None,
|
2011-02-26 09:44:41 +08:00
|
|
|
self.readonly_fields, model_admin=self.opts)
|
2010-01-26 23:02:53 +08:00
|
|
|
|
2008-08-18 23:49:58 +08:00
|
|
|
def fields(self):
|
2008-11-14 03:03:44 +08:00
|
|
|
fk = getattr(self.formset, "fk", None)
|
2009-12-23 02:29:00 +08:00
|
|
|
for i, field in enumerate(flatten_fieldsets(self.fieldsets)):
|
|
|
|
if fk and fk.name == field:
|
2008-11-01 06:07:05 +08:00
|
|
|
continue
|
2009-12-23 02:29:00 +08:00
|
|
|
if field in self.readonly_fields:
|
2010-04-30 23:24:07 +08:00
|
|
|
yield {
|
2011-02-26 09:44:41 +08:00
|
|
|
'label': label_for_field(field, self.opts.model, self.opts),
|
2010-04-30 23:24:07 +08:00
|
|
|
'widget': {
|
|
|
|
'is_hidden': False
|
|
|
|
},
|
|
|
|
'required': False
|
|
|
|
}
|
2009-12-23 02:29:00 +08:00
|
|
|
else:
|
2010-04-30 23:24:07 +08:00
|
|
|
yield self.formset.form.base_fields[field]
|
2008-08-18 23:49:58 +08:00
|
|
|
|
|
|
|
def _media(self):
|
|
|
|
media = self.opts.media + self.formset.media
|
|
|
|
for fs in self:
|
|
|
|
media = media + fs.media
|
|
|
|
return media
|
|
|
|
media = property(_media)
|
|
|
|
|
|
|
|
class InlineAdminForm(AdminForm):
|
|
|
|
"""
|
|
|
|
A wrapper around an inline form for use in the admin system.
|
|
|
|
"""
|
2009-12-23 02:29:00 +08:00
|
|
|
def __init__(self, formset, form, fieldsets, prepopulated_fields, original,
|
|
|
|
readonly_fields=None, model_admin=None):
|
2008-08-18 23:49:58 +08:00
|
|
|
self.formset = formset
|
2009-12-23 02:29:00 +08:00
|
|
|
self.model_admin = model_admin
|
2008-08-18 23:49:58 +08:00
|
|
|
self.original = original
|
2008-08-27 02:07:36 +08:00
|
|
|
if original is not None:
|
2009-05-03 21:38:36 +08:00
|
|
|
self.original_content_type_id = ContentType.objects.get_for_model(original).pk
|
2008-08-18 23:49:58 +08:00
|
|
|
self.show_url = original and hasattr(original, 'get_absolute_url')
|
2009-12-23 02:29:00 +08:00
|
|
|
super(InlineAdminForm, self).__init__(form, fieldsets, prepopulated_fields,
|
2010-03-27 07:38:05 +08:00
|
|
|
readonly_fields, model_admin)
|
2009-03-24 04:22:56 +08:00
|
|
|
|
2008-11-01 06:07:05 +08:00
|
|
|
def __iter__(self):
|
|
|
|
for name, options in self.fieldsets:
|
2009-12-23 02:29:00 +08:00
|
|
|
yield InlineFieldset(self.formset, self.form, name,
|
|
|
|
self.readonly_fields, model_admin=self.model_admin, **options)
|
2009-03-24 04:22:56 +08:00
|
|
|
|
2009-05-03 21:37:20 +08:00
|
|
|
def has_auto_field(self):
|
2009-05-09 19:40:13 +08:00
|
|
|
if self.form._meta.model._meta.has_auto_field:
|
|
|
|
return True
|
|
|
|
# Also search any parents for an auto field.
|
|
|
|
for parent in self.form._meta.model._meta.get_parent_list():
|
|
|
|
if parent._meta.has_auto_field:
|
|
|
|
return True
|
|
|
|
return False
|
2009-05-03 21:37:20 +08:00
|
|
|
|
2008-10-22 04:25:45 +08:00
|
|
|
def field_count(self):
|
|
|
|
# tabular.html uses this function for colspan value.
|
2009-05-03 21:37:20 +08:00
|
|
|
num_of_fields = 0
|
|
|
|
if self.has_auto_field():
|
|
|
|
num_of_fields += 1
|
2008-10-22 04:25:45 +08:00
|
|
|
num_of_fields += len(self.fieldsets[0][1]["fields"])
|
|
|
|
if self.formset.can_order:
|
|
|
|
num_of_fields += 1
|
|
|
|
if self.formset.can_delete:
|
|
|
|
num_of_fields += 1
|
|
|
|
return num_of_fields
|
2008-08-18 23:49:58 +08:00
|
|
|
|
|
|
|
def pk_field(self):
|
2008-08-31 17:49:55 +08:00
|
|
|
return AdminField(self.form, self.formset._pk_field.name, False)
|
2009-03-24 04:22:56 +08:00
|
|
|
|
2008-11-01 06:07:05 +08:00
|
|
|
def fk_field(self):
|
2008-11-14 03:03:44 +08:00
|
|
|
fk = getattr(self.formset, "fk", None)
|
|
|
|
if fk:
|
|
|
|
return AdminField(self.form, fk.name, False)
|
|
|
|
else:
|
|
|
|
return ""
|
2008-08-18 23:49:58 +08:00
|
|
|
|
|
|
|
def deletion_field(self):
|
|
|
|
from django.forms.formsets import DELETION_FIELD_NAME
|
|
|
|
return AdminField(self.form, DELETION_FIELD_NAME, False)
|
|
|
|
|
|
|
|
def ordering_field(self):
|
|
|
|
from django.forms.formsets import ORDERING_FIELD_NAME
|
|
|
|
return AdminField(self.form, ORDERING_FIELD_NAME, False)
|
|
|
|
|
2008-11-01 06:07:05 +08:00
|
|
|
class InlineFieldset(Fieldset):
|
|
|
|
def __init__(self, formset, *args, **kwargs):
|
|
|
|
self.formset = formset
|
|
|
|
super(InlineFieldset, self).__init__(*args, **kwargs)
|
2009-03-24 04:22:56 +08:00
|
|
|
|
2008-11-01 06:07:05 +08:00
|
|
|
def __iter__(self):
|
2008-11-14 03:03:44 +08:00
|
|
|
fk = getattr(self.formset, "fk", None)
|
2008-11-01 06:07:05 +08:00
|
|
|
for field in self.fields:
|
2008-11-14 03:03:44 +08:00
|
|
|
if fk and fk.name == field:
|
2008-11-01 06:07:05 +08:00
|
|
|
continue
|
2009-12-23 02:29:00 +08:00
|
|
|
yield Fieldline(self.form, field, self.readonly_fields,
|
|
|
|
model_admin=self.model_admin)
|
2009-03-24 04:22:56 +08:00
|
|
|
|
2008-08-18 23:49:58 +08:00
|
|
|
class AdminErrorList(forms.util.ErrorList):
|
|
|
|
"""
|
|
|
|
Stores all errors for the form/formsets in an add/change stage view.
|
|
|
|
"""
|
|
|
|
def __init__(self, form, inline_formsets):
|
|
|
|
if form.is_bound:
|
2012-07-21 03:14:27 +08:00
|
|
|
self.extend(list(six.itervalues(form.errors)))
|
2008-08-18 23:49:58 +08:00
|
|
|
for inline_formset in inline_formsets:
|
|
|
|
self.extend(inline_formset.non_form_errors())
|
|
|
|
for errors_in_inline_form in inline_formset.errors:
|
2012-07-21 03:14:27 +08:00
|
|
|
self.extend(list(six.itervalues(errors_in_inline_form)))
|
2009-04-11 15:41:59 +08:00
|
|
|
|
|
|
|
def normalize_fieldsets(fieldsets):
|
|
|
|
"""
|
|
|
|
Make sure the keys in fieldset dictionaries are strings. Returns the
|
|
|
|
normalized data.
|
|
|
|
"""
|
|
|
|
result = []
|
|
|
|
for name, options in fieldsets:
|
|
|
|
result.append((name, normalize_dictionary(options)))
|
|
|
|
return result
|
|
|
|
|
|
|
|
def normalize_dictionary(data_dict):
|
|
|
|
"""
|
|
|
|
Converts all the keys in "data_dict" to strings. The keys must be
|
|
|
|
convertible using str().
|
|
|
|
"""
|
|
|
|
for key, value in data_dict.items():
|
|
|
|
if not isinstance(key, str):
|
|
|
|
del data_dict[key]
|
|
|
|
data_dict[str(key)] = value
|
|
|
|
return data_dict
|