summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorLoek van Gent <loek@1procentclub.nl>2015-03-13 11:08:03 +0100
committerTim Graham <timograham@gmail.com>2015-06-04 21:06:26 -0400
commit0207bdd2d4157c542c981264c86706b78ca246e9 (patch)
tree3664db1ac08dda620077c14cfbb1c6da548b0529 /tests
parent40f0a84cb151669313faadf857aaddd18d39aaeb (diff)
Fixed #24474 -- Allowed configuring the admin's empty change list value.
Diffstat (limited to 'tests')
-rw-r--r--tests/admin_changelist/admin.py9
-rw-r--r--tests/admin_changelist/tests.py74
-rw-r--r--tests/admin_utils/tests.py42
3 files changed, 96 insertions, 29 deletions
diff --git a/tests/admin_changelist/admin.py b/tests/admin_changelist/admin.py
index 99deafcc62..c32082f78a 100644
--- a/tests/admin_changelist/admin.py
+++ b/tests/admin_changelist/admin.py
@@ -130,3 +130,12 @@ class DynamicSearchFieldsChildAdmin(admin.ModelAdmin):
search_fields = super(DynamicSearchFieldsChildAdmin, self).get_search_fields(request)
search_fields += ('age',)
return search_fields
+
+
+class EmptyValueChildAdmin(admin.ModelAdmin):
+ empty_value_display = '-empty-'
+ list_display = ('name', 'age_display', 'age')
+
+ def age_display(self, obj):
+ return obj.age
+ age_display.empty_value_display = '&dagger;'
diff --git a/tests/admin_changelist/tests.py b/tests/admin_changelist/tests.py
index ded3bfc154..86682581b9 100644
--- a/tests/admin_changelist/tests.py
+++ b/tests/admin_changelist/tests.py
@@ -20,9 +20,9 @@ from .admin import (
BandAdmin, ChildAdmin, ChordsBandAdmin, ConcertAdmin,
CustomPaginationAdmin, CustomPaginator, DynamicListDisplayChildAdmin,
DynamicListDisplayLinksChildAdmin, DynamicListFilterChildAdmin,
- DynamicSearchFieldsChildAdmin, FilteredChildAdmin, GroupAdmin,
- InvitationAdmin, NoListDisplayLinksParentAdmin, ParentAdmin, QuartetAdmin,
- SwallowAdmin, site as custom_site,
+ DynamicSearchFieldsChildAdmin, EmptyValueChildAdmin, FilteredChildAdmin,
+ GroupAdmin, InvitationAdmin, NoListDisplayLinksParentAdmin, ParentAdmin,
+ QuartetAdmin, SwallowAdmin, site as custom_site,
)
from .models import (
Band, Child, ChordsBand, ChordsMusician, Concert, CustomIdUser, Event,
@@ -109,14 +109,67 @@ class ChangeListTests(TestCase):
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)
+ 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_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-parent nowrap">-</td></tr></tbody>' % link
+ row_html = (
+ '<tbody><tr class="row1"><th class="field-name"><a href="%s">name</a></th>'
+ '<td class="field-age_display">&dagger;</td><td class="field-age">-empty-</td></tr></tbody>' % link
+ )
self.assertNotEqual(table_output.find(row_html), -1,
'Failed to find expected row element: %s' % table_output)
@@ -132,14 +185,17 @@ class ChangeListTests(TestCase):
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)
+ 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">Parent object</td></tr></tbody>' % link
+ 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
+ )
self.assertNotEqual(table_output.find(row_html), -1,
'Failed to find expected row element: %s' % table_output)
diff --git a/tests/admin_utils/tests.py b/tests/admin_utils/tests.py
index 499732cbbb..7296af7eb2 100644
--- a/tests/admin_utils/tests.py
+++ b/tests/admin_utils/tests.py
@@ -11,7 +11,6 @@ from django.contrib.admin.utils import (
NestedObjects, display_for_field, flatten, flatten_fieldsets,
label_for_field, lookup_field,
)
-from django.contrib.admin.views.main import EMPTY_CHANGELIST_VALUE
from django.db import DEFAULT_DB_ALIAS, models
from django.test import TestCase, override_settings
from django.utils import six
@@ -96,6 +95,9 @@ class NestedObjectsTests(TestCase):
class UtilsTests(TestCase):
+
+ empty_value = '-empty-'
+
def test_values_from_lookup_field(self):
"""
Regression test for #12654: lookup_field
@@ -136,7 +138,7 @@ class UtilsTests(TestCase):
field, attr, resolved_value = lookup_field(name, article, mock_admin)
if field is not None:
- resolved_value = display_for_field(resolved_value, field)
+ resolved_value = display_for_field(resolved_value, field, self.empty_value)
self.assertEqual(value, resolved_value)
@@ -145,53 +147,53 @@ class UtilsTests(TestCase):
Regression test for #12550: display_for_field should handle None
value.
"""
- display_value = display_for_field(None, models.CharField())
- self.assertEqual(display_value, EMPTY_CHANGELIST_VALUE)
+ display_value = display_for_field(None, models.CharField(), self.empty_value)
+ self.assertEqual(display_value, self.empty_value)
display_value = display_for_field(None, models.CharField(
choices=(
(None, "test_none"),
)
- ))
+ ), self.empty_value)
self.assertEqual(display_value, "test_none")
- display_value = display_for_field(None, models.DateField())
- self.assertEqual(display_value, EMPTY_CHANGELIST_VALUE)
+ display_value = display_for_field(None, models.DateField(), self.empty_value)
+ self.assertEqual(display_value, self.empty_value)
- display_value = display_for_field(None, models.TimeField())
- self.assertEqual(display_value, EMPTY_CHANGELIST_VALUE)
+ display_value = display_for_field(None, models.TimeField(), self.empty_value)
+ self.assertEqual(display_value, self.empty_value)
# Regression test for #13071: NullBooleanField has special
# handling.
- display_value = display_for_field(None, models.NullBooleanField())
+ display_value = display_for_field(None, models.NullBooleanField(), self.empty_value)
expected = '<img src="%sadmin/img/icon-unknown.gif" alt="None" />' % settings.STATIC_URL
self.assertHTMLEqual(display_value, expected)
- display_value = display_for_field(None, models.DecimalField())
- self.assertEqual(display_value, EMPTY_CHANGELIST_VALUE)
+ display_value = display_for_field(None, models.DecimalField(), self.empty_value)
+ self.assertEqual(display_value, self.empty_value)
- display_value = display_for_field(None, models.FloatField())
- self.assertEqual(display_value, EMPTY_CHANGELIST_VALUE)
+ display_value = display_for_field(None, models.FloatField(), self.empty_value)
+ self.assertEqual(display_value, self.empty_value)
def test_number_formats_display_for_field(self):
- display_value = display_for_field(12345.6789, models.FloatField())
+ display_value = display_for_field(12345.6789, models.FloatField(), self.empty_value)
self.assertEqual(display_value, '12345.6789')
- display_value = display_for_field(Decimal('12345.6789'), models.DecimalField())
+ display_value = display_for_field(Decimal('12345.6789'), models.DecimalField(), self.empty_value)
self.assertEqual(display_value, '12345.6789')
- display_value = display_for_field(12345, models.IntegerField())
+ display_value = display_for_field(12345, models.IntegerField(), self.empty_value)
self.assertEqual(display_value, '12345')
@override_settings(USE_L10N=True, USE_THOUSAND_SEPARATOR=True)
def test_number_formats_with_thousand_seperator_display_for_field(self):
- display_value = display_for_field(12345.6789, models.FloatField())
+ display_value = display_for_field(12345.6789, models.FloatField(), self.empty_value)
self.assertEqual(display_value, '12,345.6789')
- display_value = display_for_field(Decimal('12345.6789'), models.DecimalField())
+ display_value = display_for_field(Decimal('12345.6789'), models.DecimalField(), self.empty_value)
self.assertEqual(display_value, '12,345.6789')
- display_value = display_for_field(12345, models.IntegerField())
+ display_value = display_for_field(12345, models.IntegerField(), self.empty_value)
self.assertEqual(display_value, '12,345')
def test_label_for_field(self):