Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #7944: date-based generic views no longer get confused with a n…

…umeric 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
  • Loading branch information...
commit 641c5fd3b50fa2b064c67243c41fda817513681c 1 parent de91850
Jacob Kaplan-Moss authored April 09, 2009
1  AUTHORS
@@ -263,6 +263,7 @@ answer newbie questions, and generally made Django that much better:
263 263
     Christopher Lenz <http://www.cmlenz.net/>
264 264
     lerouxb@gmail.com
265 265
     Piotr Lewandowski <piotr.lewandowski@gmail.com>
  266
+    Justin Lilly <justinlilly@gmail.com>
266 267
     Waylan Limberg <waylan@gmail.com>
267 268
     limodou
268 269
     Philip Lindborg <philip.lindborg@gmail.com>
14  django/views/generic/date_based.py
@@ -116,7 +116,8 @@ def archive_month(request, year, month, queryset, date_field,
116 116
     """
117 117
     if extra_context is None: extra_context = {}
118 118
     try:
119  
-        date = datetime.date(*time.strptime(year+month, '%Y'+month_format)[:3])
  119
+        tt = time.strptime("%s-%s" % (year, month), '%s-%s' % ('%Y', month_format))
  120
+        date = datetime.date(*tt[:3])
120 121
     except ValueError:
121 122
         raise Http404
122 123
 
@@ -181,7 +182,8 @@ def archive_week(request, year, week, queryset, date_field,
181 182
     """
182 183
     if extra_context is None: extra_context = {}
183 184
     try:
184  
-        date = datetime.date(*time.strptime(year+'-0-'+week, '%Y-%w-%U')[:3])
  185
+        tt = time.strptime(year+'-0-'+week, '%Y-%w-%U')
  186
+        date = datetime.date(*tt[:3])
185 187
     except ValueError:
186 188
         raise Http404
187 189
 
@@ -237,7 +239,9 @@ def archive_day(request, year, month, day, queryset, date_field,
237 239
     """
238 240
     if extra_context is None: extra_context = {}
239 241
     try:
240  
-        date = datetime.date(*time.strptime(year+month+day, '%Y'+month_format+day_format)[:3])
  242
+        tt = time.strptime('%s-%s-%s' % (year, month, day), 
  243
+                           '%s-%s-%s' % ('%Y', month_format, day_format))
  244
+        date = datetime.date(*tt[:3])
241 245
     except ValueError:
242 246
         raise Http404
243 247
 
@@ -307,7 +311,9 @@ def object_detail(request, year, month, day, queryset, date_field,
307 311
     """
308 312
     if extra_context is None: extra_context = {}
309 313
     try:
310  
-        date = datetime.date(*time.strptime(year+month+day, '%Y'+month_format+day_format)[:3])
  314
+        tt = time.strptime('%s-%s-%s' % (year, month, day), 
  315
+                           '%s-%s-%s' % ('%Y', month_format, day_format))
  316
+        date = datetime.date(*tt[:3])
311 317
     except ValueError:
312 318
         raise Http404
313 319
 
11  tests/regressiontests/views/tests/generic/date_based.py
@@ -90,3 +90,14 @@ def test_archive_month_includes_only_month(self):
90 90
         response = self.client.get('/views/date_based/datefield/archive_month/2004/02/')
91 91
         self.assertEqual(response.status_code, 404)
92 92
 
  93
+class DayArchiveTests(TestCase):
  94
+
  95
+    def test_year_month_day_format(self):
  96
+        """
  97
+        Make sure day views don't get confused with numeric month formats (#7944)
  98
+        """
  99
+        author = Author.objects.create(name="John Smith")
  100
+        article = Article.objects.create(title="example", author=author, date_created=datetime(2004, 1, 21, 0, 0, 1))
  101
+        response = self.client.get('/views/date_based/archive_day/2004/1/21/')
  102
+        self.assertEqual(response.status_code, 200)
  103
+        self.assertEqual(response.context['object_list'][0], article)
5  tests/regressiontests/views/urls.py
@@ -20,6 +20,8 @@
20 20
     'date_field': 'date_created',
21 21
     'month_format': '%m',
22 22
 }
  23
+numeric_days_info_dict = dict(date_based_info_dict, day_format='%d')
  24
+
23 25
 date_based_datefield_info_dict = dict(date_based_info_dict, queryset=DateArticle.objects.all())
24 26
 
25 27
 urlpatterns = patterns('',
@@ -46,6 +48,9 @@
46 48
     (r'^date_based/object_detail/(?P<year>\d{4})/(?P<month>\d{1,2})/(?P<day>\d{1,2})/(?P<slug>[-\w]+)/allow_future/$',
47 49
         'object_detail',
48 50
         dict(allow_future=True, slug_field='slug', **date_based_info_dict)),
  51
+    (r'^date_based/archive_day/(?P<year>\d{4})/(?P<month>\d{1,2})/(?P<day>\d{1,2})/$',
  52
+        'archive_day',
  53
+        numeric_days_info_dict),
49 54
     (r'^date_based/archive_month/(?P<year>\d{4})/(?P<month>\d{1,2})/$',
50 55
         'archive_month',
51 56
         date_based_info_dict),

0 notes on commit 641c5fd

Please sign in to comment.
Something went wrong with that request. Please try again.