Ticket #7145: 7145_django_1.2.patch
File 7145_django_1.2.patch, 6.3 KB (added by , 14 years ago) |
---|
-
django/contrib/admin/templatetags/admin_list.py
diff --git a/django/contrib/admin/templatetags/admin_list.py b/django/contrib/admin/templatetags/admin_list.py index 565db32..d8d4262 100644
a b def date_hierarchy(cl): 246 246 'choices': [{ 247 247 'link': link({year_field: year_lookup, month_field: month_lookup, day_field: day.day}), 248 248 'title': capfirst(formats.date_format(day, 'MONTH_DAY_FORMAT')) 249 } for day in days ]249 } for day in days if day] 250 250 } 251 251 elif year_lookup: 252 252 months = cl.query_set.filter(**{year_field: year_lookup}).dates(field_name, 'month') … … def date_hierarchy(cl): 263 263 } 264 264 else: 265 265 years = cl.query_set.dates(field_name, 'year') 266 # Ignore ``None`` dates. 266 267 return { 267 268 'show': True, 268 269 'choices': [{ 269 270 'link': link({year_field: str(year.year)}), 270 271 'title': str(year.year), 271 } for year in years ]272 } for year in years if year] 272 273 } 273 274 date_hierarchy = register.inclusion_tag('admin/date_hierarchy.html')(date_hierarchy) 274 275 -
new file tests/regressiontests/admin_util/fixtures/admin_util_anotherarticles.json
diff --git a/tests/regressiontests/admin_util/fixtures/admin_util_anotherarticles.json b/tests/regressiontests/admin_util/fixtures/admin_util_anotherarticles.json new file mode 100644 index 0000000..4f15583
- + 1 [ 2 { 3 "pk": 1, 4 "model": "admin_util.anotherarticle", 5 "fields": { 6 "title": "Test 1", 7 "created": null 8 } 9 }, 10 { 11 "pk": 2, 12 "model": "admin_util.anotherarticle", 13 "fields": { 14 "title": "Test 2", 15 "created": "2010-01-21 10:58:32" 16 } 17 }, 18 { 19 "pk": 3, 20 "model": "admin_util.anotherarticle", 21 "fields": { 22 "title": "Test 3", 23 "created": "2010-10-12 10:58:32" 24 } 25 }, 26 { 27 "pk": 4, 28 "model": "admin_util.anotherarticle", 29 "fields": { 30 "title": "Test 3", 31 "created": "2010-10-13 10:58:32" 32 } 33 }, 34 { 35 "pk": 5, 36 "model": "admin_util.anotherarticle", 37 "fields": { 38 "title": "Test 4", 39 "created": "2011-01-21 10:58:32" 40 } 41 } 42 ] -
tests/regressiontests/admin_util/models.py
diff --git a/tests/regressiontests/admin_util/models.py b/tests/regressiontests/admin_util/models.py index 493e127..7f88fca 100644
a b class Article(models.Model): 20 20 21 21 class Count(models.Model): 22 22 num = models.PositiveSmallIntegerField() 23 24 25 class AnotherArticle(models.Model): 26 title = models.CharField(max_length=100) 27 created = models.DateTimeField(blank=True, null=True) 28 29 def __unicode__(self): 30 return self.title -
tests/regressiontests/admin_util/tests.py
diff --git a/tests/regressiontests/admin_util/tests.py b/tests/regressiontests/admin_util/tests.py index 5ea0ac5..37d6ea6 100644
a b from django.test import TestCase 8 8 9 9 from django.contrib import admin 10 10 from django.contrib.admin.util import display_for_field, label_for_field, lookup_field 11 from django.contrib.admin.views.main import EMPTY_CHANGELIST_VALUE 11 from django.contrib.admin.views.main import EMPTY_CHANGELIST_VALUE, ChangeList 12 12 from django.contrib.sites.models import Site 13 13 from django.contrib.admin.util import NestedObjects 14 from django.contrib.admin.templatetags.admin_list import date_hierarchy 14 15 15 from models import Article, Count 16 from models import Article, Count, AnotherArticle 16 17 17 18 18 19 class NestedObjectsTests(TestCase): … … class UtilTests(unittest.TestCase): 220 221 ), 221 222 ("not Really the Model", MockModelAdmin.test_from_model) 222 223 ) 224 225 226 class MockRequest(object): 227 pass 228 229 230 class AnotherArticleAdmin(admin.ModelAdmin): 231 list_display = ['title', 'created'] 232 date_hierarchy = 'created' 233 234 235 class DateHierarchyTestCase(TestCase): 236 fixtures = ['admin_util_anotherarticles.json'] 237 238 def test_nullable_date_hierarchy(self): 239 """ 240 Tests to ensure that the ``date_hierarchy`` tag doesn't blow up on 241 nullable dates. 242 """ 243 mock_admin = AnotherArticleAdmin(AnotherArticle, None) 244 mock_request = MockRequest() 245 246 mock_request.GET = {} 247 248 cl = ChangeList(mock_request, AnotherArticle, mock_admin.list_display, mock_admin.list_filter, [], mock_admin.date_hierarchy, [], None, 50, False, mock_admin) 249 self.assertEqual(date_hierarchy(cl), {'choices': [{'link': '?created__year=2010', 'title': '2010'}, {'link': '?created__year=2011', 'title': '2011'}], 'show': True}) 250 251 mock_request.GET = {'created__year': '2010'} 252 cl = ChangeList(mock_request, AnotherArticle, mock_admin.list_display, mock_admin.list_filter, [], mock_admin.date_hierarchy, [], None, 50, False, mock_admin) 253 self.assertEqual(date_hierarchy(cl), {'choices': [{'link': '?created__year=2010&created__month=1', 'title': u'January 2010'}, {'link': '?created__year=2010&created__month=10', 'title': u'October 2010'}], 'back': {'link': '?', 'title': u'All dates'}, 'show': True}) 254 255 mock_request.GET = {'created__year': '2010', 'created__month': '10'} 256 cl = ChangeList(mock_request, AnotherArticle, mock_admin.list_display, mock_admin.list_filter, [], mock_admin.date_hierarchy, [], None, 50, False, mock_admin) 257 self.assertEqual(date_hierarchy(cl), {'choices': [{'link': '?created__day=12&created__year=2010&created__month=10', 'title': u'October 12'}, {'link': '?created__day=13&created__year=2010&created__month=10', 'title': u'October 13'}], 'back': {'link': '?created__year=2010', 'title': '2010'}, 'show': True}) 258 259 mock_request.GET = {'created__year': '2010', 'created__month': '10', 'created__day': '13'} 260 cl = ChangeList(mock_request, AnotherArticle, mock_admin.list_display, mock_admin.list_filter, [], mock_admin.date_hierarchy, [], None, 50, False, mock_admin) 261 self.assertEqual(date_hierarchy(cl), {'choices': [{'title': u'October 13'}], 'back': {'link': '?created__year=2010&created__month=10', 'title': u'October 2010'}, 'show': True})