parts/django/tests/regressiontests/admin_changelist/tests.py
changeset 307 c6bca38c1cbf
equal deleted inserted replaced
306:5ff1fc726848 307:c6bca38c1cbf
       
     1 from django.contrib import admin
       
     2 from django.contrib.admin.options import IncorrectLookupParameters
       
     3 from django.contrib.admin.views.main import ChangeList
       
     4 from django.template import Context, Template
       
     5 from django.test import TransactionTestCase
       
     6 from regressiontests.admin_changelist.models import Child, Parent
       
     7 
       
     8 class ChangeListTests(TransactionTestCase):
       
     9     def test_select_related_preserved(self):
       
    10         """
       
    11         Regression test for #10348: ChangeList.get_query_set() shouldn't
       
    12         overwrite a custom select_related provided by ModelAdmin.queryset().
       
    13         """
       
    14         m = ChildAdmin(Child, admin.site)
       
    15         cl = ChangeList(MockRequest(), Child, m.list_display, m.list_display_links,
       
    16                 m.list_filter, m.date_hierarchy, m.search_fields,
       
    17                 m.list_select_related, m.list_per_page, m.list_editable, m)
       
    18         self.assertEqual(cl.query_set.query.select_related, {'parent': {'name': {}}})
       
    19 
       
    20     def test_result_list_html(self):
       
    21         """
       
    22         Verifies that inclusion tag result_list generates a table when with
       
    23         default ModelAdmin settings.
       
    24         """
       
    25         new_parent = Parent.objects.create(name='parent')
       
    26         new_child = Child.objects.create(name='name', parent=new_parent)
       
    27         request = MockRequest()
       
    28         m = ChildAdmin(Child, admin.site)
       
    29         cl = ChangeList(request, Child, m.list_display, m.list_display_links,
       
    30                 m.list_filter, m.date_hierarchy, m.search_fields,
       
    31                 m.list_select_related, m.list_per_page, m.list_editable, m)
       
    32         cl.formset = None
       
    33         template = Template('{% load admin_list %}{% spaceless %}{% result_list cl %}{% endspaceless %}')
       
    34         context = Context({'cl': cl})
       
    35         table_output = template.render(context)
       
    36         row_html = '<tbody><tr class="row1"><td><input type="checkbox" class="action-select" value="1" name="_selected_action" /></td><th><a href="1/">name</a></th><td>Parent object</td></tr></tbody>'
       
    37         self.assertFalse(table_output.find(row_html) == -1,
       
    38             'Failed to find expected row element: %s' % table_output)
       
    39 
       
    40     def test_result_list_editable_html(self):
       
    41         """
       
    42         Regression tests for #11791: Inclusion tag result_list generates a
       
    43         table and this checks that the items are nested within the table
       
    44         element tags.
       
    45         Also a regression test for #13599, verifies that hidden fields
       
    46         when list_editable is enabled are rendered in a div outside the
       
    47         table.
       
    48         """
       
    49         new_parent = Parent.objects.create(name='parent')
       
    50         new_child = Child.objects.create(name='name', parent=new_parent)
       
    51         request = MockRequest()
       
    52         m = ChildAdmin(Child, admin.site)
       
    53 
       
    54         # Test with list_editable fields
       
    55         m.list_display = ['id', 'name', 'parent']
       
    56         m.list_display_links = ['id']
       
    57         m.list_editable = ['name']
       
    58         cl = ChangeList(request, Child, m.list_display, m.list_display_links,
       
    59                 m.list_filter, m.date_hierarchy, m.search_fields,
       
    60                 m.list_select_related, m.list_per_page, m.list_editable, m)
       
    61         FormSet = m.get_changelist_formset(request)
       
    62         cl.formset = FormSet(queryset=cl.result_list)
       
    63         template = Template('{% load admin_list %}{% spaceless %}{% result_list cl %}{% endspaceless %}')
       
    64         context = Context({'cl': cl})
       
    65         table_output = template.render(context)
       
    66         # make sure that hidden fields are in the correct place
       
    67         hiddenfields_div = '<div class="hiddenfields"><input type="hidden" name="form-0-id" value="1" id="id_form-0-id" /></div>'
       
    68         self.assertFalse(table_output.find(hiddenfields_div) == -1,
       
    69             'Failed to find hidden fields in: %s' % table_output)
       
    70         # make sure that list editable fields are rendered in divs correctly
       
    71         editable_name_field = '<input name="form-0-name" value="name" class="vTextField" maxlength="30" type="text" id="id_form-0-name" />'
       
    72         self.assertFalse('<td>%s</td>' % editable_name_field == -1,
       
    73             'Failed to find "name" list_editable field in: %s' % table_output)
       
    74 
       
    75     def test_result_list_editable(self):
       
    76         """
       
    77         Regression test for #14312: list_editable with pagination
       
    78         """
       
    79 
       
    80         new_parent = Parent.objects.create(name='parent')
       
    81         for i in range(200):
       
    82             new_child = Child.objects.create(name='name %s' % i, parent=new_parent)
       
    83         request = MockRequest()
       
    84         request.GET['p'] = -1 # Anything outside range
       
    85         m = ChildAdmin(Child, admin.site)
       
    86 
       
    87         # Test with list_editable fields
       
    88         m.list_display = ['id', 'name', 'parent']
       
    89         m.list_display_links = ['id']
       
    90         m.list_editable = ['name']
       
    91         self.assertRaises(IncorrectLookupParameters, lambda: \
       
    92             ChangeList(request, Child, m.list_display, m.list_display_links,
       
    93                     m.list_filter, m.date_hierarchy, m.search_fields,
       
    94                     m.list_select_related, m.list_per_page, m.list_editable, m))
       
    95 
       
    96 
       
    97 class ChildAdmin(admin.ModelAdmin):
       
    98     list_display = ['name', 'parent']
       
    99     def queryset(self, request):
       
   100         return super(ChildAdmin, self).queryset(request).select_related("parent__name")
       
   101 
       
   102 class MockRequest(object):
       
   103     GET = {}