Code

Ticket #3274: month_archive_date_list.diff

File month_archive_date_list.diff, 3.2 KB (added by seanbrant, 5 years ago)
Line 
1Index: django/views/generic/date_based.py
2===================================================================
3--- django/views/generic/date_based.py  (revision 12157)
4+++ django/views/generic/date_based.py  (working copy)
5@@ -105,6 +105,8 @@
6 
7     Templates: ``<app_label>/<model_name>_archive_month.html``
8     Context:
9+        date_list:
10+            List of days in this month with objects
11         month:
12             (date) this month
13         next_month:
14@@ -139,6 +141,7 @@
15     if last_day >= now.date() and not allow_future:
16         lookup_kwargs['%s__lte' % date_field] = now
17     object_list = queryset.filter(**lookup_kwargs)
18+    date_list = object_list.dates(date_field, 'day')
19     if not object_list and not allow_empty:
20         raise Http404
21 
22@@ -160,6 +163,7 @@
23         template_name = "%s/%s_archive_month.html" % (model._meta.app_label, model._meta.object_name.lower())
24     t = template_loader.get_template(template_name)
25     c = RequestContext(request, {
26+        'date_list': date_list,
27         '%s_list' % template_object_name: object_list,
28         'month': date,
29         'next_month': next_month,
30Index: tests/regressiontests/views/tests/generic/date_based.py
31===================================================================
32--- tests/regressiontests/views/tests/generic/date_based.py     (revision 12157)
33+++ tests/regressiontests/views/tests/generic/date_based.py     (working copy)
34@@ -110,6 +110,22 @@
35         self.assertEqual(response.status_code, 200)
36         self.assertEqual(response.context['next_month'], None)
37         self.assertEqual(response.context['previous_month'], prev_month)
38+       
39+    def test_archive_month_date_list(self):
40+        author = Author(name="John Smith")
41+        author.save()
42+        date1 = datetime(2010, 1, 1, 0, 0, 0)
43+        date2 = datetime(2010, 1, 2, 0, 0, 0)
44+        Article.objects.create(title='example1', author=author, date_created=date1)
45+        Article.objects.create(title='example2', author=author, date_created=date2)
46+        response = self.client.get('/views/date_based/archive_month/2010/1/')
47+        self.assertEqual(response.status_code, 200)
48+        self.assertEqual(len(response.context['date_list']), 2)
49+        self.assertEqual(response.context['date_list'][0], date1)
50+        # Checks that the same date is not included more than once in the list
51+        Article.objects.create(title='example2', author=author, date_created=date2)
52+        response = self.client.get('/views/date_based/archive_month/2010/1/')
53+        self.assertEqual(len(response.context['date_list']), 2)
54 
55 class DayArchiveTests(TestCase):
56 
57Index: docs/ref/generic-views.txt
58===================================================================
59--- docs/ref/generic-views.txt  (revision 12157)
60+++ docs/ref/generic-views.txt  (working copy)
61@@ -371,6 +371,12 @@
62 
63 In addition to ``extra_context``, the template's context will be:
64 
65+.. versionadded:: 1.2
66+
67+    * ``date_list``: A list of ``datetime.date`` objects representing all
68+      days that have objects available in the given month, according to
69+      ``queryset``, in ascending order.   
70+
71     * ``month``: A ``datetime.date`` object representing the given month.
72 
73     * ``next_month``: A ``datetime.date`` object representing the first day of