Fixed #7944: date-based generic views no longer get confused with a numeric month format. Thanks to Justin Lilly and Alex Gaynor.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@10457 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Jacob Kaplan-Moss 2009-04-09 15:23:56 +00:00
parent de91850944
commit 641c5fd3b5
4 changed files with 27 additions and 4 deletions

View File

@ -263,6 +263,7 @@ answer newbie questions, and generally made Django that much better:
Christopher Lenz <http://www.cmlenz.net/> Christopher Lenz <http://www.cmlenz.net/>
lerouxb@gmail.com lerouxb@gmail.com
Piotr Lewandowski <piotr.lewandowski@gmail.com> Piotr Lewandowski <piotr.lewandowski@gmail.com>
Justin Lilly <justinlilly@gmail.com>
Waylan Limberg <waylan@gmail.com> Waylan Limberg <waylan@gmail.com>
limodou limodou
Philip Lindborg <philip.lindborg@gmail.com> Philip Lindborg <philip.lindborg@gmail.com>

View File

@ -116,7 +116,8 @@ def archive_month(request, year, month, queryset, date_field,
""" """
if extra_context is None: extra_context = {} if extra_context is None: extra_context = {}
try: try:
date = datetime.date(*time.strptime(year+month, '%Y'+month_format)[:3]) tt = time.strptime("%s-%s" % (year, month), '%s-%s' % ('%Y', month_format))
date = datetime.date(*tt[:3])
except ValueError: except ValueError:
raise Http404 raise Http404
@ -181,7 +182,8 @@ def archive_week(request, year, week, queryset, date_field,
""" """
if extra_context is None: extra_context = {} if extra_context is None: extra_context = {}
try: try:
date = datetime.date(*time.strptime(year+'-0-'+week, '%Y-%w-%U')[:3]) tt = time.strptime(year+'-0-'+week, '%Y-%w-%U')
date = datetime.date(*tt[:3])
except ValueError: except ValueError:
raise Http404 raise Http404
@ -237,7 +239,9 @@ def archive_day(request, year, month, day, queryset, date_field,
""" """
if extra_context is None: extra_context = {} if extra_context is None: extra_context = {}
try: try:
date = datetime.date(*time.strptime(year+month+day, '%Y'+month_format+day_format)[:3]) tt = time.strptime('%s-%s-%s' % (year, month, day),
'%s-%s-%s' % ('%Y', month_format, day_format))
date = datetime.date(*tt[:3])
except ValueError: except ValueError:
raise Http404 raise Http404
@ -307,7 +311,9 @@ def object_detail(request, year, month, day, queryset, date_field,
""" """
if extra_context is None: extra_context = {} if extra_context is None: extra_context = {}
try: try:
date = datetime.date(*time.strptime(year+month+day, '%Y'+month_format+day_format)[:3]) tt = time.strptime('%s-%s-%s' % (year, month, day),
'%s-%s-%s' % ('%Y', month_format, day_format))
date = datetime.date(*tt[:3])
except ValueError: except ValueError:
raise Http404 raise Http404

View File

@ -90,3 +90,14 @@ class MonthArchiveTest(TestCase):
response = self.client.get('/views/date_based/datefield/archive_month/2004/02/') response = self.client.get('/views/date_based/datefield/archive_month/2004/02/')
self.assertEqual(response.status_code, 404) self.assertEqual(response.status_code, 404)
class DayArchiveTests(TestCase):
def test_year_month_day_format(self):
"""
Make sure day views don't get confused with numeric month formats (#7944)
"""
author = Author.objects.create(name="John Smith")
article = Article.objects.create(title="example", author=author, date_created=datetime(2004, 1, 21, 0, 0, 1))
response = self.client.get('/views/date_based/archive_day/2004/1/21/')
self.assertEqual(response.status_code, 200)
self.assertEqual(response.context['object_list'][0], article)

View File

@ -20,6 +20,8 @@ date_based_info_dict = {
'date_field': 'date_created', 'date_field': 'date_created',
'month_format': '%m', 'month_format': '%m',
} }
numeric_days_info_dict = dict(date_based_info_dict, day_format='%d')
date_based_datefield_info_dict = dict(date_based_info_dict, queryset=DateArticle.objects.all()) date_based_datefield_info_dict = dict(date_based_info_dict, queryset=DateArticle.objects.all())
urlpatterns = patterns('', urlpatterns = patterns('',
@ -46,6 +48,9 @@ urlpatterns += patterns('django.views.generic.date_based',
(r'^date_based/object_detail/(?P<year>\d{4})/(?P<month>\d{1,2})/(?P<day>\d{1,2})/(?P<slug>[-\w]+)/allow_future/$', (r'^date_based/object_detail/(?P<year>\d{4})/(?P<month>\d{1,2})/(?P<day>\d{1,2})/(?P<slug>[-\w]+)/allow_future/$',
'object_detail', 'object_detail',
dict(allow_future=True, slug_field='slug', **date_based_info_dict)), dict(allow_future=True, slug_field='slug', **date_based_info_dict)),
(r'^date_based/archive_day/(?P<year>\d{4})/(?P<month>\d{1,2})/(?P<day>\d{1,2})/$',
'archive_day',
numeric_days_info_dict),
(r'^date_based/archive_month/(?P<year>\d{4})/(?P<month>\d{1,2})/$', (r'^date_based/archive_month/(?P<year>\d{4})/(?P<month>\d{1,2})/$',
'archive_month', 'archive_month',
date_based_info_dict), date_based_info_dict),