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

File 11195.changelist-cells-css-classes.2.diff, 7.8 KB (added by Julien Phalip, 13 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 ba3d132..a9db7aa 100644
    a b def items_for_result(cl, result, form):  
    170170    first = True
    171171    pk = cl.lookup_opts.pk.attname
    172172    for field_name in cl.list_display:
    173         row_class = ''
     173        row_classes = ['field-%s' % field_name]
    174174        try:
    175175            f, attr, value = lookup_field(field_name, result, cl.model_admin)
    176176        except (AttributeError, ObjectDoesNotExist):
    def items_for_result(cl, result, form):  
    178178        else:
    179179            if f is None:
    180180                if field_name == u'action_checkbox':
    181                     row_class = ' class="action-checkbox"'
     181                    row_classes = ['action-checkbox']
    182182                allow_tags = getattr(attr, 'allow_tags', False)
    183183                boolean = getattr(attr, 'boolean', False)
    184184                if boolean:
    def items_for_result(cl, result, form):  
    204204                if isinstance(f, models.DateField)\
    205205                or isinstance(f, models.TimeField)\
    206206                or isinstance(f, models.ForeignKey):
    207                     row_class = ' class="nowrap"'
     207                    row_classes.append('nowrap')
    208208        if force_unicode(result_repr) == '':
    209209            result_repr = mark_safe(' ')
     210        row_class = ' class="%s"' % ' '.join(row_classes)
    210211        # If list_display_links not defined, add the link tag to the first field
    211212        if (first and not cl.list_display_links) or field_name in cl.list_display_links:
    212213            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 9347dbf..27a7cc4 100644
    a b class ChangeListTests(TestCase):  
    4949        template = Template('{% load admin_list %}{% spaceless %}{% result_list cl %}{% endspaceless %}')
    5050        context = Context({'cl': cl})
    5151        table_output = template.render(context)
    52         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)
     52        row_html = '<tbody><tr class="row1"><td class="action-checkbox"><input type="checkbox" class="action-select" value="%d" name="_selected_action" /></td><th class="field-name"><a href="%d/">name</a></th><td class="field-parent nowrap">(None)</td></tr></tbody>' % (new_child.id, new_child.id)
    5353        self.assertFalse(table_output.find(row_html) == -1,
    5454            'Failed to find expected row element: %s' % table_output)
    5555
    class ChangeListTests(TestCase):  
    6969        template = Template('{% load admin_list %}{% spaceless %}{% result_list cl %}{% endspaceless %}')
    7070        context = Context({'cl': cl})
    7171        table_output = template.render(context)
    72         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)
     72        row_html = '<tbody><tr class="row1"><td class="action-checkbox"><input type="checkbox" class="action-select" value="%d" name="_selected_action" /></td><th class="field-name"><a href="%d/">name</a></th><td class="field-parent nowrap">Parent object</td></tr></tbody>' % (new_child.id, new_child.id)
    7373        self.assertFalse(table_output.find(row_html) == -1,
    7474            'Failed to find expected row element: %s' % table_output)
    7575
  • tests/regressiontests/admin_views/tests.py

    diff --git a/tests/regressiontests/admin_views/tests.py b/tests/regressiontests/admin_views/tests.py
    index b6e7b9e..ceda533 100644
    a b class AdminViewDeletedObjectsTest(TestCase):  
    11801180        response = self.client.get('/test_admin/admin/admin_views/plot/%s/delete/' % quote(3))
    11811181        self.assertContains(response, should_contain)
    11821182
     1183class AdminChangeListCSSClassesTest(TestCase):
     1184    urls = "regressiontests.admin_views.urls"
     1185    fixtures = ['admin-views-users.xml']
     1186
     1187    def setUp(self):
     1188        self.client.login(username='super', password='secret')
     1189
     1190    def tearDown(self):
     1191        self.client.logout()
     1192
     1193    def test_class_attributes(self):
     1194        """
     1195        Cells of the change list table should contain the field name in their class attribute
     1196        Refs #11195.
     1197        """
     1198        Podcast.objects.create(name="Django Dose",
     1199            release_date=datetime.date.today())
     1200        response = self.client.get('/test_admin/admin/admin_views/podcast/')
     1201        self.assertContains(
     1202            response, '<th class="field-name">')
     1203        self.assertContains(
     1204            response, '<td class="field-release_date nowrap">')
     1205        self.assertContains(
     1206            response, '<td class="action-checkbox">')
     1207
    11831208class AdminViewStringPrimaryKeyTest(TestCase):
    11841209    urls = "regressiontests.admin_views.urls"
    11851210    fixtures = ['admin-views-users.xml', 'string-primary-key.xml']
    class AdminViewStringPrimaryKeyTest(TestCase):  
    12111236    def test_changelist_to_changeform_link(self):
    12121237        "The link from the changelist referring to the changeform of the object should be quoted"
    12131238        response = self.client.get('/test_admin/admin/admin_views/modelwithstringprimarykey/')
    1214         should_contain = """<th><a href="%s/">%s</a></th></tr>""" % (quote(self.pk), escape(self.pk))
     1239        should_contain = """<th class="field-__str__"><a href="%s/">%s</a></th></tr>""" % (quote(self.pk), escape(self.pk))
    12151240        self.assertContains(response, should_contain)
    12161241
    12171242    def test_recentactions_link(self):
    class AdminViewListEditable(TestCase):  
    18231848        self.assertContains(response, 'id="id_form-0-id"', 1) # Only one hidden field, in a separate place than the table.
    18241849        self.assertContains(response, 'id="id_form-1-id"', 1)
    18251850        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))
    1826         self.assertContains(response, '<td>%d</td>' % story1.id, 1)
    1827         self.assertContains(response, '<td>%d</td>' % story2.id, 1)
     1851        self.assertContains(response, '<td class="field-id">%d</td>' % story1.id, 1)
     1852        self.assertContains(response, '<td class="field-id">%d</td>' % story2.id, 1)
    18281853
    18291854    def test_pk_hidden_fields_with_list_display_links(self):
    18301855        """ Similarly as test_pk_hidden_fields, but when the hidden pk fields are
    class AdminViewListEditable(TestCase):  
    18371862        self.assertContains(response, 'id="id_form-0-id"', 1) # Only one hidden field, in a separate place than the table.
    18381863        self.assertContains(response, 'id="id_form-1-id"', 1)
    18391864        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))
    1840         self.assertContains(response, '<th><a href="%d/">%d</a></th>' % (story1.id, story1.id), 1)
    1841         self.assertContains(response, '<th><a href="%d/">%d</a></th>' % (story2.id, story2.id), 1)
     1865        self.assertContains(response, '<th class="field-id"><a href="%d/">%d</a></th>' % (story1.id, story1.id), 1)
     1866        self.assertContains(response, '<th class="field-id"><a href="%d/">%d</a></th>' % (story2.id, story2.id), 1)
    18421867
    18431868
    18441869class AdminSearchTest(TestCase):
Back to Top