Ticket #7145: 7145_django_1.2.patch

File 7145_django_1.2.patch, 6.3 KB (added by daniellindsley, 14 years ago)

Django 1.2.X version of the patch

  • 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):  
    246246                'choices': [{
    247247                    'link': link({year_field: year_lookup, month_field: month_lookup, day_field: day.day}),
    248248                    'title': capfirst(formats.date_format(day, 'MONTH_DAY_FORMAT'))
    249                 } for day in days]
     249                } for day in days if day]
    250250            }
    251251        elif year_lookup:
    252252            months = cl.query_set.filter(**{year_field: year_lookup}).dates(field_name, 'month')
    def date_hierarchy(cl):  
    263263            }
    264264        else:
    265265            years = cl.query_set.dates(field_name, 'year')
     266            # Ignore ``None`` dates.
    266267            return {
    267268                'show': True,
    268269                'choices': [{
    269270                    'link': link({year_field: str(year.year)}),
    270271                    'title': str(year.year),
    271                 } for year in years]
     272                } for year in years if year]
    272273            }
    273274date_hierarchy = register.inclusion_tag('admin/date_hierarchy.html')(date_hierarchy)
    274275
  • 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):  
    2020
    2121class Count(models.Model):
    2222    num = models.PositiveSmallIntegerField()
     23
     24
     25class 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  
    88
    99from django.contrib import admin
    1010from django.contrib.admin.util import display_for_field, label_for_field, lookup_field
    11 from django.contrib.admin.views.main import EMPTY_CHANGELIST_VALUE
     11from django.contrib.admin.views.main import EMPTY_CHANGELIST_VALUE, ChangeList
    1212from django.contrib.sites.models import Site
    1313from django.contrib.admin.util import NestedObjects
     14from django.contrib.admin.templatetags.admin_list import date_hierarchy
    1415
    15 from models import Article, Count
     16from models import Article, Count, AnotherArticle
    1617
    1718
    1819class NestedObjectsTests(TestCase):
    class UtilTests(unittest.TestCase):  
    220221            ),
    221222            ("not Really the Model", MockModelAdmin.test_from_model)
    222223        )
     224
     225
     226class MockRequest(object):
     227    pass
     228
     229
     230class AnotherArticleAdmin(admin.ModelAdmin):
     231    list_display = ['title', 'created']
     232    date_hierarchy = 'created'
     233
     234
     235class 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})
Back to Top