diff -r 41ee28cb9212 django/views/generic/dates.py
a
|
b
|
|
208 | 208 | date_field = self.get_date_field() |
209 | 209 | allow_empty = self.get_allow_empty() |
210 | 210 | |
211 | | date_list = queryset.dates(date_field, date_type)[::-1] |
| 211 | date_list = queryset.dates(date_field, date_type) |
212 | 212 | if date_list is not None and not date_list and not allow_empty: |
213 | 213 | raise Http404(_(u"No %(verbose_name_plural)s available") % { |
214 | 214 | 'verbose_name_plural': force_unicode(qs.model._meta.verbose_name_plural) |
… |
… |
|
239 | 239 | Return (date_list, items, extra_context) for this request. |
240 | 240 | """ |
241 | 241 | qs = self.get_dated_queryset() |
242 | | date_list = self.get_date_list(qs, 'year') |
| 242 | date_list = self.get_date_list(qs, 'year')[::-1] |
243 | 243 | |
244 | 244 | if date_list: |
245 | 245 | object_list = qs.order_by('-' + self.get_date_field()) |
… |
… |
|
301 | 301 | """ |
302 | 302 | List of objects published in a given year. |
303 | 303 | """ |
| 304 | |
304 | 305 | def get_dated_items(self): |
305 | 306 | """ |
306 | 307 | Return (date_list, items, extra_context) for this request. |
diff -r 41ee28cb9212 docs/ref/class-based-views.txt
a
|
b
|
|
265 | 265 | A boolean specifying whether to display the page if no objects are |
266 | 266 | available. If this is ``False`` and no objects are available, the view |
267 | 267 | will raise a 404 instead of displaying an empty page. By default, this |
268 | | is ``True``. |
| 268 | is ``True``. This now also applies to all data generated for links on |
| 269 | the page, for example the ``next_month`` context variable. These will |
| 270 | be ``None`` when ``allow_empty`` is ``False`` and no data is available |
| 271 | for the next month. This prevents creating links to non-existing pages. |
269 | 272 | |
270 | 273 | .. attribute:: model |
271 | 274 | |
diff -r 41ee28cb9212 tests/regressiontests/generic_views/dates.py
a
|
b
|
|
6 | 6 | |
7 | 7 | from regressiontests.generic_views.models import Book |
8 | 8 | |
| 9 | def _make_books(n, base_date): |
| 10 | for i in range(n): |
| 11 | b = Book.objects.create( |
| 12 | name='Book %d' % i, |
| 13 | slug='book-%d' % i, |
| 14 | pages=100+i, |
| 15 | pubdate=base_date - datetime.timedelta(days=i)) |
| 16 | |
9 | 17 | class ArchiveIndexViewTests(TestCase): |
10 | 18 | fixtures = ['generic-views-test-data.json'] |
11 | 19 | urls = 'regressiontests.generic_views.urls' |
12 | 20 | |
13 | | def _make_books(self, n, base_date): |
14 | | for i in range(n): |
15 | | b = Book.objects.create( |
16 | | name='Book %d' % i, |
17 | | slug='book-%d' % i, |
18 | | pages=100+i, |
19 | | pubdate=base_date - datetime.timedelta(days=1)) |
20 | 21 | |
21 | 22 | def test_archive_view(self): |
22 | 23 | res = self.client.get('/dates/books/') |
… |
… |
|
64 | 65 | self.assertRaises(ImproperlyConfigured, self.client.get, '/dates/books/invalid/') |
65 | 66 | |
66 | 67 | def test_paginated_archive_view(self): |
67 | | self._make_books(20, base_date=datetime.date.today()) |
| 68 | _make_books(20, base_date=datetime.date.today()) |
68 | 69 | res = self.client.get('/dates/books/paginated/') |
69 | 70 | self.assertEqual(res.status_code, 200) |
70 | 71 | self.assertEqual(res.context['date_list'], Book.objects.dates('pubdate', 'year')[::-1]) |
… |
… |
|
76 | 77 | self.assertEqual(res.context['page_obj'].number, 2) |
77 | 78 | self.assertEqual(list(res.context['latest']), list(Book.objects.all()[10:20])) |
78 | 79 | |
| 80 | def test_date_list_order(self): |
| 81 | """ date_list should be descending in index """ |
| 82 | _make_books(20, base_date=datetime.date.today()) |
| 83 | res = self.client.get('/dates/books/') |
| 84 | self.assertEqual(list(res.context['date_list']), list(reversed(sorted(res.context['date_list'])))) |
| 85 | |
79 | 86 | |
80 | 87 | class YearArchiveViewTests(TestCase): |
81 | 88 | fixtures = ['generic-views-test-data.json'] |
… |
… |
|
130 | 137 | res = self.client.get('/dates/books/no_year/') |
131 | 138 | self.assertEqual(res.status_code, 404) |
132 | 139 | |
| 140 | def test_date_list_order(self): |
| 141 | """ date_list should be ascending in year view """ |
| 142 | _make_books(35, base_date=datetime.date.today()) |
| 143 | year = datetime.date.today().year |
| 144 | res = self.client.get('/dates/books/%s/' % year) |
| 145 | self.assertEqual(list(res.context['date_list']), list(sorted(res.context['date_list']))) |
| 146 | |
133 | 147 | class MonthArchiveViewTests(TestCase): |
134 | 148 | fixtures = ['generic-views-test-data.json'] |
135 | 149 | urls = 'regressiontests.generic_views.urls' |
… |
… |
|
234 | 248 | self.assertEqual(res.status_code, 200) |
235 | 249 | self.assertEqual(res.context['previous_month'], datetime.date(2010,9,1)) |
236 | 250 | |
| 251 | def test_date_list_order(self): |
| 252 | """ date_list should be ascending in month view """ |
| 253 | _make_books(35, base_date=datetime.date.today()) |
| 254 | urlbit = datetime.date.today().strftime('%Y/%b').lower() |
| 255 | res = self.client.get('/dates/books/%s/' % urlbit) |
| 256 | self.assertEqual(list(res.context['date_list']), list(sorted(res.context['date_list']))) |
| 257 | |
237 | 258 | |
238 | 259 | class WeekArchiveViewTests(TestCase): |
239 | 260 | fixtures = ['generic-views-test-data.json'] |