Ticket #16218: django-16218.diff

File django-16218.diff, 5.2 KB (added by Bas Peschier, 13 years ago)
  • django/views/generic/dates.py

    diff -r 41ee28cb9212 django/views/generic/dates.py
    a b  
    208208        date_field = self.get_date_field()
    209209        allow_empty = self.get_allow_empty()
    210210
    211         date_list = queryset.dates(date_field, date_type)[::-1]
     211        date_list = queryset.dates(date_field, date_type)
    212212        if date_list is not None and not date_list and not allow_empty:
    213213            raise Http404(_(u"No %(verbose_name_plural)s available") % {
    214214                    'verbose_name_plural': force_unicode(qs.model._meta.verbose_name_plural)
     
    239239        Return (date_list, items, extra_context) for this request.
    240240        """
    241241        qs = self.get_dated_queryset()
    242         date_list = self.get_date_list(qs, 'year')
     242        date_list = self.get_date_list(qs, 'year')[::-1]
    243243
    244244        if date_list:
    245245            object_list = qs.order_by('-' + self.get_date_field())
     
    301301    """
    302302    List of objects published in a given year.
    303303    """
     304
    304305    def get_dated_items(self):
    305306        """
    306307        Return (date_list, items, extra_context) for this request.
  • docs/ref/class-based-views.txt

    diff -r 41ee28cb9212 docs/ref/class-based-views.txt
    a b  
    265265        A boolean specifying whether to display the page if no objects are
    266266        available. If this is ``False`` and no objects are available, the view
    267267        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.
    269272
    270273    .. attribute:: model
    271274
  • tests/regressiontests/generic_views/dates.py

    diff -r 41ee28cb9212 tests/regressiontests/generic_views/dates.py
    a b  
    66
    77from regressiontests.generic_views.models import Book
    88
     9def _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
    917class ArchiveIndexViewTests(TestCase):
    1018    fixtures = ['generic-views-test-data.json']
    1119    urls = 'regressiontests.generic_views.urls'
    1220
    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))
    2021
    2122    def test_archive_view(self):
    2223        res = self.client.get('/dates/books/')
     
    6465        self.assertRaises(ImproperlyConfigured, self.client.get, '/dates/books/invalid/')
    6566
    6667    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())
    6869        res = self.client.get('/dates/books/paginated/')
    6970        self.assertEqual(res.status_code, 200)
    7071        self.assertEqual(res.context['date_list'], Book.objects.dates('pubdate', 'year')[::-1])
     
    7677        self.assertEqual(res.context['page_obj'].number, 2)
    7778        self.assertEqual(list(res.context['latest']), list(Book.objects.all()[10:20]))
    7879
     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
    7986
    8087class YearArchiveViewTests(TestCase):
    8188    fixtures = ['generic-views-test-data.json']
     
    130137        res = self.client.get('/dates/books/no_year/')
    131138        self.assertEqual(res.status_code, 404)
    132139
     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
    133147class MonthArchiveViewTests(TestCase):
    134148    fixtures = ['generic-views-test-data.json']
    135149    urls = 'regressiontests.generic_views.urls'
     
    234248        self.assertEqual(res.status_code, 200)
    235249        self.assertEqual(res.context['previous_month'], datetime.date(2010,9,1))
    236250
     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
    237258
    238259class WeekArchiveViewTests(TestCase):
    239260    fixtures = ['generic-views-test-data.json']
Back to Top