2013-07-29 17:19:04 +00:00
|
|
|
from __future__ import unicode_literals
|
2011-09-08 13:25:00 +00:00
|
|
|
|
2012-04-29 09:51:12 +00:00
|
|
|
import datetime
|
|
|
|
|
2009-05-14 15:09:33 +00:00
|
|
|
from django.contrib import admin
|
2015-02-05 18:25:59 +00:00
|
|
|
from django.contrib.admin.models import LogEntry
|
2010-12-12 22:58:47 +00:00
|
|
|
from django.contrib.admin.options import IncorrectLookupParameters
|
2013-05-29 13:23:08 +00:00
|
|
|
from django.contrib.admin.templatetags.admin_list import pagination
|
2014-02-22 10:36:15 +00:00
|
|
|
from django.contrib.admin.tests import AdminSeleniumWebDriverTestCase
|
2015-01-28 12:35:27 +00:00
|
|
|
from django.contrib.admin.views.main import ALL_VAR, SEARCH_VAR, ChangeList
|
2011-10-13 18:51:33 +00:00
|
|
|
from django.contrib.auth.models import User
|
2015-02-05 18:25:59 +00:00
|
|
|
from django.contrib.contenttypes.models import ContentType
|
2012-09-25 01:02:59 +00:00
|
|
|
from django.core.urlresolvers import reverse
|
2010-03-01 18:43:27 +00:00
|
|
|
from django.template import Context, Template
|
2014-02-22 10:36:15 +00:00
|
|
|
from django.test import TestCase, override_settings
|
2011-06-08 22:53:55 +00:00
|
|
|
from django.test.client import RequestFactory
|
2015-01-28 12:35:27 +00:00
|
|
|
from django.utils import formats, six
|
|
|
|
|
|
|
|
from .admin import (
|
2015-04-14 09:09:58 +00:00
|
|
|
BandAdmin, ChildAdmin, ChordsBandAdmin, ConcertAdmin,
|
|
|
|
CustomPaginationAdmin, CustomPaginator, DynamicListDisplayChildAdmin,
|
2015-01-28 12:35:27 +00:00
|
|
|
DynamicListDisplayLinksChildAdmin, DynamicListFilterChildAdmin,
|
2015-03-13 10:08:03 +00:00
|
|
|
DynamicSearchFieldsChildAdmin, EmptyValueChildAdmin, FilteredChildAdmin,
|
|
|
|
GroupAdmin, InvitationAdmin, NoListDisplayLinksParentAdmin, ParentAdmin,
|
|
|
|
QuartetAdmin, SwallowAdmin, site as custom_site,
|
2015-01-28 12:35:27 +00:00
|
|
|
)
|
|
|
|
from .models import (
|
2015-04-14 09:09:58 +00:00
|
|
|
Band, Child, ChordsBand, ChordsMusician, Concert, CustomIdUser, Event,
|
|
|
|
Genre, Group, Invitation, Membership, Musician, OrderedObject, Parent,
|
2015-05-27 15:43:22 +00:00
|
|
|
Quartet, Swallow, SwallowOneToOne, UnorderedObject,
|
2015-01-28 12:35:27 +00:00
|
|
|
)
|
2011-09-20 18:16:49 +00:00
|
|
|
|
2010-12-21 14:57:29 +00:00
|
|
|
|
2014-04-05 06:04:46 +00:00
|
|
|
@override_settings(ROOT_URLCONF="admin_changelist.urls")
|
2011-06-08 23:09:32 +00:00
|
|
|
class ChangeListTests(TestCase):
|
2011-09-20 18:16:49 +00:00
|
|
|
|
2011-06-08 22:53:55 +00:00
|
|
|
def setUp(self):
|
|
|
|
self.factory = RequestFactory()
|
|
|
|
|
2011-11-17 21:30:07 +00:00
|
|
|
def _create_superuser(self, username):
|
|
|
|
return User.objects.create(username=username, is_superuser=True)
|
|
|
|
|
|
|
|
def _mocked_authenticated_request(self, url, user):
|
|
|
|
request = self.factory.get(url)
|
|
|
|
request.user = user
|
|
|
|
return request
|
|
|
|
|
2009-05-14 15:09:33 +00:00
|
|
|
def test_select_related_preserved(self):
|
|
|
|
"""
|
2013-03-08 14:15:23 +00:00
|
|
|
Regression test for #10348: ChangeList.get_queryset() shouldn't
|
|
|
|
overwrite a custom select_related provided by ModelAdmin.get_queryset().
|
2009-05-14 15:09:33 +00:00
|
|
|
"""
|
2015-05-14 12:14:34 +00:00
|
|
|
m = ChildAdmin(Child, custom_site)
|
2011-06-08 22:53:55 +00:00
|
|
|
request = self.factory.get('/child/')
|
2015-03-16 10:00:16 +00:00
|
|
|
list_select_related = m.get_list_select_related(request)
|
2011-06-08 22:53:55 +00:00
|
|
|
cl = ChangeList(request, Child, m.list_display, m.list_display_links,
|
2013-06-04 21:35:11 +00:00
|
|
|
m.list_filter, m.date_hierarchy, m.search_fields,
|
2015-03-16 10:00:16 +00:00
|
|
|
list_select_related, m.list_per_page,
|
2013-06-04 21:35:11 +00:00
|
|
|
m.list_max_show_all, m.list_editable, m)
|
|
|
|
self.assertEqual(cl.queryset.query.select_related, {
|
|
|
|
'parent': {'name': {}}
|
|
|
|
})
|
|
|
|
|
|
|
|
def test_select_related_as_tuple(self):
|
2015-05-14 12:14:34 +00:00
|
|
|
ia = InvitationAdmin(Invitation, custom_site)
|
2013-06-04 21:35:11 +00:00
|
|
|
request = self.factory.get('/invitation/')
|
2015-03-16 10:00:16 +00:00
|
|
|
list_select_related = ia.get_list_select_related(request)
|
2013-06-04 21:35:11 +00:00
|
|
|
cl = ChangeList(request, Child, ia.list_display, ia.list_display_links,
|
|
|
|
ia.list_filter, ia.date_hierarchy, ia.search_fields,
|
2015-03-16 10:00:16 +00:00
|
|
|
list_select_related, ia.list_per_page,
|
2013-06-04 21:35:11 +00:00
|
|
|
ia.list_max_show_all, ia.list_editable, ia)
|
|
|
|
self.assertEqual(cl.queryset.query.select_related, {'player': {}})
|
|
|
|
|
|
|
|
def test_select_related_as_empty_tuple(self):
|
2015-05-14 12:14:34 +00:00
|
|
|
ia = InvitationAdmin(Invitation, custom_site)
|
2013-06-04 21:35:11 +00:00
|
|
|
ia.list_select_related = ()
|
|
|
|
request = self.factory.get('/invitation/')
|
2015-03-16 10:00:16 +00:00
|
|
|
list_select_related = ia.get_list_select_related(request)
|
2013-06-04 21:35:11 +00:00
|
|
|
cl = ChangeList(request, Child, ia.list_display, ia.list_display_links,
|
|
|
|
ia.list_filter, ia.date_hierarchy, ia.search_fields,
|
2015-03-16 10:00:16 +00:00
|
|
|
list_select_related, ia.list_per_page,
|
2013-06-04 21:35:11 +00:00
|
|
|
ia.list_max_show_all, ia.list_editable, ia)
|
|
|
|
self.assertEqual(cl.queryset.query.select_related, False)
|
|
|
|
|
2015-03-16 10:00:16 +00:00
|
|
|
def test_get_select_related_custom_method(self):
|
|
|
|
class GetListSelectRelatedAdmin(admin.ModelAdmin):
|
|
|
|
list_display = ('band', 'player')
|
|
|
|
|
|
|
|
def get_list_select_related(self, request):
|
|
|
|
return ('band', 'player')
|
|
|
|
|
2015-05-14 12:14:34 +00:00
|
|
|
ia = GetListSelectRelatedAdmin(Invitation, custom_site)
|
2015-03-16 10:00:16 +00:00
|
|
|
request = self.factory.get('/invitation/')
|
|
|
|
list_select_related = ia.get_list_select_related(request)
|
|
|
|
cl = ChangeList(request, Child, ia.list_display, ia.list_display_links,
|
|
|
|
ia.list_filter, ia.date_hierarchy, ia.search_fields,
|
|
|
|
list_select_related, ia.list_per_page,
|
|
|
|
ia.list_max_show_all, ia.list_editable, ia)
|
|
|
|
self.assertEqual(cl.queryset.query.select_related, {'player': {}, 'band': {}})
|
|
|
|
|
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)
|
2011-06-08 22:53:55 +00:00
|
|
|
request = self.factory.get('/child/')
|
2015-05-14 12:14:34 +00:00
|
|
|
m = ChildAdmin(Child, custom_site)
|
2011-10-26 12:19:18 +00:00
|
|
|
list_display = m.get_list_display(request)
|
|
|
|
list_display_links = m.get_list_display_links(request, list_display)
|
|
|
|
cl = ChangeList(request, Child, list_display, list_display_links,
|
2015-03-13 10:08:03 +00:00
|
|
|
m.list_filter, m.date_hierarchy, m.search_fields,
|
|
|
|
m.list_select_related, m.list_per_page, m.list_max_show_all, m.list_editable, m)
|
2011-01-24 07:01:00 +00:00
|
|
|
cl.formset = None
|
|
|
|
template = Template('{% load admin_list %}{% spaceless %}{% result_list cl %}{% endspaceless %}')
|
|
|
|
context = Context({'cl': cl})
|
|
|
|
table_output = template.render(context)
|
2012-09-25 01:02:59 +00:00
|
|
|
link = reverse('admin:admin_changelist_child_change', args=(new_child.id,))
|
2015-03-13 10:08:03 +00:00
|
|
|
row_html = (
|
|
|
|
'<tbody><tr class="row1"><th class="field-name"><a href="%s">name</a></th>'
|
|
|
|
'<td class="field-parent nowrap">-</td></tr></tbody>' % link
|
|
|
|
)
|
|
|
|
self.assertNotEqual(table_output.find(row_html), -1,
|
|
|
|
'Failed to find expected row element: %s' % table_output)
|
|
|
|
|
|
|
|
def test_result_list_set_empty_value_display_on_admin_site(self):
|
|
|
|
"""
|
|
|
|
Test that empty value display can be set on AdminSite
|
|
|
|
"""
|
|
|
|
new_child = Child.objects.create(name='name', parent=None)
|
|
|
|
request = self.factory.get('/child/')
|
|
|
|
# Set a new empty display value on AdminSite.
|
|
|
|
admin.site.empty_value_display = '???'
|
|
|
|
m = ChildAdmin(Child, admin.site)
|
|
|
|
list_display = m.get_list_display(request)
|
|
|
|
list_display_links = m.get_list_display_links(request, list_display)
|
|
|
|
cl = ChangeList(request, Child, list_display, list_display_links,
|
|
|
|
m.list_filter, m.date_hierarchy, m.search_fields,
|
|
|
|
m.list_select_related, m.list_per_page, m.list_max_show_all, 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)
|
|
|
|
link = reverse('admin:admin_changelist_child_change', args=(new_child.id,))
|
|
|
|
row_html = (
|
|
|
|
'<tbody><tr class="row1"><th class="field-name"><a href="%s">name</a></th>'
|
|
|
|
'<td class="field-parent nowrap">???</td></tr></tbody>' % link
|
|
|
|
)
|
|
|
|
self.assertNotEqual(table_output.find(row_html), -1,
|
|
|
|
'Failed to find expected row element: %s' % table_output)
|
|
|
|
|
|
|
|
def test_result_list_set_empty_value_display_in_model_admin(self):
|
|
|
|
"""
|
|
|
|
Test that empty value display can be set in ModelAdmin or individual fields.
|
|
|
|
"""
|
|
|
|
new_child = Child.objects.create(name='name', parent=None)
|
|
|
|
request = self.factory.get('/child/')
|
|
|
|
m = EmptyValueChildAdmin(Child, admin.site)
|
|
|
|
list_display = m.get_list_display(request)
|
|
|
|
list_display_links = m.get_list_display_links(request, list_display)
|
|
|
|
cl = ChangeList(request, Child, list_display, list_display_links,
|
|
|
|
m.list_filter, m.date_hierarchy, m.search_fields,
|
|
|
|
m.list_select_related, m.list_per_page, m.list_max_show_all, 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)
|
|
|
|
link = reverse('admin:admin_changelist_child_change', args=(new_child.id,))
|
|
|
|
row_html = (
|
|
|
|
'<tbody><tr class="row1"><th class="field-name"><a href="%s">name</a></th>'
|
|
|
|
'<td class="field-age_display">†</td><td class="field-age">-empty-</td></tr></tbody>' % link
|
|
|
|
)
|
2014-10-28 10:02:56 +00:00
|
|
|
self.assertNotEqual(table_output.find(row_html), -1,
|
2011-01-24 07:01:00 +00:00
|
|
|
'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)
|
2011-06-08 22:53:55 +00:00
|
|
|
request = self.factory.get('/child/')
|
2015-05-14 12:14:34 +00:00
|
|
|
m = ChildAdmin(Child, custom_site)
|
2011-10-26 12:19:18 +00:00
|
|
|
list_display = m.get_list_display(request)
|
|
|
|
list_display_links = m.get_list_display_links(request, list_display)
|
|
|
|
cl = ChangeList(request, Child, list_display, list_display_links,
|
2015-03-13 10:08:03 +00:00
|
|
|
m.list_filter, m.date_hierarchy, m.search_fields,
|
|
|
|
m.list_select_related, m.list_per_page, m.list_max_show_all, 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)
|
2012-09-25 01:02:59 +00:00
|
|
|
link = reverse('admin:admin_changelist_child_change', args=(new_child.id,))
|
2015-03-13 10:08:03 +00:00
|
|
|
row_html = (
|
|
|
|
'<tbody><tr class="row1"><th class="field-name"><a href="%s">name</a></th>'
|
|
|
|
'<td class="field-parent nowrap">Parent object</td></tr></tbody>' % link
|
|
|
|
)
|
2014-10-28 10:02:56 +00:00
|
|
|
self.assertNotEqual(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)
|
2011-06-08 22:53:55 +00:00
|
|
|
request = self.factory.get('/child/')
|
2015-05-14 12:14:34 +00:00
|
|
|
m = ChildAdmin(Child, custom_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,
|
2011-09-08 13:25:00 +00:00
|
|
|
m.list_select_related, m.list_per_page, m.list_max_show_all, 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
|
2011-03-09 23:46:28 +00:00
|
|
|
hiddenfields_div = '<div class="hiddenfields"><input type="hidden" name="form-0-id" value="%d" id="id_form-0-id" /></div>' % new_child.id
|
2012-09-28 07:20:01 +00:00
|
|
|
self.assertInHTML(hiddenfields_div, table_output, msg_prefix='Failed to find hidden fields')
|
|
|
|
|
2010-09-11 03:01:07 +00:00
|
|
|
# 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" />'
|
2013-07-28 02:50:02 +00:00
|
|
|
self.assertInHTML('<td class="field-name">%s</td>' % editable_name_field, table_output, msg_prefix='Failed to find "name" list_editable field')
|
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):
|
2013-09-08 15:05:16 +00:00
|
|
|
Child.objects.create(name='name %s' % i, parent=new_parent)
|
2011-06-08 22:53:55 +00:00
|
|
|
request = self.factory.get('/child/', data={'p': -1}) # Anything outside range
|
2015-05-14 12:14:34 +00:00
|
|
|
m = ChildAdmin(Child, custom_site)
|
2010-12-12 22:58:47 +00:00
|
|
|
|
|
|
|
# Test with list_editable fields
|
|
|
|
m.list_display = ['id', 'name', 'parent']
|
|
|
|
m.list_display_links = ['id']
|
|
|
|
m.list_editable = ['name']
|
2013-10-18 00:24:41 +00:00
|
|
|
self.assertRaises(IncorrectLookupParameters, lambda:
|
2010-12-12 22:58:47 +00:00
|
|
|
ChangeList(request, Child, m.list_display, m.list_display_links,
|
|
|
|
m.list_filter, m.date_hierarchy, m.search_fields,
|
2011-09-08 13:25:00 +00:00
|
|
|
m.list_select_related, m.list_per_page, m.list_max_show_all, 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):
|
2013-09-08 15:05:16 +00:00
|
|
|
Child.objects.create(name='name %s' % i, parent=new_parent)
|
2010-12-21 14:57:29 +00:00
|
|
|
|
2011-06-08 22:53:55 +00:00
|
|
|
request = self.factory.get('/child/')
|
2015-05-14 12:14:34 +00:00
|
|
|
m = CustomPaginationAdmin(Child, custom_site)
|
2010-12-21 14:57:29 +00:00
|
|
|
|
|
|
|
cl = ChangeList(request, Child, m.list_display, m.list_display_links,
|
|
|
|
m.list_filter, m.date_hierarchy, m.search_fields,
|
2011-09-08 13:25:00 +00:00
|
|
|
m.list_select_related, m.list_per_page, m.list_max_show_all, 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
|
|
|
|
2011-02-13 22:51:40 +00:00
|
|
|
def test_distinct_for_m2m_in_list_filter(self):
|
|
|
|
"""
|
|
|
|
Regression test for #13902: When using a ManyToMany in list_filter,
|
2014-03-02 14:25:53 +00:00
|
|
|
results shouldn't appear more than once. Basic ManyToMany.
|
2011-02-13 22:51:40 +00:00
|
|
|
"""
|
|
|
|
blues = Genre.objects.create(name='Blues')
|
|
|
|
band = Band.objects.create(name='B.B. King Review', nr_of_members=11)
|
|
|
|
|
|
|
|
band.genres.add(blues)
|
|
|
|
band.genres.add(blues)
|
|
|
|
|
2015-05-14 12:14:34 +00:00
|
|
|
m = BandAdmin(Band, custom_site)
|
2011-06-08 22:53:55 +00:00
|
|
|
request = self.factory.get('/band/', data={'genres': blues.pk})
|
2011-04-23 03:55:21 +00:00
|
|
|
|
|
|
|
cl = ChangeList(request, Band, m.list_display,
|
2011-02-13 22:51:40 +00:00
|
|
|
m.list_display_links, m.list_filter, m.date_hierarchy,
|
|
|
|
m.search_fields, m.list_select_related, m.list_per_page,
|
2011-09-08 13:25:00 +00:00
|
|
|
m.list_max_show_all, m.list_editable, m)
|
2011-02-13 22:51:40 +00:00
|
|
|
|
2011-04-23 03:55:21 +00:00
|
|
|
cl.get_results(request)
|
2011-02-13 22:51:40 +00:00
|
|
|
|
|
|
|
# There's only one Group instance
|
|
|
|
self.assertEqual(cl.result_count, 1)
|
|
|
|
|
|
|
|
def test_distinct_for_through_m2m_in_list_filter(self):
|
|
|
|
"""
|
|
|
|
Regression test for #13902: When using a ManyToMany in list_filter,
|
2014-03-02 14:25:53 +00:00
|
|
|
results shouldn't appear more than once. With an intermediate model.
|
2011-02-13 22:51:40 +00:00
|
|
|
"""
|
|
|
|
lead = Musician.objects.create(name='Vox')
|
|
|
|
band = Group.objects.create(name='The Hype')
|
|
|
|
Membership.objects.create(group=band, music=lead, role='lead voice')
|
|
|
|
Membership.objects.create(group=band, music=lead, role='bass player')
|
|
|
|
|
2015-05-14 12:14:34 +00:00
|
|
|
m = GroupAdmin(Group, custom_site)
|
2011-06-08 22:53:55 +00:00
|
|
|
request = self.factory.get('/group/', data={'members': lead.pk})
|
2011-04-23 03:55:21 +00:00
|
|
|
|
|
|
|
cl = ChangeList(request, Group, m.list_display,
|
2011-02-13 22:51:40 +00:00
|
|
|
m.list_display_links, m.list_filter, m.date_hierarchy,
|
|
|
|
m.search_fields, m.list_select_related, m.list_per_page,
|
2011-09-08 13:25:00 +00:00
|
|
|
m.list_max_show_all, m.list_editable, m)
|
2011-02-13 22:51:40 +00:00
|
|
|
|
2011-04-23 03:55:21 +00:00
|
|
|
cl.get_results(request)
|
2011-02-13 22:51:40 +00:00
|
|
|
|
|
|
|
# There's only one Group instance
|
|
|
|
self.assertEqual(cl.result_count, 1)
|
|
|
|
|
2015-04-14 09:09:58 +00:00
|
|
|
def test_distinct_for_through_m2m_at_second_level_in_list_filter(self):
|
|
|
|
"""
|
|
|
|
When using a ManyToMany in list_filter at the second level behind a
|
|
|
|
ForeignKey, distinct() must be called and results shouldn't appear more
|
|
|
|
than once.
|
|
|
|
"""
|
|
|
|
lead = Musician.objects.create(name='Vox')
|
|
|
|
band = Group.objects.create(name='The Hype')
|
|
|
|
Concert.objects.create(name='Woodstock', group=band)
|
|
|
|
Membership.objects.create(group=band, music=lead, role='lead voice')
|
|
|
|
Membership.objects.create(group=band, music=lead, role='bass player')
|
|
|
|
|
2015-05-14 12:14:34 +00:00
|
|
|
m = ConcertAdmin(Concert, custom_site)
|
2015-04-14 09:09:58 +00:00
|
|
|
request = self.factory.get('/concert/', data={'group__members': lead.pk})
|
|
|
|
|
|
|
|
cl = ChangeList(request, Concert, 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_max_show_all, m.list_editable, m)
|
|
|
|
|
|
|
|
cl.get_results(request)
|
|
|
|
|
|
|
|
# There's only one Concert instance
|
|
|
|
self.assertEqual(cl.result_count, 1)
|
|
|
|
|
2011-02-13 22:51:40 +00:00
|
|
|
def test_distinct_for_inherited_m2m_in_list_filter(self):
|
|
|
|
"""
|
|
|
|
Regression test for #13902: When using a ManyToMany in list_filter,
|
2014-03-02 14:25:53 +00:00
|
|
|
results shouldn't appear more than once. Model managed in the
|
2011-02-13 22:51:40 +00:00
|
|
|
admin inherits from the one that defins the relationship.
|
|
|
|
"""
|
|
|
|
lead = Musician.objects.create(name='John')
|
|
|
|
four = Quartet.objects.create(name='The Beatles')
|
|
|
|
Membership.objects.create(group=four, music=lead, role='lead voice')
|
|
|
|
Membership.objects.create(group=four, music=lead, role='guitar player')
|
|
|
|
|
2015-05-14 12:14:34 +00:00
|
|
|
m = QuartetAdmin(Quartet, custom_site)
|
2011-06-08 22:53:55 +00:00
|
|
|
request = self.factory.get('/quartet/', data={'members': lead.pk})
|
2011-04-23 03:55:21 +00:00
|
|
|
|
|
|
|
cl = ChangeList(request, Quartet, m.list_display,
|
2011-02-13 22:51:40 +00:00
|
|
|
m.list_display_links, m.list_filter, m.date_hierarchy,
|
|
|
|
m.search_fields, m.list_select_related, m.list_per_page,
|
2011-09-08 13:25:00 +00:00
|
|
|
m.list_max_show_all, m.list_editable, m)
|
2011-02-13 22:51:40 +00:00
|
|
|
|
2011-04-23 03:55:21 +00:00
|
|
|
cl.get_results(request)
|
2011-02-13 22:51:40 +00:00
|
|
|
|
|
|
|
# There's only one Quartet instance
|
|
|
|
self.assertEqual(cl.result_count, 1)
|
|
|
|
|
|
|
|
def test_distinct_for_m2m_to_inherited_in_list_filter(self):
|
|
|
|
"""
|
|
|
|
Regression test for #13902: When using a ManyToMany in list_filter,
|
2014-03-02 14:25:53 +00:00
|
|
|
results shouldn't appear more than once. Target of the relationship
|
2011-02-13 22:51:40 +00:00
|
|
|
inherits from another.
|
|
|
|
"""
|
|
|
|
lead = ChordsMusician.objects.create(name='Player A')
|
|
|
|
three = ChordsBand.objects.create(name='The Chords Trio')
|
|
|
|
Invitation.objects.create(band=three, player=lead, instrument='guitar')
|
|
|
|
Invitation.objects.create(band=three, player=lead, instrument='bass')
|
|
|
|
|
2015-05-14 12:14:34 +00:00
|
|
|
m = ChordsBandAdmin(ChordsBand, custom_site)
|
2011-06-08 22:53:55 +00:00
|
|
|
request = self.factory.get('/chordsband/', data={'members': lead.pk})
|
2011-04-23 03:55:21 +00:00
|
|
|
|
|
|
|
cl = ChangeList(request, ChordsBand, m.list_display,
|
2011-02-13 22:51:40 +00:00
|
|
|
m.list_display_links, m.list_filter, m.date_hierarchy,
|
|
|
|
m.search_fields, m.list_select_related, m.list_per_page,
|
2011-09-08 13:25:00 +00:00
|
|
|
m.list_max_show_all, m.list_editable, m)
|
2011-02-13 22:51:40 +00:00
|
|
|
|
2011-04-23 03:55:21 +00:00
|
|
|
cl.get_results(request)
|
2011-02-13 22:51:40 +00:00
|
|
|
|
|
|
|
# There's only one ChordsBand instance
|
|
|
|
self.assertEqual(cl.result_count, 1)
|
|
|
|
|
2011-04-23 03:55:21 +00:00
|
|
|
def test_distinct_for_non_unique_related_object_in_list_filter(self):
|
|
|
|
"""
|
|
|
|
Regressions tests for #15819: If a field listed in list_filters
|
|
|
|
is a non-unique related object, distinct() must be called.
|
|
|
|
"""
|
|
|
|
parent = Parent.objects.create(name='Mary')
|
|
|
|
# Two children with the same name
|
|
|
|
Child.objects.create(parent=parent, name='Daniel')
|
|
|
|
Child.objects.create(parent=parent, name='Daniel')
|
|
|
|
|
2015-05-14 12:14:34 +00:00
|
|
|
m = ParentAdmin(Parent, custom_site)
|
2011-06-08 22:53:55 +00:00
|
|
|
request = self.factory.get('/parent/', data={'child__name': 'Daniel'})
|
2011-04-23 03:55:21 +00:00
|
|
|
|
|
|
|
cl = ChangeList(request, Parent, m.list_display, m.list_display_links,
|
|
|
|
m.list_filter, m.date_hierarchy, m.search_fields,
|
|
|
|
m.list_select_related, m.list_per_page,
|
2011-09-08 13:25:00 +00:00
|
|
|
m.list_max_show_all, m.list_editable, m)
|
2011-04-23 03:55:21 +00:00
|
|
|
|
|
|
|
# Make sure distinct() was called
|
2013-03-08 14:15:23 +00:00
|
|
|
self.assertEqual(cl.queryset.count(), 1)
|
2011-04-23 03:55:21 +00:00
|
|
|
|
|
|
|
def test_distinct_for_non_unique_related_object_in_search_fields(self):
|
|
|
|
"""
|
|
|
|
Regressions tests for #15819: If a field listed in search_fields
|
|
|
|
is a non-unique related object, distinct() must be called.
|
|
|
|
"""
|
|
|
|
parent = Parent.objects.create(name='Mary')
|
|
|
|
Child.objects.create(parent=parent, name='Danielle')
|
|
|
|
Child.objects.create(parent=parent, name='Daniel')
|
|
|
|
|
2015-05-14 12:14:34 +00:00
|
|
|
m = ParentAdmin(Parent, custom_site)
|
2011-06-08 22:53:55 +00:00
|
|
|
request = self.factory.get('/parent/', data={SEARCH_VAR: 'daniel'})
|
2011-04-23 03:55:21 +00:00
|
|
|
|
|
|
|
cl = ChangeList(request, Parent, m.list_display, m.list_display_links,
|
|
|
|
m.list_filter, m.date_hierarchy, m.search_fields,
|
|
|
|
m.list_select_related, m.list_per_page,
|
2011-09-08 13:25:00 +00:00
|
|
|
m.list_max_show_all, m.list_editable, m)
|
2011-04-23 03:55:21 +00:00
|
|
|
|
|
|
|
# Make sure distinct() was called
|
2013-03-08 14:15:23 +00:00
|
|
|
self.assertEqual(cl.queryset.count(), 1)
|
2011-04-23 03:55:21 +00:00
|
|
|
|
2015-04-14 09:09:58 +00:00
|
|
|
def test_distinct_for_many_to_many_at_second_level_in_search_fields(self):
|
|
|
|
"""
|
|
|
|
When using a ManyToMany in search_fields at the second level behind a
|
|
|
|
ForeignKey, distinct() must be called and results shouldn't appear more
|
|
|
|
than once.
|
|
|
|
"""
|
|
|
|
lead = Musician.objects.create(name='Vox')
|
|
|
|
band = Group.objects.create(name='The Hype')
|
|
|
|
Concert.objects.create(name='Woodstock', group=band)
|
|
|
|
Membership.objects.create(group=band, music=lead, role='lead voice')
|
|
|
|
Membership.objects.create(group=band, music=lead, role='bass player')
|
|
|
|
|
2015-05-14 12:14:34 +00:00
|
|
|
m = ConcertAdmin(Concert, custom_site)
|
2015-04-14 09:09:58 +00:00
|
|
|
request = self.factory.get('/concert/', data={SEARCH_VAR: 'vox'})
|
|
|
|
|
|
|
|
cl = ChangeList(request, Concert, 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_max_show_all, m.list_editable, m)
|
|
|
|
|
|
|
|
# There's only one Concert instance
|
|
|
|
self.assertEqual(cl.queryset.count(), 1)
|
|
|
|
|
2011-02-19 08:37:46 +00:00
|
|
|
def test_pagination(self):
|
|
|
|
"""
|
|
|
|
Regression tests for #12893: Pagination in admins changelist doesn't
|
|
|
|
use queryset set by modeladmin.
|
|
|
|
"""
|
|
|
|
parent = Parent.objects.create(name='anything')
|
|
|
|
for i in range(30):
|
|
|
|
Child.objects.create(name='name %s' % i, parent=parent)
|
|
|
|
Child.objects.create(name='filtered %s' % i, parent=parent)
|
|
|
|
|
2011-06-08 22:53:55 +00:00
|
|
|
request = self.factory.get('/child/')
|
2011-02-19 08:37:46 +00:00
|
|
|
|
|
|
|
# Test default queryset
|
2015-05-14 12:14:34 +00:00
|
|
|
m = ChildAdmin(Child, custom_site)
|
2011-02-19 08:37:46 +00:00
|
|
|
cl = ChangeList(request, Child, m.list_display, m.list_display_links,
|
|
|
|
m.list_filter, m.date_hierarchy, m.search_fields,
|
2011-09-08 13:25:00 +00:00
|
|
|
m.list_select_related, m.list_per_page, m.list_max_show_all,
|
|
|
|
m.list_editable, m)
|
2013-03-08 14:15:23 +00:00
|
|
|
self.assertEqual(cl.queryset.count(), 60)
|
2011-02-19 08:37:46 +00:00
|
|
|
self.assertEqual(cl.paginator.count, 60)
|
2012-08-16 03:09:01 +00:00
|
|
|
self.assertEqual(list(cl.paginator.page_range), [1, 2, 3, 4, 5, 6])
|
2011-02-19 08:37:46 +00:00
|
|
|
|
|
|
|
# Test custom queryset
|
2015-05-14 12:14:34 +00:00
|
|
|
m = FilteredChildAdmin(Child, custom_site)
|
2011-02-19 08:37:46 +00:00
|
|
|
cl = ChangeList(request, Child, m.list_display, m.list_display_links,
|
|
|
|
m.list_filter, m.date_hierarchy, m.search_fields,
|
2011-09-08 13:25:00 +00:00
|
|
|
m.list_select_related, m.list_per_page, m.list_max_show_all,
|
|
|
|
m.list_editable, m)
|
2013-03-08 14:15:23 +00:00
|
|
|
self.assertEqual(cl.queryset.count(), 30)
|
2011-02-19 08:37:46 +00:00
|
|
|
self.assertEqual(cl.paginator.count, 30)
|
2012-08-16 03:09:01 +00:00
|
|
|
self.assertEqual(list(cl.paginator.page_range), [1, 2, 3])
|
2011-02-19 08:37:46 +00:00
|
|
|
|
2012-04-29 09:51:12 +00:00
|
|
|
def test_computed_list_display_localization(self):
|
|
|
|
"""
|
|
|
|
Regression test for #13196: output of functions should be localized
|
|
|
|
in the changelist.
|
|
|
|
"""
|
|
|
|
User.objects.create_superuser(
|
|
|
|
username='super', email='super@localhost', password='secret')
|
|
|
|
self.client.login(username='super', password='secret')
|
|
|
|
event = Event.objects.create(date=datetime.date.today())
|
2015-02-06 22:25:15 +00:00
|
|
|
response = self.client.get(reverse('admin:admin_changelist_event_changelist'))
|
2012-04-29 09:51:12 +00:00
|
|
|
self.assertContains(response, formats.localize(event.date))
|
2012-07-20 12:48:51 +00:00
|
|
|
self.assertNotContains(response, six.text_type(event.date))
|
2012-04-29 09:51:12 +00:00
|
|
|
|
2011-06-08 22:53:55 +00:00
|
|
|
def test_dynamic_list_display(self):
|
|
|
|
"""
|
|
|
|
Regression tests for #14206: dynamic list_display support.
|
|
|
|
"""
|
|
|
|
parent = Parent.objects.create(name='parent')
|
|
|
|
for i in range(10):
|
|
|
|
Child.objects.create(name='child %s' % i, parent=parent)
|
|
|
|
|
2011-11-17 21:30:07 +00:00
|
|
|
user_noparents = self._create_superuser('noparents')
|
|
|
|
user_parents = self._create_superuser('parents')
|
2011-06-08 22:53:55 +00:00
|
|
|
|
|
|
|
# Test with user 'noparents'
|
2011-09-20 18:16:49 +00:00
|
|
|
m = custom_site._registry[Child]
|
2011-11-17 21:30:07 +00:00
|
|
|
request = self._mocked_authenticated_request('/child/', user_noparents)
|
2011-06-08 22:53:55 +00:00
|
|
|
response = m.changelist_view(request)
|
|
|
|
self.assertNotContains(response, 'Parent object')
|
|
|
|
|
2011-10-26 12:19:18 +00:00
|
|
|
list_display = m.get_list_display(request)
|
|
|
|
list_display_links = m.get_list_display_links(request, list_display)
|
|
|
|
self.assertEqual(list_display, ['name', 'age'])
|
|
|
|
self.assertEqual(list_display_links, ['name'])
|
|
|
|
|
2011-06-08 22:53:55 +00:00
|
|
|
# Test with user 'parents'
|
2015-05-14 12:14:34 +00:00
|
|
|
m = DynamicListDisplayChildAdmin(Child, custom_site)
|
2011-11-17 21:30:07 +00:00
|
|
|
request = self._mocked_authenticated_request('/child/', user_parents)
|
2011-06-08 22:53:55 +00:00
|
|
|
response = m.changelist_view(request)
|
|
|
|
self.assertContains(response, 'Parent object')
|
|
|
|
|
2011-09-20 18:16:49 +00:00
|
|
|
custom_site.unregister(Child)
|
|
|
|
|
2011-10-26 12:19:18 +00:00
|
|
|
list_display = m.get_list_display(request)
|
|
|
|
list_display_links = m.get_list_display_links(request, list_display)
|
|
|
|
self.assertEqual(list_display, ('parent', 'name', 'age'))
|
|
|
|
self.assertEqual(list_display_links, ['parent'])
|
|
|
|
|
2011-06-08 22:53:55 +00:00
|
|
|
# Test default implementation
|
2011-09-20 18:16:49 +00:00
|
|
|
custom_site.register(Child, ChildAdmin)
|
|
|
|
m = custom_site._registry[Child]
|
2011-11-17 21:30:07 +00:00
|
|
|
request = self._mocked_authenticated_request('/child/', user_noparents)
|
2011-06-08 22:53:55 +00:00
|
|
|
response = m.changelist_view(request)
|
|
|
|
self.assertContains(response, 'Parent object')
|
|
|
|
|
2011-09-08 13:25:00 +00:00
|
|
|
def test_show_all(self):
|
|
|
|
parent = Parent.objects.create(name='anything')
|
|
|
|
for i in range(30):
|
|
|
|
Child.objects.create(name='name %s' % i, parent=parent)
|
|
|
|
Child.objects.create(name='filtered %s' % i, parent=parent)
|
|
|
|
|
|
|
|
# Add "show all" parameter to request
|
|
|
|
request = self.factory.get('/child/', data={ALL_VAR: ''})
|
|
|
|
|
|
|
|
# Test valid "show all" request (number of total objects is under max)
|
2015-05-14 12:14:34 +00:00
|
|
|
m = ChildAdmin(Child, custom_site)
|
2011-09-08 13:25:00 +00:00
|
|
|
# 200 is the max we'll pass to ChangeList
|
|
|
|
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, 200, m.list_editable, m)
|
|
|
|
cl.get_results(request)
|
|
|
|
self.assertEqual(len(cl.result_list), 60)
|
|
|
|
|
|
|
|
# Test invalid "show all" request (number of total objects over max)
|
|
|
|
# falls back to paginated pages
|
2015-05-14 12:14:34 +00:00
|
|
|
m = ChildAdmin(Child, custom_site)
|
2011-09-08 13:25:00 +00:00
|
|
|
# 30 is the max we'll pass to ChangeList for this test
|
|
|
|
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, 30, m.list_editable, m)
|
|
|
|
cl.get_results(request)
|
|
|
|
self.assertEqual(len(cl.result_list), 10)
|
|
|
|
|
2011-10-26 12:19:18 +00:00
|
|
|
def test_dynamic_list_display_links(self):
|
|
|
|
"""
|
|
|
|
Regression tests for #16257: dynamic list_display_links support.
|
|
|
|
"""
|
|
|
|
parent = Parent.objects.create(name='parent')
|
|
|
|
for i in range(1, 10):
|
|
|
|
Child.objects.create(id=i, name='child %s' % i, parent=parent, age=i)
|
|
|
|
|
2015-05-14 12:14:34 +00:00
|
|
|
m = DynamicListDisplayLinksChildAdmin(Child, custom_site)
|
2011-11-17 21:30:07 +00:00
|
|
|
superuser = self._create_superuser('superuser')
|
|
|
|
request = self._mocked_authenticated_request('/child/', superuser)
|
2011-10-26 12:19:18 +00:00
|
|
|
response = m.changelist_view(request)
|
|
|
|
for i in range(1, 10):
|
2012-09-25 01:02:59 +00:00
|
|
|
link = reverse('admin:admin_changelist_child_change', args=(i,))
|
|
|
|
self.assertContains(response, '<a href="%s">%s</a>' % (link, i))
|
2011-10-26 12:19:18 +00:00
|
|
|
|
|
|
|
list_display = m.get_list_display(request)
|
|
|
|
list_display_links = m.get_list_display_links(request, list_display)
|
|
|
|
self.assertEqual(list_display, ('parent', 'name', 'age'))
|
2011-11-17 21:30:07 +00:00
|
|
|
self.assertEqual(list_display_links, ['age'])
|
|
|
|
|
2013-09-06 18:25:13 +00:00
|
|
|
def test_no_list_display_links(self):
|
|
|
|
"""#15185 -- Allow no links from the 'change list' view grid."""
|
|
|
|
p = Parent.objects.create(name='parent')
|
2015-05-14 12:14:34 +00:00
|
|
|
m = NoListDisplayLinksParentAdmin(Parent, custom_site)
|
2013-09-06 18:25:13 +00:00
|
|
|
superuser = self._create_superuser('superuser')
|
|
|
|
request = self._mocked_authenticated_request('/parent/', superuser)
|
|
|
|
response = m.changelist_view(request)
|
|
|
|
link = reverse('admin:admin_changelist_parent_change', args=(p.pk,))
|
|
|
|
self.assertNotContains(response, '<a href="%s">' % link)
|
|
|
|
|
2011-11-17 21:30:07 +00:00
|
|
|
def test_tuple_list_display(self):
|
|
|
|
"""
|
|
|
|
Regression test for #17128
|
|
|
|
(ChangeList failing under Python 2.5 after r16319)
|
|
|
|
"""
|
2015-05-27 15:43:22 +00:00
|
|
|
swallow = Swallow.objects.create(origin='Africa', load='12.34', speed='22.2')
|
|
|
|
swallow2 = Swallow.objects.create(origin='Africa', load='12.34', speed='22.2')
|
|
|
|
swallow_o2o = SwallowOneToOne.objects.create(swallow=swallow2)
|
|
|
|
|
2015-05-14 12:14:34 +00:00
|
|
|
model_admin = SwallowAdmin(Swallow, custom_site)
|
2011-11-17 21:30:07 +00:00
|
|
|
superuser = self._create_superuser('superuser')
|
|
|
|
request = self._mocked_authenticated_request('/swallow/', superuser)
|
|
|
|
response = model_admin.changelist_view(request)
|
|
|
|
# just want to ensure it doesn't blow up during rendering
|
2012-07-20 12:48:51 +00:00
|
|
|
self.assertContains(response, six.text_type(swallow.origin))
|
|
|
|
self.assertContains(response, six.text_type(swallow.load))
|
|
|
|
self.assertContains(response, six.text_type(swallow.speed))
|
2015-05-27 15:43:22 +00:00
|
|
|
# Reverse one-to-one relations should work.
|
|
|
|
self.assertContains(response, '<td class="field-swallowonetoone">-</td>')
|
|
|
|
self.assertContains(response, '<td class="field-swallowonetoone">%s</td>' % swallow_o2o)
|
2011-11-17 21:30:07 +00:00
|
|
|
|
2012-03-03 02:13:35 +00:00
|
|
|
def test_deterministic_order_for_unordered_model(self):
|
|
|
|
"""
|
|
|
|
Ensure that the primary key is systematically used in the ordering of
|
|
|
|
the changelist's results to guarantee a deterministic order, even
|
|
|
|
when the Model doesn't have any default ordering defined.
|
|
|
|
Refs #17198.
|
|
|
|
"""
|
|
|
|
superuser = self._create_superuser('superuser')
|
|
|
|
|
|
|
|
for counter in range(1, 51):
|
|
|
|
UnorderedObject.objects.create(id=counter, bool=True)
|
|
|
|
|
|
|
|
class UnorderedObjectAdmin(admin.ModelAdmin):
|
|
|
|
list_per_page = 10
|
|
|
|
|
2012-04-09 04:28:32 +00:00
|
|
|
def check_results_order(ascending=False):
|
2015-05-14 12:14:34 +00:00
|
|
|
custom_site.register(UnorderedObject, UnorderedObjectAdmin)
|
|
|
|
model_admin = UnorderedObjectAdmin(UnorderedObject, custom_site)
|
2012-04-09 04:28:32 +00:00
|
|
|
counter = 0 if ascending else 51
|
2013-10-11 11:25:14 +00:00
|
|
|
for page in range(0, 5):
|
2012-03-03 02:13:35 +00:00
|
|
|
request = self._mocked_authenticated_request('/unorderedobject/?p=%s' % page, superuser)
|
|
|
|
response = model_admin.changelist_view(request)
|
|
|
|
for result in response.context_data['cl'].result_list:
|
2012-04-09 04:28:32 +00:00
|
|
|
counter += 1 if ascending else -1
|
2012-03-03 02:13:35 +00:00
|
|
|
self.assertEqual(result.id, counter)
|
2015-05-14 12:14:34 +00:00
|
|
|
custom_site.unregister(UnorderedObject)
|
2012-03-03 02:13:35 +00:00
|
|
|
|
2012-04-09 04:28:32 +00:00
|
|
|
# When no order is defined at all, everything is ordered by '-pk'.
|
2012-03-03 02:13:35 +00:00
|
|
|
check_results_order()
|
|
|
|
|
|
|
|
# When an order field is defined but multiple records have the same
|
2012-04-09 04:28:32 +00:00
|
|
|
# value for that field, make sure everything gets ordered by -pk as well.
|
2012-03-03 02:13:35 +00:00
|
|
|
UnorderedObjectAdmin.ordering = ['bool']
|
|
|
|
check_results_order()
|
|
|
|
|
|
|
|
# When order fields are defined, including the pk itself, use them.
|
|
|
|
UnorderedObjectAdmin.ordering = ['bool', '-pk']
|
|
|
|
check_results_order()
|
2012-04-09 04:28:32 +00:00
|
|
|
UnorderedObjectAdmin.ordering = ['bool', 'pk']
|
|
|
|
check_results_order(ascending=True)
|
2012-03-03 02:13:35 +00:00
|
|
|
UnorderedObjectAdmin.ordering = ['-id', 'bool']
|
|
|
|
check_results_order()
|
2012-04-09 04:28:32 +00:00
|
|
|
UnorderedObjectAdmin.ordering = ['id', 'bool']
|
|
|
|
check_results_order(ascending=True)
|
2012-03-03 02:13:35 +00:00
|
|
|
|
|
|
|
def test_deterministic_order_for_model_ordered_by_its_manager(self):
|
|
|
|
"""
|
|
|
|
Ensure that the primary key is systematically used in the ordering of
|
|
|
|
the changelist's results to guarantee a deterministic order, even
|
|
|
|
when the Model has a manager that defines a default ordering.
|
|
|
|
Refs #17198.
|
|
|
|
"""
|
|
|
|
superuser = self._create_superuser('superuser')
|
|
|
|
|
|
|
|
for counter in range(1, 51):
|
|
|
|
OrderedObject.objects.create(id=counter, bool=True, number=counter)
|
|
|
|
|
|
|
|
class OrderedObjectAdmin(admin.ModelAdmin):
|
|
|
|
list_per_page = 10
|
|
|
|
|
2012-04-09 04:28:32 +00:00
|
|
|
def check_results_order(ascending=False):
|
2015-05-14 12:14:34 +00:00
|
|
|
custom_site.register(OrderedObject, OrderedObjectAdmin)
|
|
|
|
model_admin = OrderedObjectAdmin(OrderedObject, custom_site)
|
2012-04-09 04:28:32 +00:00
|
|
|
counter = 0 if ascending else 51
|
2013-10-11 11:25:14 +00:00
|
|
|
for page in range(0, 5):
|
2012-03-03 02:13:35 +00:00
|
|
|
request = self._mocked_authenticated_request('/orderedobject/?p=%s' % page, superuser)
|
|
|
|
response = model_admin.changelist_view(request)
|
|
|
|
for result in response.context_data['cl'].result_list:
|
2012-04-09 04:28:32 +00:00
|
|
|
counter += 1 if ascending else -1
|
2012-03-03 02:13:35 +00:00
|
|
|
self.assertEqual(result.id, counter)
|
2015-05-14 12:14:34 +00:00
|
|
|
custom_site.unregister(OrderedObject)
|
2012-03-03 02:13:35 +00:00
|
|
|
|
2012-04-09 04:28:32 +00:00
|
|
|
# When no order is defined at all, use the model's default ordering (i.e. 'number')
|
|
|
|
check_results_order(ascending=True)
|
2012-03-03 02:13:35 +00:00
|
|
|
|
|
|
|
# When an order field is defined but multiple records have the same
|
2012-04-09 04:28:32 +00:00
|
|
|
# value for that field, make sure everything gets ordered by -pk as well.
|
2012-03-03 02:13:35 +00:00
|
|
|
OrderedObjectAdmin.ordering = ['bool']
|
|
|
|
check_results_order()
|
|
|
|
|
|
|
|
# When order fields are defined, including the pk itself, use them.
|
|
|
|
OrderedObjectAdmin.ordering = ['bool', '-pk']
|
|
|
|
check_results_order()
|
2012-04-09 04:28:32 +00:00
|
|
|
OrderedObjectAdmin.ordering = ['bool', 'pk']
|
|
|
|
check_results_order(ascending=True)
|
2012-03-03 02:13:35 +00:00
|
|
|
OrderedObjectAdmin.ordering = ['-id', 'bool']
|
2012-04-09 04:28:32 +00:00
|
|
|
check_results_order()
|
2012-03-03 02:13:35 +00:00
|
|
|
OrderedObjectAdmin.ordering = ['id', 'bool']
|
2012-04-29 09:51:12 +00:00
|
|
|
check_results_order(ascending=True)
|
2012-11-25 19:39:23 +00:00
|
|
|
|
|
|
|
def test_dynamic_list_filter(self):
|
|
|
|
"""
|
|
|
|
Regression tests for ticket #17646: dynamic list_filter support.
|
|
|
|
"""
|
|
|
|
parent = Parent.objects.create(name='parent')
|
|
|
|
for i in range(10):
|
|
|
|
Child.objects.create(name='child %s' % i, parent=parent)
|
|
|
|
|
|
|
|
user_noparents = self._create_superuser('noparents')
|
|
|
|
user_parents = self._create_superuser('parents')
|
|
|
|
|
|
|
|
# Test with user 'noparents'
|
2015-05-14 12:14:34 +00:00
|
|
|
m = DynamicListFilterChildAdmin(Child, custom_site)
|
2012-11-25 19:39:23 +00:00
|
|
|
request = self._mocked_authenticated_request('/child/', user_noparents)
|
|
|
|
response = m.changelist_view(request)
|
|
|
|
self.assertEqual(response.context_data['cl'].list_filter, ['name', 'age'])
|
|
|
|
|
|
|
|
# Test with user 'parents'
|
2015-05-14 12:14:34 +00:00
|
|
|
m = DynamicListFilterChildAdmin(Child, custom_site)
|
2012-11-25 19:39:23 +00:00
|
|
|
request = self._mocked_authenticated_request('/child/', user_parents)
|
|
|
|
response = m.changelist_view(request)
|
|
|
|
self.assertEqual(response.context_data['cl'].list_filter, ('parent', 'name', 'age'))
|
2013-03-19 22:17:31 +00:00
|
|
|
|
2013-08-03 16:15:15 +00:00
|
|
|
def test_dynamic_search_fields(self):
|
|
|
|
child = self._create_superuser('child')
|
2015-05-14 12:14:34 +00:00
|
|
|
m = DynamicSearchFieldsChildAdmin(Child, custom_site)
|
2013-08-03 16:15:15 +00:00
|
|
|
request = self._mocked_authenticated_request('/child/', child)
|
|
|
|
response = m.changelist_view(request)
|
|
|
|
self.assertEqual(response.context_data['cl'].search_fields, ('name', 'age'))
|
|
|
|
|
2013-05-29 13:23:08 +00:00
|
|
|
def test_pagination_page_range(self):
|
|
|
|
"""
|
|
|
|
Regression tests for ticket #15653: ensure the number of pages
|
|
|
|
generated for changelist views are correct.
|
|
|
|
"""
|
|
|
|
# instantiating and setting up ChangeList object
|
2015-05-14 12:14:34 +00:00
|
|
|
m = GroupAdmin(Group, custom_site)
|
2013-05-29 13:23:08 +00:00
|
|
|
request = self.factory.get('/group/')
|
|
|
|
cl = ChangeList(request, Group, 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_max_show_all, m.list_editable, m)
|
|
|
|
per_page = cl.list_per_page = 10
|
|
|
|
|
|
|
|
for page_num, objects_count, expected_page_range in [
|
|
|
|
(0, per_page, []),
|
2013-05-29 19:38:52 +00:00
|
|
|
(0, per_page * 2, list(range(2))),
|
2013-05-29 13:23:08 +00:00
|
|
|
(5, per_page * 11, list(range(11))),
|
|
|
|
(5, per_page * 12, [0, 1, 2, 3, 4, 5, 6, 7, 8, '.', 10, 11]),
|
|
|
|
(6, per_page * 12, [0, 1, '.', 3, 4, 5, 6, 7, 8, 9, 10, 11]),
|
|
|
|
(6, per_page * 13, [0, 1, '.', 3, 4, 5, 6, 7, 8, 9, '.', 11, 12]),
|
|
|
|
]:
|
|
|
|
# assuming we have exactly `objects_count` objects
|
|
|
|
Group.objects.all().delete()
|
|
|
|
for i in range(objects_count):
|
|
|
|
Group.objects.create(name='test band')
|
|
|
|
|
|
|
|
# setting page number and calculating page range
|
|
|
|
cl.page_num = page_num
|
|
|
|
cl.get_results(request)
|
|
|
|
real_page_range = pagination(cl)['page_range']
|
|
|
|
|
2013-05-29 19:38:52 +00:00
|
|
|
self.assertListEqual(
|
2013-05-29 13:23:08 +00:00
|
|
|
expected_page_range,
|
2013-05-29 19:38:52 +00:00
|
|
|
list(real_page_range),
|
2013-05-29 13:23:08 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
|
2013-03-19 22:17:31 +00:00
|
|
|
class AdminLogNodeTestCase(TestCase):
|
|
|
|
|
|
|
|
def test_get_admin_log_templatetag_custom_user(self):
|
|
|
|
"""
|
|
|
|
Regression test for ticket #20088: admin log depends on User model
|
|
|
|
having id field as primary key.
|
|
|
|
|
|
|
|
The old implementation raised an AttributeError when trying to use
|
|
|
|
the id field.
|
|
|
|
"""
|
|
|
|
context = Context({'user': CustomIdUser()})
|
|
|
|
template_string = '{% load log %}{% get_admin_log 10 as admin_log for_user user %}'
|
|
|
|
|
|
|
|
template = Template(template_string)
|
|
|
|
|
|
|
|
# Rendering should be u'' since this templatetag just logs,
|
|
|
|
# it doesn't render any string.
|
2013-03-20 02:16:48 +00:00
|
|
|
self.assertEqual(template.render(context), '')
|
2014-02-22 10:36:15 +00:00
|
|
|
|
2015-02-05 18:25:59 +00:00
|
|
|
def test_get_admin_log_templatetag_no_user(self):
|
|
|
|
"""
|
|
|
|
The {% get_admin_log %} tag should work without specifying a user.
|
|
|
|
"""
|
|
|
|
user = User(username='jondoe', password='secret', email='super@example.com')
|
|
|
|
user.save()
|
|
|
|
ct = ContentType.objects.get_for_model(User)
|
|
|
|
LogEntry.objects.log_action(user.pk, ct.pk, user.pk, repr(user), 1)
|
|
|
|
|
|
|
|
t = Template(
|
|
|
|
'{% load log %}'
|
|
|
|
'{% get_admin_log 100 as admin_log %}'
|
|
|
|
'{% for entry in admin_log %}'
|
|
|
|
'{{ entry|safe }}'
|
|
|
|
'{% endfor %}'
|
|
|
|
)
|
|
|
|
self.assertEqual(t.render(Context({})), 'Added "<User: jondoe>".')
|
|
|
|
|
2014-02-22 10:36:15 +00:00
|
|
|
|
2015-01-21 16:55:57 +00:00
|
|
|
@override_settings(PASSWORD_HASHERS=['django.contrib.auth.hashers.SHA1PasswordHasher'],
|
2014-04-05 06:04:46 +00:00
|
|
|
ROOT_URLCONF="admin_changelist.urls")
|
2014-02-22 10:36:15 +00:00
|
|
|
class SeleniumFirefoxTests(AdminSeleniumWebDriverTestCase):
|
|
|
|
|
|
|
|
available_apps = ['admin_changelist'] + AdminSeleniumWebDriverTestCase.available_apps
|
|
|
|
webdriver_class = 'selenium.webdriver.firefox.webdriver.WebDriver'
|
|
|
|
|
2015-03-05 13:21:29 +00:00
|
|
|
def setUp(self):
|
2015-02-23 00:53:57 +00:00
|
|
|
# password = "secret"
|
|
|
|
User.objects.create(
|
|
|
|
pk=100, username='super', first_name='Super', last_name='User', email='super@example.com',
|
|
|
|
password='sha1$995a3$6011485ea3834267d719b4c801409b8b1ddd0158', is_active=True, is_superuser=True,
|
|
|
|
is_staff=True, last_login=datetime.datetime(2007, 5, 30, 13, 20, 10),
|
|
|
|
date_joined=datetime.datetime(2007, 5, 30, 13, 20, 10)
|
|
|
|
)
|
|
|
|
|
2014-02-22 10:36:15 +00:00
|
|
|
def test_add_row_selection(self):
|
|
|
|
"""
|
|
|
|
Ensure that the status line for selected rows gets updated correcly (#22038)
|
|
|
|
"""
|
|
|
|
self.admin_login(username='super', password='secret')
|
|
|
|
self.selenium.get('%s%s' % (self.live_server_url,
|
2015-02-06 22:25:15 +00:00
|
|
|
reverse('admin:auth_user_changelist')))
|
2014-02-22 10:36:15 +00:00
|
|
|
|
|
|
|
form_id = '#changelist-form'
|
|
|
|
|
|
|
|
# Test amount of rows in the Changelist
|
|
|
|
rows = self.selenium.find_elements_by_css_selector(
|
|
|
|
'%s #result_list tbody tr' % form_id)
|
|
|
|
self.assertEqual(len(rows), 1)
|
|
|
|
|
|
|
|
# Test current selection
|
|
|
|
selection_indicator = self.selenium.find_element_by_css_selector(
|
|
|
|
'%s .action-counter' % form_id)
|
|
|
|
self.assertEqual(selection_indicator.text, "0 of 1 selected")
|
|
|
|
|
|
|
|
# Select a row and check again
|
|
|
|
row_selector = self.selenium.find_element_by_css_selector(
|
|
|
|
'%s #result_list tbody tr:first-child .action-select' % form_id)
|
|
|
|
row_selector.click()
|
|
|
|
self.assertEqual(selection_indicator.text, "1 of 1 selected")
|
|
|
|
|
|
|
|
|
|
|
|
class SeleniumChromeTests(SeleniumFirefoxTests):
|
|
|
|
webdriver_class = 'selenium.webdriver.chrome.webdriver.WebDriver'
|
|
|
|
|
|
|
|
|
|
|
|
class SeleniumIETests(SeleniumFirefoxTests):
|
|
|
|
webdriver_class = 'selenium.webdriver.ie.webdriver.WebDriver'
|