PageRenderTime 197ms CodeModel.GetById 144ms app.highlight 46ms RepoModel.GetById 1ms app.codeStats 1ms

/tests/regressiontests/admin_changelist/tests.py

https://code.google.com/p/mango-py/
Python | 351 lines | 307 code | 15 blank | 29 comment | 1 complexity | 8bfe154938b3502a6ae90241c9def877 MD5 | raw file
  1from django.contrib import admin
  2from django.contrib.admin.options import IncorrectLookupParameters
  3from django.contrib.admin.views.main import ChangeList, SEARCH_VAR
  4from django.core.paginator import Paginator
  5from django.template import Context, Template
  6from django.test import TransactionTestCase
  7
  8from models import (Child, Parent, Genre, Band, Musician, Group, Quartet,
  9    Membership, ChordsMusician, ChordsBand, Invitation)
 10
 11
 12class ChangeListTests(TransactionTestCase):
 13    def test_select_related_preserved(self):
 14        """
 15        Regression test for #10348: ChangeList.get_query_set() shouldn't
 16        overwrite a custom select_related provided by ModelAdmin.queryset().
 17        """
 18        m = ChildAdmin(Child, admin.site)
 19        cl = ChangeList(MockRequest(), Child, m.list_display, m.list_display_links,
 20                m.list_filter, m.date_hierarchy, m.search_fields,
 21                m.list_select_related, m.list_per_page, m.list_editable, m)
 22        self.assertEqual(cl.query_set.query.select_related, {'parent': {'name': {}}})
 23
 24    def test_result_list_empty_changelist_value(self):
 25        """
 26        Regression test for #14982: EMPTY_CHANGELIST_VALUE should be honored
 27        for relationship fields
 28        """
 29        new_child = Child.objects.create(name='name', parent=None)
 30        request = MockRequest()
 31        m = ChildAdmin(Child, admin.site)
 32        cl = ChangeList(request, Child, m.list_display, m.list_display_links,
 33                m.list_filter, m.date_hierarchy, m.search_fields,
 34                m.list_select_related, m.list_per_page, m.list_editable, m)
 35        cl.formset = None
 36        template = Template('{% load admin_list %}{% spaceless %}{% result_list cl %}{% endspaceless %}')
 37        context = Context({'cl': cl})
 38        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)
 40        self.assertFalse(table_output.find(row_html) == -1,
 41            'Failed to find expected row element: %s' % table_output)
 42
 43
 44    def test_result_list_html(self):
 45        """
 46        Verifies that inclusion tag result_list generates a table when with
 47        default ModelAdmin settings.
 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        cl = ChangeList(request, Child, m.list_display, m.list_display_links,
 54                m.list_filter, m.date_hierarchy, m.search_fields,
 55                m.list_select_related, m.list_per_page, m.list_editable, m)
 56        cl.formset = None
 57        template = Template('{% load admin_list %}{% spaceless %}{% result_list cl %}{% endspaceless %}')
 58        context = Context({'cl': cl})
 59        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)
 61        self.assertFalse(table_output.find(row_html) == -1,
 62            'Failed to find expected row element: %s' % table_output)
 63
 64    def test_result_list_editable_html(self):
 65        """
 66        Regression tests for #11791: Inclusion tag result_list generates a
 67        table and this checks that the items are nested within the table
 68        element tags.
 69        Also a regression test for #13599, verifies that hidden fields
 70        when list_editable is enabled are rendered in a div outside the
 71        table.
 72        """
 73        new_parent = Parent.objects.create(name='parent')
 74        new_child = Child.objects.create(name='name', parent=new_parent)
 75        request = MockRequest()
 76        m = ChildAdmin(Child, admin.site)
 77
 78        # Test with list_editable fields
 79        m.list_display = ['id', 'name', 'parent']
 80        m.list_display_links = ['id']
 81        m.list_editable = ['name']
 82        cl = ChangeList(request, Child, m.list_display, m.list_display_links,
 83                m.list_filter, m.date_hierarchy, m.search_fields,
 84                m.list_select_related, m.list_per_page, m.list_editable, m)
 85        FormSet = m.get_changelist_formset(request)
 86        cl.formset = FormSet(queryset=cl.result_list)
 87        template = Template('{% load admin_list %}{% spaceless %}{% result_list cl %}{% endspaceless %}')
 88        context = Context({'cl': cl})
 89        table_output = template.render(context)
 90        # make sure that hidden fields are in the correct place
 91        hiddenfields_div = '<div class="hiddenfields"><input type="hidden" name="form-0-id" value="%d" id="id_form-0-id" /></div>' % new_child.id
 92        self.assertFalse(table_output.find(hiddenfields_div) == -1,
 93            'Failed to find hidden fields in: %s' % table_output)
 94        # make sure that list editable fields are rendered in divs correctly
 95        editable_name_field = '<input name="form-0-name" value="name" class="vTextField" maxlength="30" type="text" id="id_form-0-name" />'
 96        self.assertFalse('<td>%s</td>' % editable_name_field == -1,
 97            'Failed to find "name" list_editable field in: %s' % table_output)
 98
 99    def test_result_list_editable(self):
100        """
101        Regression test for #14312: list_editable with pagination
102        """
103
104        new_parent = Parent.objects.create(name='parent')
105        for i in range(200):
106            new_child = Child.objects.create(name='name %s' % i, parent=new_parent)
107        request = MockRequest()
108        request.GET['p'] = -1 # Anything outside range
109        m = ChildAdmin(Child, admin.site)
110
111        # Test with list_editable fields
112        m.list_display = ['id', 'name', 'parent']
113        m.list_display_links = ['id']
114        m.list_editable = ['name']
115        self.assertRaises(IncorrectLookupParameters, lambda: \
116            ChangeList(request, Child, m.list_display, m.list_display_links,
117                    m.list_filter, m.date_hierarchy, m.search_fields,
118                    m.list_select_related, m.list_per_page, m.list_editable, m))
119
120    def test_custom_paginator(self):
121        new_parent = Parent.objects.create(name='parent')
122        for i in range(200):
123            new_child = Child.objects.create(name='name %s' % i, parent=new_parent)
124
125        request = MockRequest()
126        m = ChildAdmin(Child, admin.site)
127        m.list_display = ['id', 'name', 'parent']
128        m.list_display_links = ['id']
129        m.list_editable = ['name']
130        m.paginator = CustomPaginator
131
132        cl = ChangeList(request, Child, m.list_display, m.list_display_links,
133                m.list_filter, m.date_hierarchy, m.search_fields,
134                m.list_select_related, m.list_per_page, m.list_editable, m)
135
136        cl.get_results(request)
137        self.assertIsInstance(cl.paginator, CustomPaginator)
138
139    def test_distinct_for_m2m_in_list_filter(self):
140        """
141        Regression test for #13902: When using a ManyToMany in list_filter,
142        results shouldn't apper more than once. Basic ManyToMany.
143        """
144        blues = Genre.objects.create(name='Blues')
145        band = Band.objects.create(name='B.B. King Review', nr_of_members=11)
146
147        band.genres.add(blues)
148        band.genres.add(blues)
149
150        m = BandAdmin(Band, admin.site)
151        request = MockFilterRequest('genres', blues.pk)
152
153        cl = ChangeList(request, Band, m.list_display,
154                m.list_display_links, m.list_filter, m.date_hierarchy,
155                m.search_fields, m.list_select_related, m.list_per_page,
156                m.list_editable, m)
157
158        cl.get_results(request)
159
160        # There's only one Group instance
161        self.assertEqual(cl.result_count, 1)
162
163    def test_distinct_for_through_m2m_in_list_filter(self):
164        """
165        Regression test for #13902: When using a ManyToMany in list_filter,
166        results shouldn't apper more than once. With an intermediate model.
167        """
168        lead = Musician.objects.create(name='Vox')
169        band = Group.objects.create(name='The Hype')
170        Membership.objects.create(group=band, music=lead, role='lead voice')
171        Membership.objects.create(group=band, music=lead, role='bass player')
172
173        m = GroupAdmin(Group, admin.site)
174        request = MockFilterRequest('members', lead.pk)
175
176        cl = ChangeList(request, Group, m.list_display,
177                m.list_display_links, m.list_filter, m.date_hierarchy,
178                m.search_fields, m.list_select_related, m.list_per_page,
179                m.list_editable, m)
180
181        cl.get_results(request)
182
183        # There's only one Group instance
184        self.assertEqual(cl.result_count, 1)
185
186    def test_distinct_for_inherited_m2m_in_list_filter(self):
187        """
188        Regression test for #13902: When using a ManyToMany in list_filter,
189        results shouldn't apper more than once. Model managed in the
190        admin inherits from the one that defins the relationship.
191        """
192        lead = Musician.objects.create(name='John')
193        four = Quartet.objects.create(name='The Beatles')
194        Membership.objects.create(group=four, music=lead, role='lead voice')
195        Membership.objects.create(group=four, music=lead, role='guitar player')
196
197        m = QuartetAdmin(Quartet, admin.site)
198        request = MockFilterRequest('members', lead.pk)
199
200        cl = ChangeList(request, Quartet, m.list_display,
201                m.list_display_links, m.list_filter, m.date_hierarchy,
202                m.search_fields, m.list_select_related, m.list_per_page,
203                m.list_editable, m)
204
205        cl.get_results(request)
206
207        # There's only one Quartet instance
208        self.assertEqual(cl.result_count, 1)
209
210    def test_distinct_for_m2m_to_inherited_in_list_filter(self):
211        """
212        Regression test for #13902: When using a ManyToMany in list_filter,
213        results shouldn't apper more than once. Target of the relationship
214        inherits from another.
215        """
216        lead = ChordsMusician.objects.create(name='Player A')
217        three = ChordsBand.objects.create(name='The Chords Trio')
218        Invitation.objects.create(band=three, player=lead, instrument='guitar')
219        Invitation.objects.create(band=three, player=lead, instrument='bass')
220
221        m = ChordsBandAdmin(ChordsBand, admin.site)
222        request = MockFilterRequest('members', lead.pk)
223
224        cl = ChangeList(request, ChordsBand, m.list_display,
225                m.list_display_links, m.list_filter, m.date_hierarchy,
226                m.search_fields, m.list_select_related, m.list_per_page,
227                m.list_editable, m)
228
229        cl.get_results(request)
230
231        # There's only one ChordsBand instance
232        self.assertEqual(cl.result_count, 1)
233
234    def test_distinct_for_non_unique_related_object_in_list_filter(self):
235        """
236        Regressions tests for #15819: If a field listed in list_filters
237        is a non-unique related object, distinct() must be called.
238        """
239        parent = Parent.objects.create(name='Mary')
240        # Two children with the same name
241        Child.objects.create(parent=parent, name='Daniel')
242        Child.objects.create(parent=parent, name='Daniel')
243
244        m = ParentAdmin(Parent, admin.site)
245        request = MockFilterRequest('child__name', 'Daniel')
246
247        cl = ChangeList(request, Parent, m.list_display, m.list_display_links,
248                        m.list_filter, m.date_hierarchy, m.search_fields,
249                        m.list_select_related, m.list_per_page,
250                        m.list_editable, m)
251
252        # Make sure distinct() was called
253        self.assertEqual(cl.query_set.count(), 1)
254
255    def test_distinct_for_non_unique_related_object_in_search_fields(self):
256        """
257        Regressions tests for #15819: If a field listed in search_fields
258        is a non-unique related object, distinct() must be called.
259        """
260        parent = Parent.objects.create(name='Mary')
261        Child.objects.create(parent=parent, name='Danielle')
262        Child.objects.create(parent=parent, name='Daniel')
263
264        m = ParentAdmin(Parent, admin.site)
265        request = MockSearchRequest('daniel')
266
267        cl = ChangeList(request, Parent, m.list_display, m.list_display_links,
268                        m.list_filter, m.date_hierarchy, m.search_fields,
269                        m.list_select_related, m.list_per_page,
270                        m.list_editable, m)
271
272        # Make sure distinct() was called
273        self.assertEqual(cl.query_set.count(), 1)
274
275    def test_pagination(self):
276        """
277        Regression tests for #12893: Pagination in admins changelist doesn't
278        use queryset set by modeladmin.
279        """
280        parent = Parent.objects.create(name='anything')
281        for i in range(30):
282            Child.objects.create(name='name %s' % i, parent=parent)
283            Child.objects.create(name='filtered %s' % i, parent=parent)
284
285        request = MockRequest()
286
287        # Test default queryset
288        m = ChildAdmin(Child, admin.site)
289        cl = ChangeList(request, Child, m.list_display, m.list_display_links,
290                m.list_filter, m.date_hierarchy, m.search_fields,
291                m.list_select_related, m.list_per_page, m.list_editable, m)
292        self.assertEqual(cl.query_set.count(), 60)
293        self.assertEqual(cl.paginator.count, 60)
294        self.assertEqual(cl.paginator.page_range, [1, 2, 3, 4, 5, 6])
295
296        # Test custom queryset
297        m = FilteredChildAdmin(Child, admin.site)
298        cl = ChangeList(request, Child, m.list_display, m.list_display_links,
299                m.list_filter, m.date_hierarchy, m.search_fields,
300                m.list_select_related, m.list_per_page, m.list_editable, m)
301        self.assertEqual(cl.query_set.count(), 30)
302        self.assertEqual(cl.paginator.count, 30)
303        self.assertEqual(cl.paginator.page_range, [1, 2, 3])
304
305
306class ParentAdmin(admin.ModelAdmin):
307    list_filter = ['child__name']
308    search_fields = ['child__name']
309
310
311class ChildAdmin(admin.ModelAdmin):
312    list_display = ['name', 'parent']
313    list_per_page = 10
314    def queryset(self, request):
315        return super(ChildAdmin, self).queryset(request).select_related("parent__name")
316
317class FilteredChildAdmin(admin.ModelAdmin):
318    list_display = ['name', 'parent']
319    list_per_page = 10
320    def queryset(self, request):
321        return super(FilteredChildAdmin, self).queryset(request).filter(
322            name__contains='filtered')
323
324class MockRequest(object):
325    GET = {}
326
327class CustomPaginator(Paginator):
328    def __init__(self, queryset, page_size, orphans=0, allow_empty_first_page=True):
329        super(CustomPaginator, self).__init__(queryset, 5, orphans=2,
330            allow_empty_first_page=allow_empty_first_page)
331
332
333class BandAdmin(admin.ModelAdmin):
334    list_filter = ['genres']
335
336class GroupAdmin(admin.ModelAdmin):
337    list_filter = ['members']
338
339class QuartetAdmin(admin.ModelAdmin):
340    list_filter = ['members']
341
342class ChordsBandAdmin(admin.ModelAdmin):
343    list_filter = ['members']
344
345class MockFilterRequest(object):
346    def __init__(self, filter, q):
347        self.GET = {filter: q}
348
349class MockSearchRequest(object):
350    def __init__(self, q):
351        self.GET = {SEARCH_VAR: q}