Commit 641c5fd3 authored by Jacob Kaplan-Moss's avatar Jacob Kaplan-Moss
Browse files

Fixed #7944: date-based generic views no longer get confused with a numeric...

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
parent de918509
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -263,6 +263,7 @@ answer newbie questions, and generally made Django that much better:
    Christopher Lenz <http://www.cmlenz.net/>
    lerouxb@gmail.com
    Piotr Lewandowski <piotr.lewandowski@gmail.com>
    Justin Lilly <justinlilly@gmail.com>
    Waylan Limberg <waylan@gmail.com>
    limodou
    Philip Lindborg <philip.lindborg@gmail.com>
+10 −4
Original line number Diff line number Diff line
@@ -116,7 +116,8 @@ def archive_month(request, year, month, queryset, date_field,
    """
    if extra_context is None: extra_context = {}
    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:
        raise Http404

@@ -181,7 +182,8 @@ def archive_week(request, year, week, queryset, date_field,
    """
    if extra_context is None: extra_context = {}
    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:
        raise Http404

@@ -237,7 +239,9 @@ def archive_day(request, year, month, day, queryset, date_field,
    """
    if extra_context is None: extra_context = {}
    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:
        raise Http404

@@ -307,7 +311,9 @@ def object_detail(request, year, month, day, queryset, date_field,
    """
    if extra_context is None: extra_context = {}
    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:
        raise Http404

+11 −0
Original line number Diff line number Diff line
@@ -90,3 +90,14 @@ class MonthArchiveTest(TestCase):
        response = self.client.get('/views/date_based/datefield/archive_month/2004/02/')
        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)
 No newline at end of file
+5 −0
Original line number Diff line number Diff line
@@ -20,6 +20,8 @@ date_based_info_dict = {
    'date_field': 'date_created',
    '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())

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/$',
        'object_detail',
        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})/$',
        'archive_month',
        date_based_info_dict),