2010-02-01 22:12:56 +08:00
|
|
|
from datetime import datetime
|
2015-04-14 02:56:36 +08:00
|
|
|
from decimal import Decimal
|
2010-01-10 05:28:54 +08:00
|
|
|
|
2015-01-28 20:35:27 +08:00
|
|
|
from django import forms
|
2010-03-10 16:37:17 +08:00
|
|
|
from django.conf import settings
|
2012-02-04 20:48:21 +08:00
|
|
|
from django.contrib.admin import helpers
|
2015-01-28 20:35:27 +08:00
|
|
|
from django.contrib.admin.utils import (
|
2016-05-08 03:49:41 +08:00
|
|
|
NestedObjects, display_for_field, display_for_value, flatten,
|
2020-06-25 17:23:26 +08:00
|
|
|
flatten_fieldsets, help_text_for_field, label_for_field, lookup_field,
|
|
|
|
quote,
|
2015-01-28 20:35:27 +08:00
|
|
|
)
|
|
|
|
from django.db import DEFAULT_DB_ALIAS, models
|
2015-07-24 19:51:40 +08:00
|
|
|
from django.test import SimpleTestCase, TestCase, override_settings
|
2010-10-11 20:55:17 +08:00
|
|
|
from django.utils.formats import localize
|
2012-02-04 20:48:21 +08:00
|
|
|
from django.utils.safestring import mark_safe
|
2010-02-26 21:17:43 +08:00
|
|
|
|
2015-01-28 20:35:27 +08:00
|
|
|
from .models import (
|
|
|
|
Article, Car, Count, Event, EventGuide, Location, Site, Vehicle,
|
|
|
|
)
|
2010-02-26 21:17:43 +08:00
|
|
|
|
|
|
|
|
|
|
|
class NestedObjectsTests(TestCase):
|
|
|
|
"""
|
|
|
|
Tests for ``NestedObject`` utility collection.
|
|
|
|
"""
|
2018-11-24 10:24:25 +08:00
|
|
|
@classmethod
|
|
|
|
def setUpTestData(cls):
|
|
|
|
cls.n = NestedObjects(using=DEFAULT_DB_ALIAS)
|
|
|
|
cls.objs = [Count.objects.create(num=i) for i in range(5)]
|
2010-02-26 21:17:43 +08:00
|
|
|
|
|
|
|
def _check(self, target):
|
2011-03-03 23:04:39 +08:00
|
|
|
self.assertEqual(self.n.nested(lambda obj: obj.num), target)
|
2010-02-26 21:17:43 +08:00
|
|
|
|
2010-11-10 00:46:42 +08:00
|
|
|
def _connect(self, i, j):
|
|
|
|
self.objs[i].parent = self.objs[j]
|
|
|
|
self.objs[i].save()
|
2010-02-26 21:17:43 +08:00
|
|
|
|
2010-11-10 00:46:42 +08:00
|
|
|
def _collect(self, *indices):
|
|
|
|
self.n.collect([self.objs[i] for i in indices])
|
2010-02-26 21:17:43 +08:00
|
|
|
|
2010-11-10 00:46:42 +08:00
|
|
|
def test_unrelated_roots(self):
|
|
|
|
self._connect(2, 1)
|
|
|
|
self._collect(0)
|
|
|
|
self._collect(1)
|
2010-02-26 21:17:43 +08:00
|
|
|
self._check([0, 1, [2]])
|
|
|
|
|
|
|
|
def test_siblings(self):
|
2010-11-10 00:46:42 +08:00
|
|
|
self._connect(1, 0)
|
|
|
|
self._connect(2, 0)
|
|
|
|
self._collect(0)
|
2010-02-26 21:17:43 +08:00
|
|
|
self._check([0, [1, 2]])
|
|
|
|
|
|
|
|
def test_non_added_parent(self):
|
2010-11-10 00:46:42 +08:00
|
|
|
self._connect(0, 1)
|
|
|
|
self._collect(0)
|
2010-02-26 21:17:43 +08:00
|
|
|
self._check([0])
|
2010-01-10 05:28:54 +08:00
|
|
|
|
2010-02-26 21:17:43 +08:00
|
|
|
def test_cyclic(self):
|
2010-11-10 00:46:42 +08:00
|
|
|
self._connect(0, 2)
|
|
|
|
self._connect(1, 0)
|
|
|
|
self._connect(2, 1)
|
|
|
|
self._collect(0)
|
2010-02-26 21:17:43 +08:00
|
|
|
self._check([0, [1, [2]]])
|
2010-01-10 05:28:54 +08:00
|
|
|
|
2010-11-10 00:46:42 +08:00
|
|
|
def test_queries(self):
|
|
|
|
self._connect(1, 0)
|
|
|
|
self._connect(2, 0)
|
|
|
|
# 1 query to fetch all children of 0 (1 and 2)
|
|
|
|
# 1 query to fetch all children of 1 and 2 (none)
|
|
|
|
# Should not require additional queries to populate the nested graph.
|
|
|
|
self.assertNumQueries(2, self._collect, 0)
|
2010-01-10 05:28:54 +08:00
|
|
|
|
2012-09-20 23:51:30 +08:00
|
|
|
def test_on_delete_do_nothing(self):
|
|
|
|
"""
|
2016-10-27 15:53:39 +08:00
|
|
|
The nested collector doesn't query for DO_NOTHING objects.
|
2012-09-20 23:51:30 +08:00
|
|
|
"""
|
|
|
|
n = NestedObjects(using=DEFAULT_DB_ALIAS)
|
|
|
|
objs = [Event.objects.create()]
|
|
|
|
EventGuide.objects.create(event=objs[0])
|
|
|
|
with self.assertNumQueries(2):
|
|
|
|
# One for Location, one for Guest, and no query for EventGuide
|
|
|
|
n.collect(objs)
|
|
|
|
|
2014-01-22 01:25:33 +08:00
|
|
|
def test_relation_on_abstract(self):
|
|
|
|
"""
|
2016-10-27 15:53:39 +08:00
|
|
|
NestedObjects.collect() doesn't trip (AttributeError) on the special
|
|
|
|
notation for relations on abstract models (related_name that contains
|
|
|
|
%(app_label)s and/or %(class)s) (#21846).
|
2014-01-22 01:25:33 +08:00
|
|
|
"""
|
|
|
|
n = NestedObjects(using=DEFAULT_DB_ALIAS)
|
|
|
|
Car.objects.create()
|
|
|
|
n.collect([Vehicle.objects.first()])
|
|
|
|
|
2013-11-03 12:36:09 +08:00
|
|
|
|
2015-07-24 19:51:40 +08:00
|
|
|
class UtilsTests(SimpleTestCase):
|
2015-03-13 18:08:03 +08:00
|
|
|
|
|
|
|
empty_value = '-empty-'
|
|
|
|
|
2010-02-01 22:12:56 +08:00
|
|
|
def test_values_from_lookup_field(self):
|
|
|
|
"""
|
|
|
|
Regression test for #12654: lookup_field
|
|
|
|
"""
|
|
|
|
SITE_NAME = 'example.com'
|
|
|
|
TITLE_TEXT = 'Some title'
|
|
|
|
CREATED_DATE = datetime.min
|
|
|
|
ADMIN_METHOD = 'admin method'
|
|
|
|
SIMPLE_FUNCTION = 'function'
|
|
|
|
INSTANCE_ATTRIBUTE = 'attr'
|
|
|
|
|
2017-01-19 15:39:46 +08:00
|
|
|
class MockModelAdmin:
|
2010-02-01 22:12:56 +08:00
|
|
|
def get_admin_value(self, obj):
|
|
|
|
return ADMIN_METHOD
|
|
|
|
|
2016-01-24 00:47:07 +08:00
|
|
|
def simple_function(obj):
|
|
|
|
return SIMPLE_FUNCTION
|
2010-02-01 22:12:56 +08:00
|
|
|
|
2015-07-24 19:51:40 +08:00
|
|
|
site_obj = Site(domain=SITE_NAME)
|
2010-02-01 22:12:56 +08:00
|
|
|
article = Article(
|
2014-05-19 16:45:55 +08:00
|
|
|
site=site_obj,
|
2010-02-01 22:12:56 +08:00
|
|
|
title=TITLE_TEXT,
|
|
|
|
created=CREATED_DATE,
|
|
|
|
)
|
|
|
|
article.non_field = INSTANCE_ATTRIBUTE
|
|
|
|
|
|
|
|
verifications = (
|
|
|
|
('site', SITE_NAME),
|
|
|
|
('created', localize(CREATED_DATE)),
|
|
|
|
('title', TITLE_TEXT),
|
|
|
|
('get_admin_value', ADMIN_METHOD),
|
|
|
|
(simple_function, SIMPLE_FUNCTION),
|
|
|
|
('test_from_model', article.test_from_model()),
|
|
|
|
('non_field', INSTANCE_ATTRIBUTE)
|
|
|
|
)
|
|
|
|
|
|
|
|
mock_admin = MockModelAdmin()
|
|
|
|
for name, value in verifications:
|
|
|
|
field, attr, resolved_value = lookup_field(name, article, mock_admin)
|
|
|
|
|
|
|
|
if field is not None:
|
2015-03-13 18:08:03 +08:00
|
|
|
resolved_value = display_for_field(resolved_value, field, self.empty_value)
|
2010-02-01 22:12:56 +08:00
|
|
|
|
|
|
|
self.assertEqual(value, resolved_value)
|
2010-01-10 11:36:59 +08:00
|
|
|
|
2010-01-10 05:28:54 +08:00
|
|
|
def test_null_display_for_field(self):
|
|
|
|
"""
|
|
|
|
Regression test for #12550: display_for_field should handle None
|
|
|
|
value.
|
|
|
|
"""
|
2015-03-13 18:08:03 +08:00
|
|
|
display_value = display_for_field(None, models.CharField(), self.empty_value)
|
|
|
|
self.assertEqual(display_value, self.empty_value)
|
2010-01-10 05:28:54 +08:00
|
|
|
|
|
|
|
display_value = display_for_field(None, models.CharField(
|
|
|
|
choices=(
|
|
|
|
(None, "test_none"),
|
|
|
|
)
|
2015-03-13 18:08:03 +08:00
|
|
|
), self.empty_value)
|
2010-01-10 05:28:54 +08:00
|
|
|
self.assertEqual(display_value, "test_none")
|
|
|
|
|
2015-03-13 18:08:03 +08:00
|
|
|
display_value = display_for_field(None, models.DateField(), self.empty_value)
|
|
|
|
self.assertEqual(display_value, self.empty_value)
|
2010-01-10 05:28:54 +08:00
|
|
|
|
2015-03-13 18:08:03 +08:00
|
|
|
display_value = display_for_field(None, models.TimeField(), self.empty_value)
|
|
|
|
self.assertEqual(display_value, self.empty_value)
|
2010-01-10 05:28:54 +08:00
|
|
|
|
2010-03-10 16:37:17 +08:00
|
|
|
# Regression test for #13071: NullBooleanField has special
|
|
|
|
# handling.
|
2015-03-13 18:08:03 +08:00
|
|
|
display_value = display_for_field(None, models.NullBooleanField(), self.empty_value)
|
2018-01-21 15:09:10 +08:00
|
|
|
expected = '<img src="%sadmin/img/icon-unknown.svg" alt="None">' % settings.STATIC_URL
|
2013-05-25 21:18:48 +08:00
|
|
|
self.assertHTMLEqual(display_value, expected)
|
2010-01-10 05:28:54 +08:00
|
|
|
|
2017-05-06 22:56:28 +08:00
|
|
|
display_value = display_for_field(None, models.BooleanField(null=True), self.empty_value)
|
|
|
|
expected = '<img src="%sadmin/img/icon-unknown.svg" alt="None" />' % settings.STATIC_URL
|
|
|
|
self.assertHTMLEqual(display_value, expected)
|
|
|
|
|
2015-03-13 18:08:03 +08:00
|
|
|
display_value = display_for_field(None, models.DecimalField(), self.empty_value)
|
|
|
|
self.assertEqual(display_value, self.empty_value)
|
2010-01-10 05:28:54 +08:00
|
|
|
|
2015-03-13 18:08:03 +08:00
|
|
|
display_value = display_for_field(None, models.FloatField(), self.empty_value)
|
|
|
|
self.assertEqual(display_value, self.empty_value)
|
2020-05-08 14:44:33 +08:00
|
|
|
|
|
|
|
display_value = display_for_field(None, models.JSONField(), self.empty_value)
|
|
|
|
self.assertEqual(display_value, self.empty_value)
|
2010-01-10 11:36:59 +08:00
|
|
|
|
2020-05-08 15:25:54 +08:00
|
|
|
def test_json_display_for_field(self):
|
|
|
|
tests = [
|
|
|
|
({'a': {'b': 'c'}}, '{"a": {"b": "c"}}'),
|
|
|
|
(['a', 'b'], '["a", "b"]'),
|
|
|
|
('a', '"a"'),
|
|
|
|
({('a', 'b'): 'c'}, "{('a', 'b'): 'c'}"), # Invalid JSON.
|
|
|
|
]
|
|
|
|
for value, display_value in tests:
|
|
|
|
with self.subTest(value=value):
|
|
|
|
self.assertEqual(
|
|
|
|
display_for_field(value, models.JSONField(), self.empty_value),
|
|
|
|
display_value,
|
|
|
|
)
|
|
|
|
|
2015-04-14 02:56:36 +08:00
|
|
|
def test_number_formats_display_for_field(self):
|
2015-03-13 18:08:03 +08:00
|
|
|
display_value = display_for_field(12345.6789, models.FloatField(), self.empty_value)
|
2015-04-14 02:56:36 +08:00
|
|
|
self.assertEqual(display_value, '12345.6789')
|
|
|
|
|
2015-03-13 18:08:03 +08:00
|
|
|
display_value = display_for_field(Decimal('12345.6789'), models.DecimalField(), self.empty_value)
|
2015-04-14 02:56:36 +08:00
|
|
|
self.assertEqual(display_value, '12345.6789')
|
|
|
|
|
2015-03-13 18:08:03 +08:00
|
|
|
display_value = display_for_field(12345, models.IntegerField(), self.empty_value)
|
2015-04-15 05:09:27 +08:00
|
|
|
self.assertEqual(display_value, '12345')
|
|
|
|
|
2015-04-14 02:56:36 +08:00
|
|
|
@override_settings(USE_L10N=True, USE_THOUSAND_SEPARATOR=True)
|
2015-11-03 17:55:10 +08:00
|
|
|
def test_number_formats_with_thousand_separator_display_for_field(self):
|
2015-03-13 18:08:03 +08:00
|
|
|
display_value = display_for_field(12345.6789, models.FloatField(), self.empty_value)
|
2015-04-14 02:56:36 +08:00
|
|
|
self.assertEqual(display_value, '12,345.6789')
|
|
|
|
|
2015-03-13 18:08:03 +08:00
|
|
|
display_value = display_for_field(Decimal('12345.6789'), models.DecimalField(), self.empty_value)
|
2015-04-14 02:56:36 +08:00
|
|
|
self.assertEqual(display_value, '12,345.6789')
|
|
|
|
|
2015-03-13 18:08:03 +08:00
|
|
|
display_value = display_for_field(12345, models.IntegerField(), self.empty_value)
|
2015-04-15 05:09:27 +08:00
|
|
|
self.assertEqual(display_value, '12,345')
|
|
|
|
|
2016-05-08 03:49:41 +08:00
|
|
|
def test_list_display_for_value(self):
|
|
|
|
display_value = display_for_value([1, 2, 3], self.empty_value)
|
|
|
|
self.assertEqual(display_value, '1, 2, 3')
|
|
|
|
|
|
|
|
display_value = display_for_value([1, 2, 'buckle', 'my', 'shoe'], self.empty_value)
|
|
|
|
self.assertEqual(display_value, '1, 2, buckle, my, shoe')
|
|
|
|
|
2018-01-04 03:48:41 +08:00
|
|
|
@override_settings(USE_L10N=True, USE_THOUSAND_SEPARATOR=True)
|
|
|
|
def test_list_display_for_value_boolean(self):
|
|
|
|
self.assertEqual(
|
|
|
|
display_for_value(True, '', boolean=True),
|
2018-01-21 15:09:10 +08:00
|
|
|
'<img src="/static/admin/img/icon-yes.svg" alt="True">'
|
2018-01-04 03:48:41 +08:00
|
|
|
)
|
|
|
|
self.assertEqual(
|
|
|
|
display_for_value(False, '', boolean=True),
|
2018-01-21 15:09:10 +08:00
|
|
|
'<img src="/static/admin/img/icon-no.svg" alt="False">'
|
2018-01-04 03:48:41 +08:00
|
|
|
)
|
2018-01-04 01:04:57 +08:00
|
|
|
self.assertEqual(display_for_value(True, ''), 'True')
|
|
|
|
self.assertEqual(display_for_value(False, ''), 'False')
|
2018-01-04 03:48:41 +08:00
|
|
|
|
2010-01-10 11:36:59 +08:00
|
|
|
def test_label_for_field(self):
|
|
|
|
"""
|
|
|
|
Tests for label_for_field
|
|
|
|
"""
|
2011-03-03 23:04:39 +08:00
|
|
|
self.assertEqual(
|
2010-01-10 11:36:59 +08:00
|
|
|
label_for_field("title", Article),
|
|
|
|
"title"
|
|
|
|
)
|
2011-03-03 23:04:39 +08:00
|
|
|
self.assertEqual(
|
2015-12-27 02:51:22 +08:00
|
|
|
label_for_field("hist", Article),
|
|
|
|
"History"
|
2010-01-10 11:36:59 +08:00
|
|
|
)
|
2011-03-03 23:04:39 +08:00
|
|
|
self.assertEqual(
|
2015-12-27 02:51:22 +08:00
|
|
|
label_for_field("hist", Article, return_attr=True),
|
|
|
|
("History", None)
|
2010-01-10 11:36:59 +08:00
|
|
|
)
|
|
|
|
|
2011-03-03 23:04:39 +08:00
|
|
|
self.assertEqual(
|
2010-01-10 11:36:59 +08:00
|
|
|
label_for_field("__str__", Article),
|
2017-01-12 06:17:25 +08:00
|
|
|
"article"
|
2010-01-10 11:36:59 +08:00
|
|
|
)
|
|
|
|
|
2017-05-29 03:37:21 +08:00
|
|
|
with self.assertRaisesMessage(AttributeError, "Unable to lookup 'unknown' on Article"):
|
2016-01-17 19:26:39 +08:00
|
|
|
label_for_field("unknown", Article)
|
2010-01-10 11:36:59 +08:00
|
|
|
|
|
|
|
def test_callable(obj):
|
|
|
|
return "nothing"
|
2011-03-03 23:04:39 +08:00
|
|
|
self.assertEqual(
|
2010-01-10 11:36:59 +08:00
|
|
|
label_for_field(test_callable, Article),
|
2010-04-11 16:35:04 +08:00
|
|
|
"Test callable"
|
2010-01-10 11:36:59 +08:00
|
|
|
)
|
2011-03-03 23:04:39 +08:00
|
|
|
self.assertEqual(
|
2010-01-10 11:36:59 +08:00
|
|
|
label_for_field(test_callable, Article, return_attr=True),
|
2010-04-11 16:35:04 +08:00
|
|
|
("Test callable", test_callable)
|
2010-01-10 11:36:59 +08:00
|
|
|
)
|
|
|
|
|
2011-03-03 23:04:39 +08:00
|
|
|
self.assertEqual(
|
2010-01-10 11:36:59 +08:00
|
|
|
label_for_field("test_from_model", Article),
|
2010-04-11 16:35:04 +08:00
|
|
|
"Test from model"
|
2010-01-10 11:36:59 +08:00
|
|
|
)
|
2011-03-03 23:04:39 +08:00
|
|
|
self.assertEqual(
|
2010-01-10 11:36:59 +08:00
|
|
|
label_for_field("test_from_model", Article, return_attr=True),
|
2010-04-11 16:35:04 +08:00
|
|
|
("Test from model", Article.test_from_model)
|
2010-01-10 11:36:59 +08:00
|
|
|
)
|
2011-03-03 23:04:39 +08:00
|
|
|
self.assertEqual(
|
2010-01-10 11:36:59 +08:00
|
|
|
label_for_field("test_from_model_with_override", Article),
|
2010-04-11 16:35:04 +08:00
|
|
|
"not What you Expect"
|
2010-01-10 11:36:59 +08:00
|
|
|
)
|
|
|
|
|
2011-03-03 23:04:39 +08:00
|
|
|
self.assertEqual(
|
2010-01-10 11:36:59 +08:00
|
|
|
label_for_field(lambda x: "nothing", Article),
|
|
|
|
"--"
|
|
|
|
)
|
2016-04-24 01:35:18 +08:00
|
|
|
self.assertEqual(label_for_field('site_id', Article), 'Site id')
|
2010-01-10 11:36:59 +08:00
|
|
|
|
2017-01-19 15:39:46 +08:00
|
|
|
class MockModelAdmin:
|
2010-01-10 11:36:59 +08:00
|
|
|
def test_from_model(self, obj):
|
|
|
|
return "nothing"
|
2010-04-11 16:35:04 +08:00
|
|
|
test_from_model.short_description = "not Really the Model"
|
|
|
|
|
2011-03-03 23:04:39 +08:00
|
|
|
self.assertEqual(
|
2010-01-10 11:36:59 +08:00
|
|
|
label_for_field("test_from_model", Article, model_admin=MockModelAdmin),
|
2010-04-11 16:35:04 +08:00
|
|
|
"not Really the Model"
|
2010-01-10 11:36:59 +08:00
|
|
|
)
|
2011-03-03 23:04:39 +08:00
|
|
|
self.assertEqual(
|
2016-04-08 10:04:45 +08:00
|
|
|
label_for_field("test_from_model", Article, model_admin=MockModelAdmin, return_attr=True),
|
2010-04-11 16:35:04 +08:00
|
|
|
("not Really the Model", MockModelAdmin.test_from_model)
|
2010-01-10 11:36:59 +08:00
|
|
|
)
|
2010-10-17 23:17:52 +08:00
|
|
|
|
2018-08-19 04:15:18 +08:00
|
|
|
def test_label_for_field_form_argument(self):
|
|
|
|
class ArticleForm(forms.ModelForm):
|
|
|
|
extra_form_field = forms.BooleanField()
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
fields = '__all__'
|
|
|
|
model = Article
|
|
|
|
|
|
|
|
self.assertEqual(
|
|
|
|
label_for_field('extra_form_field', Article, form=ArticleForm()),
|
|
|
|
'Extra form field'
|
|
|
|
)
|
|
|
|
msg = "Unable to lookup 'nonexistent' on Article or ArticleForm"
|
|
|
|
with self.assertRaisesMessage(AttributeError, msg):
|
|
|
|
label_for_field('nonexistent', Article, form=ArticleForm()),
|
|
|
|
|
2013-05-21 19:03:45 +08:00
|
|
|
def test_label_for_property(self):
|
|
|
|
# NOTE: cannot use @property decorator, because of
|
|
|
|
# AttributeError: 'property' object has no attribute 'short_description'
|
2017-01-19 15:39:46 +08:00
|
|
|
class MockModelAdmin:
|
2013-05-21 19:03:45 +08:00
|
|
|
def my_property(self):
|
|
|
|
return "this if from property"
|
|
|
|
my_property.short_description = 'property short description'
|
|
|
|
test_from_property = property(my_property)
|
|
|
|
|
|
|
|
self.assertEqual(
|
|
|
|
label_for_field("test_from_property", Article, model_admin=MockModelAdmin),
|
|
|
|
'property short description'
|
|
|
|
)
|
|
|
|
|
2020-06-25 17:23:26 +08:00
|
|
|
def test_help_text_for_field(self):
|
|
|
|
tests = [
|
|
|
|
('article', ''),
|
|
|
|
('unknown', ''),
|
|
|
|
('hist', 'History help text'),
|
|
|
|
]
|
|
|
|
for name, help_text in tests:
|
|
|
|
with self.subTest(name=name):
|
|
|
|
self.assertEqual(help_text_for_field(name, Article), help_text)
|
|
|
|
|
2010-10-17 23:17:52 +08:00
|
|
|
def test_related_name(self):
|
|
|
|
"""
|
|
|
|
Regression test for #13963
|
|
|
|
"""
|
2011-03-03 23:04:39 +08:00
|
|
|
self.assertEqual(
|
2010-10-17 23:17:52 +08:00
|
|
|
label_for_field('location', Event, return_attr=True),
|
|
|
|
('location', None),
|
|
|
|
)
|
2011-03-03 23:04:39 +08:00
|
|
|
self.assertEqual(
|
2010-10-17 23:17:52 +08:00
|
|
|
label_for_field('event', Location, return_attr=True),
|
|
|
|
('awesome event', None),
|
|
|
|
)
|
2011-03-03 23:04:39 +08:00
|
|
|
self.assertEqual(
|
2010-10-17 23:17:52 +08:00
|
|
|
label_for_field('guest', Event, return_attr=True),
|
|
|
|
('awesome guest', None),
|
|
|
|
)
|
2011-04-28 22:54:09 +08:00
|
|
|
|
2012-02-04 20:48:21 +08:00
|
|
|
def test_safestring_in_field_label(self):
|
|
|
|
# safestring should not be escaped
|
|
|
|
class MyForm(forms.Form):
|
|
|
|
text = forms.CharField(label=mark_safe('<i>text</i>'))
|
2013-10-22 21:31:43 +08:00
|
|
|
cb = forms.BooleanField(label=mark_safe('<i>cb</i>'))
|
2012-02-04 20:48:21 +08:00
|
|
|
|
|
|
|
form = MyForm()
|
2013-05-25 21:18:48 +08:00
|
|
|
self.assertHTMLEqual(helpers.AdminField(form, 'text', is_first=False).label_tag(),
|
|
|
|
'<label for="id_text" class="required inline"><i>text</i>:</label>')
|
|
|
|
self.assertHTMLEqual(helpers.AdminField(form, 'cb', is_first=False).label_tag(),
|
2013-07-27 02:43:46 +08:00
|
|
|
'<label for="id_cb" class="vCheckboxLabel required inline"><i>cb</i></label>')
|
2012-02-04 20:48:21 +08:00
|
|
|
|
|
|
|
# normal strings needs to be escaped
|
|
|
|
class MyForm(forms.Form):
|
|
|
|
text = forms.CharField(label='&text')
|
2013-10-22 21:31:43 +08:00
|
|
|
cb = forms.BooleanField(label='&cb')
|
2012-02-04 20:48:21 +08:00
|
|
|
|
|
|
|
form = MyForm()
|
2013-05-25 21:18:48 +08:00
|
|
|
self.assertHTMLEqual(helpers.AdminField(form, 'text', is_first=False).label_tag(),
|
|
|
|
'<label for="id_text" class="required inline">&text:</label>')
|
|
|
|
self.assertHTMLEqual(helpers.AdminField(form, 'cb', is_first=False).label_tag(),
|
2013-07-27 02:43:46 +08:00
|
|
|
'<label for="id_cb" class="vCheckboxLabel required inline">&cb</label>')
|
2013-01-22 05:34:36 +08:00
|
|
|
|
2014-02-15 18:28:09 +08:00
|
|
|
def test_flatten(self):
|
|
|
|
flat_all = ['url', 'title', 'content', 'sites']
|
|
|
|
inputs = (
|
|
|
|
((), []),
|
|
|
|
(('url', 'title', ('content', 'sites')), flat_all),
|
|
|
|
(('url', 'title', 'content', 'sites'), flat_all),
|
|
|
|
((('url', 'title'), ('content', 'sites')), flat_all)
|
|
|
|
)
|
|
|
|
for orig, expected in inputs:
|
|
|
|
self.assertEqual(flatten(orig), expected)
|
|
|
|
|
2013-01-22 05:34:36 +08:00
|
|
|
def test_flatten_fieldsets(self):
|
|
|
|
"""
|
|
|
|
Regression test for #18051
|
|
|
|
"""
|
|
|
|
fieldsets = (
|
|
|
|
(None, {
|
|
|
|
'fields': ('url', 'title', ('content', 'sites'))
|
|
|
|
}),
|
|
|
|
)
|
|
|
|
self.assertEqual(flatten_fieldsets(fieldsets), ['url', 'title', 'content', 'sites'])
|
|
|
|
|
|
|
|
fieldsets = (
|
|
|
|
(None, {
|
2013-01-22 05:42:47 +08:00
|
|
|
'fields': ('url', 'title', ['content', 'sites'])
|
2013-01-22 05:34:36 +08:00
|
|
|
}),
|
|
|
|
)
|
|
|
|
self.assertEqual(flatten_fieldsets(fieldsets), ['url', 'title', 'content', 'sites'])
|
2015-06-19 03:46:15 +08:00
|
|
|
|
|
|
|
def test_quote(self):
|
|
|
|
self.assertEqual(quote('something\nor\nother'), 'something_0Aor_0Aother')
|