Code

Ticket #1282: patch-1282.diff

File patch-1282.diff, 4.1 KB (added by nickefford, 6 years ago)

Updated patch

Line 
1Index: django/views/generic/date_based.py
2===================================================================
3--- django/views/generic/date_based.py  (revision 7977)
4+++ django/views/generic/date_based.py  (working copy)
5@@ -274,15 +274,16 @@
6             c[key] = value
7     return HttpResponse(t.render(c), mimetype=mimetype)
8 
9-def archive_today(request, **kwargs):
10+def archive_today(request, month_format='%b', **kwargs):
11     """
12     Generic daily archive view for today. Same as archive_day view.
13     """
14     today = datetime.date.today()
15     kwargs.update({
16         'year': str(today.year),
17-        'month': today.strftime('%b').lower(),
18+        'month': today.strftime(month_format).lower(),
19         'day': str(today.day),
20+        'month_format': month_format,
21     })
22     return archive_day(request, **kwargs)
23 
24Index: tests/regressiontests/views/tests/generic/date_based.py
25===================================================================
26--- tests/regressiontests/views/tests/generic/date_based.py     (revision 7977)
27+++ tests/regressiontests/views/tests/generic/date_based.py     (working copy)
28@@ -67,5 +67,40 @@
29         article.save()
30         response = self.client.get('/views/date_based/archive_month/2004/02/')
31         self.assertEqual(response.status_code, 404)
32-               
33-       
34\ No newline at end of file
35+
36+class DayArchiveTest(TestCase):
37+    fixtures = ['testdata.json']
38+
39+    def setUp(self):
40+        # Correct the date for the current article
41+        current_article = Article.objects.get(title="Current Article")
42+        current_article.date_created = datetime.now()
43+        current_article.save()
44+
45+    def test_archive_today(self):
46+        response = self.client.get('/views/date_based/today/')
47+        self.assertEqual(response.status_code, 200)
48+        objects = response.context['object_list']
49+        self.assertEqual(len(objects), 1)
50+        self.assertEqual(objects[0].title, 'Current Article')
51+
52+    def test_archive_today_month_format(self):
53+        response = self.client.get('/views/date_based/today_month_format/')
54+        self.assertEqual(response.status_code, 200)
55+        objects = response.context['object_list']
56+        self.assertEqual(len(objects), 1)
57+        self.assertEqual(objects[0].title, 'Current Article')
58+
59+    def test_archive_day_abbrev_month(self):
60+        response = self.client.get('/views/date_based/archive_day/2001/jan/1/')
61+        self.assertEqual(response.status_code, 200)
62+        objects = response.context['object_list']
63+        self.assertEqual(len(objects), 1)
64+        self.assertEqual(objects[0].title, 'Old Article')
65+
66+    def test_archive_day_numeric_month(self):
67+        response = self.client.get('/views/date_based/archive_day/2001/1/1/')
68+        self.assertEqual(response.status_code, 200)
69+        objects = response.context['object_list']
70+        self.assertEqual(len(objects), 1)
71+        self.assertEqual(objects[0].title, 'Old Article')
72Index: tests/regressiontests/views/urls.py
73===================================================================
74--- tests/regressiontests/views/urls.py (revision 7977)
75+++ tests/regressiontests/views/urls.py (working copy)
76@@ -21,6 +21,12 @@
77     'month_format': '%m',
78 }
79 
80+# For testing of archive_day
81+archive_day_info_dict = {
82+    'queryset': Article.objects.all(),
83+    'date_field': 'date_created',
84+}
85+
86 urlpatterns = patterns('',
87     (r'^$', views.index_page),
88 
89@@ -48,6 +54,18 @@
90     (r'^date_based/archive_month/(?P<year>\d{4})/(?P<month>\d{1,2})/$',
91         'archive_month',
92         date_based_info_dict),
93+    (r'^date_based/today/$',
94+        'archive_today',
95+        archive_day_info_dict),
96+    (r'^date_based/today_month_format/$',
97+        'archive_today',
98+        dict(archive_day_info_dict, month_format='%m')),
99+    (r'^date_based/archive_day/(?P<year>\d{4})/(?P<month>\w{3})/(?P<day>\d{1,2})/$',
100+        'archive_day',
101+        dict(archive_day_info_dict, month_format='%b')),
102+    (r'^date_based/archive_day/(?P<year>\d{4})/(?P<month>\d{1,2})/(?P<day>\d{1,2})/$',
103+        'archive_day',
104+        dict(archive_day_info_dict, month_format='%m')),
105 )
106 
107 # crud generic views.