Fixed #13038 -- Ensured that readonly fields in the admin have their name added as a CSS class. Thanks to andybak for the report, and javimansilla, fisadev and fgallina for their work on the patch.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@12922 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Russell Keith-Magee 2010-04-05 15:37:43 +00:00
parent 653b27dcf2
commit 90d112785b
3 changed files with 18 additions and 8 deletions

View File

@ -128,7 +128,15 @@ class AdminField(object):
class AdminReadonlyField(object): class AdminReadonlyField(object):
def __init__(self, form, field, is_first, model_admin=None): def __init__(self, form, field, is_first, model_admin=None):
self.field = field 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.
self.field = {
'name': force_unicode(label != '--' and label or ''),
'label': label,
'field': field,
}
self.form = form self.form = form
self.model_admin = model_admin self.model_admin = model_admin
self.is_first = is_first self.is_first = is_first
@ -139,10 +147,8 @@ class AdminReadonlyField(object):
attrs = {} attrs = {}
if not self.is_first: if not self.is_first:
attrs["class"] = "inline" attrs["class"] = "inline"
name = forms.forms.pretty_name( label = forms.forms.pretty_name(self.field['label'])
label_for_field(self.field, self.form._meta.model, self.model_admin) contents = force_unicode(escape(label)) + u":"
)
contents = force_unicode(escape(name)) + u":"
return mark_safe('<label%(attrs)s>%(contents)s</label>' % { return mark_safe('<label%(attrs)s>%(contents)s</label>' % {
"attrs": flatatt(attrs), "attrs": flatatt(attrs),
"contents": contents, "contents": contents,
@ -151,7 +157,7 @@ class AdminReadonlyField(object):
def contents(self): def contents(self):
from django.contrib.admin.templatetags.admin_list import _boolean_icon from django.contrib.admin.templatetags.admin_list import _boolean_icon
from django.contrib.admin.views.main import EMPTY_CHANGELIST_VALUE from django.contrib.admin.views.main import EMPTY_CHANGELIST_VALUE
field, obj, model_admin = self.field, self.form.instance, self.model_admin field, obj, model_admin = self.field['field'], self.form.instance, self.model_admin
try: try:
f, attr, value = lookup_field(field, obj, model_admin) f, attr, value = lookup_field(field, obj, model_admin)
except (AttributeError, ValueError, ObjectDoesNotExist): except (AttributeError, ValueError, ObjectDoesNotExist):
@ -323,4 +329,3 @@ def normalize_dictionary(data_dict):
del data_dict[key] del data_dict[key]
data_dict[str(key)] = value data_dict[str(key)] = value
return data_dict return data_dict

View File

@ -4,7 +4,7 @@
<div class="description">{{ fieldset.description|safe }}</div> <div class="description">{{ fieldset.description|safe }}</div>
{% endif %} {% endif %}
{% for line in fieldset %} {% for line in fieldset %}
<div class="form-row{% if line.errors %} errors{% endif %} {% for field in line %}{{ field.field.name }} {% endfor %} "> <div class="form-row{% if line.errors %} errors{% endif %}{% for field in line %} {{ field.field.name }}{% endfor %}">
{{ line.errors }} {{ line.errors }}
{% for field in line %} {% for field in line %}
<div{% if not line.fields|length_is:"1" %} class="field-box"{% endif %}> <div{% if not line.fields|length_is:"1" %} class="field-box"{% endif %}>

View File

@ -1967,6 +1967,11 @@ class ReadonlyTest(TestCase):
formats.localize(datetime.date.today() - datetime.timedelta(days=7)) formats.localize(datetime.date.today() - datetime.timedelta(days=7))
) )
self.assertContains(response, '<div class="form-row coolness">')
self.assertContains(response, '<div class="form-row awesomeness_level">')
self.assertContains(response, '<div class="form-row posted">')
self.assertContains(response, '<div class="form-row ">')
p = Post.objects.create(title="I worked on readonly_fields", content="Its good stuff") p = Post.objects.create(title="I worked on readonly_fields", content="Its good stuff")
response = self.client.get('/test_admin/admin/admin_views/post/%d/' % p.pk) response = self.client.get('/test_admin/admin/admin_views/post/%d/' % p.pk)
self.assertContains(response, "%d amount of cool" % p.pk) self.assertContains(response, "%d amount of cool" % p.pk)