2009-05-14 15:09:33 +00:00
|
|
|
from django.contrib import admin
|
2010-12-12 22:58:47 +00:00
|
|
|
from django.contrib.admin.options import IncorrectLookupParameters
|
2009-05-14 15:09:33 +00:00
|
|
|
from django.contrib.admin.views.main import ChangeList
|
2010-12-21 14:57:29 +00:00
|
|
|
from django.core.paginator import Paginator
|
2010-03-01 18:43:27 +00:00
|
|
|
from django.template import Context, Template
|
2010-09-11 03:01:07 +00:00
|
|
|
from django.test import TransactionTestCase
|
2010-12-21 14:57:29 +00:00
|
|
|
|
2010-03-01 18:43:27 +00:00
|
|
|
from regressiontests.admin_changelist.models import Child, Parent
|
2009-05-14 15:09:33 +00:00
|
|
|
|
2010-12-21 14:57:29 +00:00
|
|
|
|
2010-09-11 03:01:07 +00:00
|
|
|
class ChangeListTests(TransactionTestCase):
|
2009-05-14 15:09:33 +00:00
|
|
|
def test_select_related_preserved(self):
|
|
|
|
"""
|
|
|
|
Regression test for #10348: ChangeList.get_query_set() shouldn't
|
|
|
|
overwrite a custom select_related provided by ModelAdmin.queryset().
|
|
|
|
"""
|
|
|
|
m = ChildAdmin(Child, admin.site)
|
2010-12-04 07:28:12 +00:00
|
|
|
cl = ChangeList(MockRequest(), Child, m.list_display, m.list_display_links,
|
|
|
|
m.list_filter, m.date_hierarchy, m.search_fields,
|
2010-12-21 19:19:04 +00:00
|
|
|
m.list_select_related, m.list_per_page, m.list_editable, m)
|
2009-05-14 15:09:33 +00:00
|
|
|
self.assertEqual(cl.query_set.query.select_related, {'parent': {'name': {}}})
|
|
|
|
|
2011-01-24 07:01:00 +00:00
|
|
|
def test_result_list_empty_changelist_value(self):
|
|
|
|
"""
|
|
|
|
Regression test for #14982: EMPTY_CHANGELIST_VALUE should be honored
|
|
|
|
for relationship fields
|
|
|
|
"""
|
|
|
|
new_child = Child.objects.create(name='name', parent=None)
|
|
|
|
request = MockRequest()
|
|
|
|
m = ChildAdmin(Child, admin.site)
|
|
|
|
cl = ChangeList(request, Child, m.list_display, m.list_display_links,
|
|
|
|
m.list_filter, m.date_hierarchy, m.search_fields,
|
|
|
|
m.list_select_related, m.list_per_page, m.list_editable, m)
|
|
|
|
cl.formset = None
|
|
|
|
template = Template('{% load admin_list %}{% spaceless %}{% result_list cl %}{% endspaceless %}')
|
|
|
|
context = Context({'cl': cl})
|
|
|
|
table_output = template.render(context)
|
|
|
|
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>(None)</td></tr></tbody>'
|
|
|
|
self.assertFalse(table_output.find(row_html) == -1,
|
|
|
|
'Failed to find expected row element: %s' % table_output)
|
|
|
|
|
|
|
|
|
2010-03-01 18:43:27 +00:00
|
|
|
def test_result_list_html(self):
|
|
|
|
"""
|
2010-09-11 03:01:07 +00:00
|
|
|
Verifies that inclusion tag result_list generates a table when with
|
|
|
|
default ModelAdmin settings.
|
2010-03-01 18:43:27 +00:00
|
|
|
"""
|
|
|
|
new_parent = Parent.objects.create(name='parent')
|
|
|
|
new_child = Child.objects.create(name='name', parent=new_parent)
|
|
|
|
request = MockRequest()
|
|
|
|
m = ChildAdmin(Child, admin.site)
|
2010-12-04 07:28:12 +00:00
|
|
|
cl = ChangeList(request, Child, m.list_display, m.list_display_links,
|
|
|
|
m.list_filter, m.date_hierarchy, m.search_fields,
|
2010-12-21 19:19:04 +00:00
|
|
|
m.list_select_related, m.list_per_page, m.list_editable, m)
|
2010-09-11 03:01:07 +00:00
|
|
|
cl.formset = None
|
2010-03-01 18:43:27 +00:00
|
|
|
template = Template('{% load admin_list %}{% spaceless %}{% result_list cl %}{% endspaceless %}')
|
|
|
|
context = Context({'cl': cl})
|
|
|
|
table_output = template.render(context)
|
2010-09-11 03:01:07 +00:00
|
|
|
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>'
|
2010-12-04 07:28:12 +00:00
|
|
|
self.assertFalse(table_output.find(row_html) == -1,
|
2010-09-11 03:01:07 +00:00
|
|
|
'Failed to find expected row element: %s' % table_output)
|
|
|
|
|
|
|
|
def test_result_list_editable_html(self):
|
|
|
|
"""
|
2010-12-04 07:28:12 +00:00
|
|
|
Regression tests for #11791: Inclusion tag result_list generates a
|
2010-09-11 03:01:07 +00:00
|
|
|
table and this checks that the items are nested within the table
|
|
|
|
element tags.
|
|
|
|
Also a regression test for #13599, verifies that hidden fields
|
2010-12-04 07:28:12 +00:00
|
|
|
when list_editable is enabled are rendered in a div outside the
|
2010-09-11 03:01:07 +00:00
|
|
|
table.
|
|
|
|
"""
|
|
|
|
new_parent = Parent.objects.create(name='parent')
|
|
|
|
new_child = Child.objects.create(name='name', parent=new_parent)
|
|
|
|
request = MockRequest()
|
|
|
|
m = ChildAdmin(Child, admin.site)
|
2010-03-01 18:43:27 +00:00
|
|
|
|
|
|
|
# Test with list_editable fields
|
|
|
|
m.list_display = ['id', 'name', 'parent']
|
|
|
|
m.list_display_links = ['id']
|
|
|
|
m.list_editable = ['name']
|
2010-12-04 07:28:12 +00:00
|
|
|
cl = ChangeList(request, Child, m.list_display, m.list_display_links,
|
|
|
|
m.list_filter, m.date_hierarchy, m.search_fields,
|
2010-12-21 19:19:04 +00:00
|
|
|
m.list_select_related, m.list_per_page, m.list_editable, m)
|
2010-03-01 18:43:27 +00:00
|
|
|
FormSet = m.get_changelist_formset(request)
|
|
|
|
cl.formset = FormSet(queryset=cl.result_list)
|
|
|
|
template = Template('{% load admin_list %}{% spaceless %}{% result_list cl %}{% endspaceless %}')
|
|
|
|
context = Context({'cl': cl})
|
|
|
|
table_output = template.render(context)
|
2010-09-11 03:01:07 +00:00
|
|
|
# make sure that hidden fields are in the correct place
|
|
|
|
hiddenfields_div = '<div class="hiddenfields"><input type="hidden" name="form-0-id" value="1" id="id_form-0-id" /></div>'
|
2010-12-04 07:28:12 +00:00
|
|
|
self.assertFalse(table_output.find(hiddenfields_div) == -1,
|
2010-09-11 03:01:07 +00:00
|
|
|
'Failed to find hidden fields in: %s' % table_output)
|
|
|
|
# make sure that list editable fields are rendered in divs correctly
|
|
|
|
editable_name_field = '<input name="form-0-name" value="name" class="vTextField" maxlength="30" type="text" id="id_form-0-name" />'
|
2010-12-04 07:28:12 +00:00
|
|
|
self.assertFalse('<td>%s</td>' % editable_name_field == -1,
|
2010-09-11 03:01:07 +00:00
|
|
|
'Failed to find "name" list_editable field in: %s' % table_output)
|
2010-03-01 18:43:27 +00:00
|
|
|
|
2010-12-12 22:58:47 +00:00
|
|
|
def test_result_list_editable(self):
|
|
|
|
"""
|
|
|
|
Regression test for #14312: list_editable with pagination
|
|
|
|
"""
|
|
|
|
|
|
|
|
new_parent = Parent.objects.create(name='parent')
|
|
|
|
for i in range(200):
|
|
|
|
new_child = Child.objects.create(name='name %s' % i, parent=new_parent)
|
|
|
|
request = MockRequest()
|
|
|
|
request.GET['p'] = -1 # Anything outside range
|
|
|
|
m = ChildAdmin(Child, admin.site)
|
|
|
|
|
|
|
|
# Test with list_editable fields
|
|
|
|
m.list_display = ['id', 'name', 'parent']
|
|
|
|
m.list_display_links = ['id']
|
|
|
|
m.list_editable = ['name']
|
|
|
|
self.assertRaises(IncorrectLookupParameters, lambda: \
|
|
|
|
ChangeList(request, Child, m.list_display, m.list_display_links,
|
|
|
|
m.list_filter, m.date_hierarchy, m.search_fields,
|
2010-12-21 19:19:04 +00:00
|
|
|
m.list_select_related, m.list_per_page, m.list_editable, m))
|
2010-12-21 14:57:29 +00:00
|
|
|
|
|
|
|
def test_custom_paginator(self):
|
|
|
|
new_parent = Parent.objects.create(name='parent')
|
|
|
|
for i in range(200):
|
|
|
|
new_child = Child.objects.create(name='name %s' % i, parent=new_parent)
|
|
|
|
|
|
|
|
request = MockRequest()
|
|
|
|
m = ChildAdmin(Child, admin.site)
|
|
|
|
m.list_display = ['id', 'name', 'parent']
|
|
|
|
m.list_display_links = ['id']
|
|
|
|
m.list_editable = ['name']
|
|
|
|
m.paginator = CustomPaginator
|
|
|
|
|
|
|
|
cl = ChangeList(request, Child, m.list_display, m.list_display_links,
|
|
|
|
m.list_filter, m.date_hierarchy, m.search_fields,
|
2010-12-21 19:19:04 +00:00
|
|
|
m.list_select_related, m.list_per_page, m.list_editable, m)
|
2010-12-21 14:57:29 +00:00
|
|
|
|
|
|
|
cl.get_results(request)
|
|
|
|
self.assertIsInstance(cl.paginator, CustomPaginator)
|
2010-12-12 22:58:47 +00:00
|
|
|
|
|
|
|
|
2009-05-14 15:09:33 +00:00
|
|
|
class ChildAdmin(admin.ModelAdmin):
|
|
|
|
list_display = ['name', 'parent']
|
|
|
|
def queryset(self, request):
|
|
|
|
return super(ChildAdmin, self).queryset(request).select_related("parent__name")
|
|
|
|
|
2010-12-21 14:57:29 +00:00
|
|
|
|
2009-05-14 15:09:33 +00:00
|
|
|
class MockRequest(object):
|
|
|
|
GET = {}
|
2010-12-21 14:57:29 +00:00
|
|
|
|
|
|
|
|
|
|
|
class CustomPaginator(Paginator):
|
|
|
|
def __init__(self, queryset, page_size, orphans=0, allow_empty_first_page=True):
|
2010-12-21 19:19:04 +00:00
|
|
|
super(CustomPaginator, self).__init__(queryset, 5, orphans=2,
|
2010-12-21 14:57:29 +00:00
|
|
|
allow_empty_first_page=allow_empty_first_page)
|