Ticket #11195: 11195.changelist-cells-css-classes.diff

File 11195.changelist-cells-css-classes.diff, 7.7 KB (added by julien, 4 years ago)
  • 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 b72c0be..e068cd6 100644
    a b def items_for_result(cl, result, form): 
    133133    first = True
    134134    pk = cl.lookup_opts.pk.attname
    135135    for field_name in cl.list_display:
    136         row_class = ''
     136        row_classes = ['%s-cell' % field_name]
    137137        try:
    138138            f, attr, value = lookup_field(field_name, result, cl.model_admin)
    139139        except (AttributeError, ObjectDoesNotExist):
    def items_for_result(cl, result, form): 
    141141        else:
    142142            if f is None:
    143143                if field_name == u'action_checkbox':
    144                     row_class = ' class="action-checkbox"'
     144                    row_classes = ['action-checkbox']
    145145                allow_tags = getattr(attr, 'allow_tags', False)
    146146                boolean = getattr(attr, 'boolean', False)
    147147                if boolean:
    def items_for_result(cl, result, form): 
    167167                if isinstance(f, models.DateField)\
    168168                or isinstance(f, models.TimeField)\
    169169                or isinstance(f, models.ForeignKey):
    170                     row_class = ' class="nowrap"'
     170                    row_classes.append('nowrap')
    171171        if force_unicode(result_repr) == '':
    172172            result_repr = mark_safe(' ')
     173        row_class = ' class="%s"' % ' '.join(row_classes)
    173174        # If list_display_links not defined, add the link tag to the first field
    174175        if (first and not cl.list_display_links) or field_name in cl.list_display_links:
    175176            table_tag = {True:'th', False:'td'}[first]
  • tests/regressiontests/admin_changelist/tests.py

    diff --git a/tests/regressiontests/admin_changelist/tests.py b/tests/regressiontests/admin_changelist/tests.py
    index 5186508..782f91a 100644
    a b class ChangeListTests(TransactionTestCase): 
    3636        template = Template('{% load admin_list %}{% spaceless %}{% result_list cl %}{% endspaceless %}')
    3737        context = Context({'cl': cl})
    3838        table_output = template.render(context)
    39         row_html = '<tbody><tr class="row1"><td class="action-checkbox"><input type="checkbox" class="action-select" value="%d" name="_selected_action" /></td><th><a href="%d/">name</a></th><td class="nowrap">(None)</td></tr></tbody>' % (new_child.id, new_child.id)
     39        row_html = '<tbody><tr class="row1"><td class="action-checkbox"><input type="checkbox" class="action-select" value="%d" name="_selected_action" /></td><th class="name-cell"><a href="%d/">name</a></th><td class="parent-cell nowrap">(None)</td></tr></tbody>' % (new_child.id, new_child.id)
    4040        self.assertFalse(table_output.find(row_html) == -1,
    4141            'Failed to find expected row element: %s' % table_output)
    4242
    class ChangeListTests(TransactionTestCase): 
    5757        template = Template('{% load admin_list %}{% spaceless %}{% result_list cl %}{% endspaceless %}')
    5858        context = Context({'cl': cl})
    5959        table_output = template.render(context)
    60         row_html = '<tbody><tr class="row1"><td class="action-checkbox"><input type="checkbox" class="action-select" value="%d" name="_selected_action" /></td><th><a href="%d/">name</a></th><td class="nowrap">Parent object</td></tr></tbody>' % (new_child.id, new_child.id)
     60        row_html = '<tbody><tr class="row1"><td class="action-checkbox"><input type="checkbox" class="action-select" value="%d" name="_selected_action" /></td><th class="name-cell"><a href="%d/">name</a></th><td class="parent-cell nowrap">Parent object</td></tr></tbody>' % (new_child.id, new_child.id)
    6161        self.assertFalse(table_output.find(row_html) == -1,
    6262            'Failed to find expected row element: %s' % table_output)
    6363
  • tests/regressiontests/admin_views/tests.py

    diff --git a/tests/regressiontests/admin_views/tests.py b/tests/regressiontests/admin_views/tests.py
    index 54d89e4..c9a5bd1 100644
    a b class AdminViewDeletedObjectsTest(TestCase): 
    10521052        response = self.client.get('/test_admin/admin/admin_views/plot/%s/delete/' % quote(3))
    10531053        self.assertContains(response, should_contain)
    10541054
     1055class AdminChangeListCSSClassesTest(TestCase):
     1056    fixtures = ['admin-views-users.xml']
     1057
     1058    def setUp(self):
     1059        self.client.login(username='super', password='secret')
     1060
     1061    def tearDown(self):
     1062        self.client.logout()
     1063
     1064    def test_class_attributes(self):
     1065        """
     1066        Cells of the change list table should contain the field name in their class attribute
     1067        Refs #11195.
     1068        """
     1069        Podcast.objects.create(name="Django Dose",
     1070            release_date=datetime.date.today())
     1071        response = self.client.get('/test_admin/admin/admin_views/podcast/')
     1072        self.assertContains(
     1073            response, '<th class="name-cell">')
     1074        self.assertContains(
     1075            response, '<td class="release_date-cell nowrap">')
     1076        self.assertContains(
     1077            response, '<td class="action-checkbox">')
     1078       
    10551079class AdminViewStringPrimaryKeyTest(TestCase):
    10561080    fixtures = ['admin-views-users.xml', 'string-primary-key.xml']
    10571081
    class AdminViewStringPrimaryKeyTest(TestCase): 
    10821106    def test_changelist_to_changeform_link(self):
    10831107        "The link from the changelist referring to the changeform of the object should be quoted"
    10841108        response = self.client.get('/test_admin/admin/admin_views/modelwithstringprimarykey/')
    1085         should_contain = """<th><a href="%s/">%s</a></th></tr>""" % (quote(self.pk), escape(self.pk))
     1109        should_contain = """<th class="__str__-cell"><a href="%s/">%s</a></th></tr>""" % (quote(self.pk), escape(self.pk))
    10861110        self.assertContains(response, should_contain)
    10871111
    10881112    def test_recentactions_link(self):
    class AdminViewListEditable(TestCase): 
    16911715        self.assertContains(response, 'id="id_form-0-id"', 1) # Only one hidden field, in a separate place than the table.
    16921716        self.assertContains(response, 'id="id_form-1-id"', 1)
    16931717        self.assertContains(response, '<div class="hiddenfields">\n<input type="hidden" name="form-0-id" value="%d" id="id_form-0-id" /><input type="hidden" name="form-1-id" value="%d" id="id_form-1-id" />\n</div>' % (story2.id, story1.id))
    1694         self.assertContains(response, '<td>%d</td>' % story1.id, 1)
    1695         self.assertContains(response, '<td>%d</td>' % story2.id, 1)
     1718        self.assertContains(response, '<td class="id-cell">%d</td>' % story1.id, 1)
     1719        self.assertContains(response, '<td class="id-cell">%d</td>' % story2.id, 1)
    16961720
    16971721    def test_pk_hidden_fields_with_list_display_links(self):
    16981722        """ Similarly as test_pk_hidden_fields, but when the hidden pk fields are
    class AdminViewListEditable(TestCase): 
    17051729        self.assertContains(response, 'id="id_form-0-id"', 1) # Only one hidden field, in a separate place than the table.
    17061730        self.assertContains(response, 'id="id_form-1-id"', 1)
    17071731        self.assertContains(response, '<div class="hiddenfields">\n<input type="hidden" name="form-0-id" value="%d" id="id_form-0-id" /><input type="hidden" name="form-1-id" value="%d" id="id_form-1-id" />\n</div>' % (story2.id, story1.id))
    1708         self.assertContains(response, '<th><a href="%d/">%d</a></th>' % (story1.id, story1.id), 1)
    1709         self.assertContains(response, '<th><a href="%d/">%d</a></th>' % (story2.id, story2.id), 1)
     1732        self.assertContains(response, '<th class="id-cell"><a href="%d/">%d</a></th>' % (story1.id, story1.id), 1)
     1733        self.assertContains(response, '<th class="id-cell"><a href="%d/">%d</a></th>' % (story2.id, story2.id), 1)
    17101734
    17111735
    17121736class AdminSearchTest(TestCase):
Back to Top