Ticket #11195: 11195_admin-changelist-cell-class-fieldname.1.diff

File 11195_admin-changelist-cell-class-fieldname.1.diff, 3.5 KB (added by akaihola, 6 years ago)

patch: initial implementation and tests

  • django/contrib/admin/templatetags/admin_list.py

    diff --git a/django/contrib/admin/templatetags/admin_list.py b/django/contrib/admin/templatetags/admin_list.py
    index d97c36a..e046246 100644
    a b def items_for_result(cl, result, form): 
    137137    first = True
    138138    pk = cl.lookup_opts.pk.attname
    139139    for field_name in cl.list_display:
    140         row_class = ''
     140        row_classes = ['fieldname_%s' % field_name]
    141141        try:
    142142            f = cl.lookup_opts.get_field(field_name)
    143143        except models.FieldDoesNotExist:
    def items_for_result(cl, result, form): 
    193193                        result_repr = capfirst(dateformat.format(field_val, date_format))
    194194                else:
    195195                    result_repr = EMPTY_CHANGELIST_VALUE
    196                 row_class = ' class="nowrap"'
     196                row_classes.append('nowrap')
    197197            # Booleans are special: We use images.
    198198            elif isinstance(f, models.BooleanField) or isinstance(f, models.NullBooleanField):
    199199                result_repr = _boolean_icon(field_val)
    def items_for_result(cl, result, form): 
    211211                result_repr = escape(field_val)
    212212        if force_unicode(result_repr) == '':
    213213            result_repr = mark_safe(' ')
     214        row_class = ' class="%s"' % ' '.join(row_classes)
    214215        # If list_display_links not defined, add the link tag to the first field
    215216        if (first and not cl.list_display_links) or field_name in cl.list_display_links:
    216217            table_tag = {True:'th', False:'td'}[first]
  • tests/regressiontests/admin_views/tests.py

    diff --git a/tests/regressiontests/admin_views/tests.py b/tests/regressiontests/admin_views/tests.py
    index 8e7010b..c3c5dbd 100644
    a b class AdminViewPermissionsTest(TestCase): 
    577577        self.failUnlessEqual(logged.object_id, u'1')
    578578        self.client.get('/test_admin/admin/logout/')
    579579
     580class AdminChangeListFieldNameClassAttrTest(TestCase):
     581    fixtures = ['admin-views-users.xml']
     582
     583    def setUp(self):
     584        self.client.login(username='super', password='secret')
     585
     586    def tearDown(self):
     587        self.client.logout()
     588
     589    def test_class_attributes(self):
     590        "Cells of the change list table should contain the field name in their class attribute"
     591        Podcast.objects.create(name="This Week in Django",
     592            release_date=datetime.date.today())
     593        response = self.client.get('/test_admin/admin/admin_views/podcast/')
     594        self.assertContains(
     595            response, '<td class="fieldname_action_checkbox">')
     596        self.assertContains(
     597            response, '<th class="fieldname_name">')
     598        self.assertContains(
     599            response, '<td class="fieldname_release_date nowrap">')
     600
    580601class AdminViewStringPrimaryKeyTest(TestCase):
    581602    fixtures = ['admin-views-users.xml', 'string-primary-key.xml']
    582603
    class AdminViewStringPrimaryKeyTest(TestCase): 
    601622    def test_changelist_to_changeform_link(self):
    602623        "The link from the changelist referring to the changeform of the object should be quoted"
    603624        response = self.client.get('/test_admin/admin/admin_views/modelwithstringprimarykey/')
    604         should_contain = """<th><a href="%s/">%s</a></th></tr>""" % (quote(self.pk), escape(self.pk))
     625        should_contain = """<th class="fieldname___str__"><a href="%s/">%s</a></th></tr>""" % (quote(self.pk), escape(self.pk))
    605626        self.assertContains(response, should_contain)
    606627
    607628    def test_recentactions_link(self):
Back to Top