Skip to content

Commit

Permalink
Fixed #13038 -- Ensured that readonly fields in the admin have their …
Browse files Browse the repository at this point in the history
…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
  • Loading branch information
freakboy3742 committed Apr 5, 2010
1 parent 653b27d commit 90d1127
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 8 deletions.
19 changes: 12 additions & 7 deletions django/contrib/admin/helpers.py
Expand Up @@ -128,7 +128,15 @@ def label_tag(self):


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
Expand All @@ -139,10 +147,8 @@ def label_tag(self):
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,
Expand All @@ -151,7 +157,7 @@ def label_tag(self):
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):
Expand Down Expand Up @@ -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

Expand Up @@ -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 %}>
Expand Down
5 changes: 5 additions & 0 deletions tests/regressiontests/admin_views/tests.py
Expand Up @@ -1967,6 +1967,11 @@ def test_readonly_get(self):
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)
Expand Down

0 comments on commit 90d1127

Please sign in to comment.