2011-10-13 18:51:33 +00:00
|
|
|
from __future__ import absolute_import
|
|
|
|
|
2010-02-01 14:12:56 +00:00
|
|
|
from datetime import datetime
|
2010-01-09 21:28:54 +00:00
|
|
|
|
2010-03-10 08:37:17 +00:00
|
|
|
from django.conf import settings
|
2010-01-09 21:28:54 +00:00
|
|
|
from django.contrib import admin
|
2011-10-13 18:51:33 +00:00
|
|
|
from django.contrib.admin.util import (display_for_field, label_for_field,
|
|
|
|
lookup_field, NestedObjects)
|
2010-01-09 21:28:54 +00:00
|
|
|
from django.contrib.admin.views.main import EMPTY_CHANGELIST_VALUE
|
2010-02-01 14:12:56 +00:00
|
|
|
from django.contrib.sites.models import Site
|
2010-11-09 16:46:42 +00:00
|
|
|
from django.db import models, DEFAULT_DB_ALIAS
|
2010-10-11 12:55:17 +00:00
|
|
|
from django.test import TestCase
|
|
|
|
from django.utils import unittest
|
|
|
|
from django.utils.formats import localize
|
2010-02-26 13:17:43 +00:00
|
|
|
|
2011-10-13 18:51:33 +00:00
|
|
|
from .models import Article, Count, Event, Location
|
2010-02-26 13:17:43 +00:00
|
|
|
|
|
|
|
|
|
|
|
class NestedObjectsTests(TestCase):
|
|
|
|
"""
|
|
|
|
Tests for ``NestedObject`` utility collection.
|
|
|
|
|
|
|
|
"""
|
|
|
|
def setUp(self):
|
2010-11-09 16:46:42 +00:00
|
|
|
self.n = NestedObjects(using=DEFAULT_DB_ALIAS)
|
2010-02-26 13:17:43 +00:00
|
|
|
self.objs = [Count.objects.create(num=i) for i in range(5)]
|
|
|
|
|
|
|
|
def _check(self, target):
|
2011-03-03 15:04:39 +00:00
|
|
|
self.assertEqual(self.n.nested(lambda obj: obj.num), target)
|
2010-02-26 13:17:43 +00:00
|
|
|
|
2010-11-09 16:46:42 +00:00
|
|
|
def _connect(self, i, j):
|
|
|
|
self.objs[i].parent = self.objs[j]
|
|
|
|
self.objs[i].save()
|
2010-02-26 13:17:43 +00:00
|
|
|
|
2010-11-09 16:46:42 +00:00
|
|
|
def _collect(self, *indices):
|
|
|
|
self.n.collect([self.objs[i] for i in indices])
|
2010-02-26 13:17:43 +00:00
|
|
|
|
2010-11-09 16:46:42 +00:00
|
|
|
def test_unrelated_roots(self):
|
|
|
|
self._connect(2, 1)
|
|
|
|
self._collect(0)
|
|
|
|
self._collect(1)
|
2010-02-26 13:17:43 +00:00
|
|
|
self._check([0, 1, [2]])
|
|
|
|
|
|
|
|
def test_siblings(self):
|
2010-11-09 16:46:42 +00:00
|
|
|
self._connect(1, 0)
|
|
|
|
self._connect(2, 0)
|
|
|
|
self._collect(0)
|
2010-02-26 13:17:43 +00:00
|
|
|
self._check([0, [1, 2]])
|
|
|
|
|
|
|
|
def test_non_added_parent(self):
|
2010-11-09 16:46:42 +00:00
|
|
|
self._connect(0, 1)
|
|
|
|
self._collect(0)
|
2010-02-26 13:17:43 +00:00
|
|
|
self._check([0])
|
2010-01-09 21:28:54 +00:00
|
|
|
|
2010-02-26 13:17:43 +00:00
|
|
|
def test_cyclic(self):
|
2010-11-09 16:46:42 +00:00
|
|
|
self._connect(0, 2)
|
|
|
|
self._connect(1, 0)
|
|
|
|
self._connect(2, 1)
|
|
|
|
self._collect(0)
|
2010-02-26 13:17:43 +00:00
|
|
|
self._check([0, [1, [2]]])
|
2010-01-09 21:28:54 +00:00
|
|
|
|
2010-11-09 16:46:42 +00: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-09 21:28:54 +00:00
|
|
|
|
|
|
|
class UtilTests(unittest.TestCase):
|
2010-02-01 14:12:56 +00: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'
|
|
|
|
|
|
|
|
class MockModelAdmin(object):
|
|
|
|
def get_admin_value(self, obj):
|
|
|
|
return ADMIN_METHOD
|
|
|
|
|
|
|
|
simple_function = lambda obj: SIMPLE_FUNCTION
|
|
|
|
|
|
|
|
article = Article(
|
|
|
|
site=Site(domain=SITE_NAME),
|
|
|
|
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:
|
|
|
|
resolved_value = display_for_field(resolved_value, field)
|
|
|
|
|
|
|
|
self.assertEqual(value, resolved_value)
|
2010-01-10 03:36:59 +00:00
|
|
|
|
2010-01-09 21:28:54 +00:00
|
|
|
def test_null_display_for_field(self):
|
|
|
|
"""
|
|
|
|
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(
|
|
|
|
choices=(
|
|
|
|
(None, "test_none"),
|
|
|
|
)
|
|
|
|
))
|
|
|
|
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.TimeField())
|
|
|
|
self.assertEqual(display_value, EMPTY_CHANGELIST_VALUE)
|
|
|
|
|
2010-03-10 08:37:17 +00:00
|
|
|
# Regression test for #13071: NullBooleanField has special
|
|
|
|
# handling.
|
2010-01-09 21:28:54 +00:00
|
|
|
display_value = display_for_field(None, models.NullBooleanField())
|
2011-06-30 09:06:19 +00:00
|
|
|
expected = u'<img src="%sadmin/img/icon-unknown.gif" alt="None" />' % settings.STATIC_URL
|
2010-03-10 08:37:17 +00:00
|
|
|
self.assertEqual(display_value, expected)
|
2010-01-09 21:28:54 +00:00
|
|
|
|
|
|
|
display_value = display_for_field(None, models.DecimalField())
|
|
|
|
self.assertEqual(display_value, EMPTY_CHANGELIST_VALUE)
|
|
|
|
|
|
|
|
display_value = display_for_field(None, models.FloatField())
|
|
|
|
self.assertEqual(display_value, EMPTY_CHANGELIST_VALUE)
|
2010-01-10 03:36:59 +00:00
|
|
|
|
|
|
|
def test_label_for_field(self):
|
|
|
|
"""
|
|
|
|
Tests for label_for_field
|
|
|
|
"""
|
2011-03-03 15:04:39 +00:00
|
|
|
self.assertEqual(
|
2010-01-10 03:36:59 +00:00
|
|
|
label_for_field("title", Article),
|
|
|
|
"title"
|
|
|
|
)
|
2011-03-03 15:04:39 +00:00
|
|
|
self.assertEqual(
|
2010-01-10 03:36:59 +00:00
|
|
|
label_for_field("title2", Article),
|
|
|
|
"another name"
|
|
|
|
)
|
2011-03-03 15:04:39 +00:00
|
|
|
self.assertEqual(
|
2010-04-11 08:35:04 +00:00
|
|
|
label_for_field("title2", Article, return_attr=True),
|
2010-01-10 03:36:59 +00:00
|
|
|
("another name", None)
|
|
|
|
)
|
|
|
|
|
2011-03-03 15:04:39 +00:00
|
|
|
self.assertEqual(
|
2010-01-10 03:36:59 +00:00
|
|
|
label_for_field("__unicode__", Article),
|
|
|
|
"article"
|
|
|
|
)
|
2011-03-03 15:04:39 +00:00
|
|
|
self.assertEqual(
|
2010-01-10 03:36:59 +00:00
|
|
|
label_for_field("__str__", Article),
|
|
|
|
"article"
|
|
|
|
)
|
|
|
|
|
|
|
|
self.assertRaises(
|
|
|
|
AttributeError,
|
|
|
|
lambda: label_for_field("unknown", Article)
|
|
|
|
)
|
|
|
|
|
|
|
|
def test_callable(obj):
|
|
|
|
return "nothing"
|
2011-03-03 15:04:39 +00:00
|
|
|
self.assertEqual(
|
2010-01-10 03:36:59 +00:00
|
|
|
label_for_field(test_callable, Article),
|
2010-04-11 08:35:04 +00:00
|
|
|
"Test callable"
|
2010-01-10 03:36:59 +00:00
|
|
|
)
|
2011-03-03 15:04:39 +00:00
|
|
|
self.assertEqual(
|
2010-01-10 03:36:59 +00:00
|
|
|
label_for_field(test_callable, Article, return_attr=True),
|
2010-04-11 08:35:04 +00:00
|
|
|
("Test callable", test_callable)
|
2010-01-10 03:36:59 +00:00
|
|
|
)
|
|
|
|
|
2011-03-03 15:04:39 +00:00
|
|
|
self.assertEqual(
|
2010-01-10 03:36:59 +00:00
|
|
|
label_for_field("test_from_model", Article),
|
2010-04-11 08:35:04 +00:00
|
|
|
"Test from model"
|
2010-01-10 03:36:59 +00:00
|
|
|
)
|
2011-03-03 15:04:39 +00:00
|
|
|
self.assertEqual(
|
2010-01-10 03:36:59 +00:00
|
|
|
label_for_field("test_from_model", Article, return_attr=True),
|
2010-04-11 08:35:04 +00:00
|
|
|
("Test from model", Article.test_from_model)
|
2010-01-10 03:36:59 +00:00
|
|
|
)
|
2011-03-03 15:04:39 +00:00
|
|
|
self.assertEqual(
|
2010-01-10 03:36:59 +00:00
|
|
|
label_for_field("test_from_model_with_override", Article),
|
2010-04-11 08:35:04 +00:00
|
|
|
"not What you Expect"
|
2010-01-10 03:36:59 +00:00
|
|
|
)
|
|
|
|
|
2011-03-03 15:04:39 +00:00
|
|
|
self.assertEqual(
|
2010-01-10 03:36:59 +00:00
|
|
|
label_for_field(lambda x: "nothing", Article),
|
|
|
|
"--"
|
|
|
|
)
|
|
|
|
|
|
|
|
class MockModelAdmin(object):
|
|
|
|
def test_from_model(self, obj):
|
|
|
|
return "nothing"
|
2010-04-11 08:35:04 +00:00
|
|
|
test_from_model.short_description = "not Really the Model"
|
|
|
|
|
2011-03-03 15:04:39 +00:00
|
|
|
self.assertEqual(
|
2010-01-10 03:36:59 +00:00
|
|
|
label_for_field("test_from_model", Article, model_admin=MockModelAdmin),
|
2010-04-11 08:35:04 +00:00
|
|
|
"not Really the Model"
|
2010-01-10 03:36:59 +00:00
|
|
|
)
|
2011-03-03 15:04:39 +00:00
|
|
|
self.assertEqual(
|
2010-01-10 03:36:59 +00:00
|
|
|
label_for_field("test_from_model", Article,
|
|
|
|
model_admin = MockModelAdmin,
|
|
|
|
return_attr = True
|
|
|
|
),
|
2010-04-11 08:35:04 +00:00
|
|
|
("not Really the Model", MockModelAdmin.test_from_model)
|
2010-01-10 03:36:59 +00:00
|
|
|
)
|
2010-10-17 15:17:52 +00:00
|
|
|
|
|
|
|
def test_related_name(self):
|
|
|
|
"""
|
|
|
|
Regression test for #13963
|
|
|
|
"""
|
2011-03-03 15:04:39 +00:00
|
|
|
self.assertEqual(
|
2010-10-17 15:17:52 +00:00
|
|
|
label_for_field('location', Event, return_attr=True),
|
|
|
|
('location', None),
|
|
|
|
)
|
2011-03-03 15:04:39 +00:00
|
|
|
self.assertEqual(
|
2010-10-17 15:17:52 +00:00
|
|
|
label_for_field('event', Location, return_attr=True),
|
|
|
|
('awesome event', None),
|
|
|
|
)
|
2011-03-03 15:04:39 +00:00
|
|
|
self.assertEqual(
|
2010-10-17 15:17:52 +00:00
|
|
|
label_for_field('guest', Event, return_attr=True),
|
|
|
|
('awesome guest', None),
|
|
|
|
)
|
2011-04-28 14:54:09 +00:00
|
|
|
|
|
|
|
def test_logentry_unicode(self):
|
|
|
|
"""
|
|
|
|
Regression test for #15661
|
|
|
|
"""
|
|
|
|
log_entry = admin.models.LogEntry()
|
|
|
|
|
|
|
|
log_entry.action_flag = admin.models.ADDITION
|
|
|
|
self.assertTrue(
|
|
|
|
unicode(log_entry).startswith('Added ')
|
|
|
|
)
|
|
|
|
|
|
|
|
log_entry.action_flag = admin.models.CHANGE
|
|
|
|
self.assertTrue(
|
|
|
|
unicode(log_entry).startswith('Changed ')
|
|
|
|
)
|
|
|
|
|
|
|
|
log_entry.action_flag = admin.models.DELETION
|
|
|
|
self.assertTrue(
|
|
|
|
unicode(log_entry).startswith('Deleted ')
|
|
|
|
)
|