Ticket #1453: issue_1453.diff

File issue_1453.diff, 8.5 KB (added by zefciu, 13 years ago)
  • django/views/generic/date_based.py

    diff --git a/django/views/generic/date_based.py b/django/views/generic/date_based.py
    index 75094aa..2795833 100644
    a b from django.template import loader, RequestContext  
    55from django.core.exceptions import ObjectDoesNotExist
    66from django.core.xheaders import populate_xheaders
    77from django.db.models.fields import DateTimeField
     8from django.db.models.query import DateQuerySet
    89from django.http import Http404, HttpResponse
    910from django.utils import timezone
     11from django.utils.functional import lazy
    1012
    1113import warnings
    1214warnings.warn(
    def archive_index(request, queryset, date_field, num_latest=15,  
    3335    model = queryset.model
    3436    if not allow_future:
    3537        queryset = queryset.filter(**{'%s__lte' % date_field: timezone.now()})
    36     date_list = queryset.dates(date_field, 'year')[::-1]
    37     if not date_list and not allow_empty:
     38    date_list = lazy(
     39        lambda: queryset.dates(date_field, 'year')[::-1],
     40        list, DateQuerySet
     41    )()
     42    if not (queryset.count() or allow_empty):
    3843        raise Http404("No %s available" % model._meta.verbose_name)
    3944
    40     if date_list and num_latest:
     45    if queryset.count() and num_latest:
    4146        latest = queryset.order_by('-'+date_field)[:num_latest]
    4247    else:
    4348        latest = None
  • django/views/generic/dates.py

    diff --git a/django/views/generic/dates.py b/django/views/generic/dates.py
    index 5f5f959..4ca7ad7 100644
    a b from django.core.exceptions import ImproperlyConfigured  
    44from django.http import Http404
    55from django.utils.encoding import force_unicode
    66from django.utils.translation import ugettext as _
     7from django.utils.functional import lazy
    78from django.utils import timezone
    89from django.views.generic.base import View
    910from django.views.generic.detail import BaseDetailView, SingleObjectTemplateResponseMixin
    class BaseDateListView(MultipleObjectMixin, DateMixin, View):  
    203204
    204205    def get_date_list(self, queryset, date_type):
    205206        """
    206         Get a date list by calling `queryset.dates()`, checking along the way
    207         for empty lists that aren't allowed.
     207        Get lazily a date list by calling `queryset.dates()`, checking along
     208        the way for empty lists that aren't allowed.
    208209        """
    209210        date_field = self.get_date_field()
    210211        allow_empty = self.get_allow_empty()
    211212
    212213        date_list = queryset.dates(date_field, date_type)[::-1]
    213         if date_list is not None and not date_list and not allow_empty:
     214        if not (queryset.count() or allow_empty):
    214215            name = force_unicode(queryset.model._meta.verbose_name_plural)
    215216            raise Http404(_(u"No %(verbose_name_plural)s available") %
    216217                          {'verbose_name_plural': name})
    217218
    218         return date_list
     219
     220        return lazy(
     221            lambda: queryset.dates(date_field, date_type)[::-1],
     222            list, models.query.DateQuerySet
     223        )()
     224
    219225
    220226    def get_context_data(self, **kwargs):
    221227        """
    class BaseArchiveIndexView(BaseDateListView):  
    242248        qs = self.get_dated_queryset()
    243249        date_list = self.get_date_list(qs, 'year')
    244250
    245         if date_list:
     251        if qs.count():
    246252            object_list = qs.order_by('-' + self.get_date_field())
    247253        else:
    248254            object_list = qs.none()
  • tests/regressiontests/generic_views/dates.py

    diff --git a/tests/regressiontests/generic_views/dates.py b/tests/regressiontests/generic_views/dates.py
    index 652f66b..034cc85 100644
    a b import datetime  
    44
    55from django.core.exceptions import ImproperlyConfigured
    66from django.test import TestCase
     7from django.utils.functional import Promise
    78
    89from .models import Book
    910
    class ArchiveIndexViewTests(TestCase):  
    2324    def test_archive_view(self):
    2425        res = self.client.get('/dates/books/')
    2526        self.assertEqual(res.status_code, 200)
     27        assert isinstance(res.context['date_list'], Promise)
    2628        self.assertEqual(res.context['date_list'], Book.objects.dates('pubdate', 'year')[::-1])
    2729        self.assertEqual(list(res.context['latest']), list(Book.objects.all()))
    2830        self.assertTemplateUsed(res, 'generic_views/book_archive.html')
  • tests/regressiontests/views/generic_urls.py

    diff --git a/tests/regressiontests/views/generic_urls.py b/tests/regressiontests/views/generic_urls.py
    index 40a6c01..6385601 100644
    a b numeric_days_info_dict = dict(date_based_info_dict, day_format='%d')  
    2626
    2727date_based_datefield_info_dict = dict(date_based_info_dict, queryset=DateArticle.objects.all())
    2828
     29archive_index_info_dict = dict(
     30    allow_future=True, **date_based_info_dict
     31)
     32del archive_index_info_dict['month_format']
     33
     34archive_index_disallow_empty_info_dict = dict(
     35    allow_empty=False, **archive_index_info_dict
     36)
     37
    2938urlpatterns = patterns('',
    3039    (r'^accounts/login/$', 'django.contrib.auth.views.login', {'template_name': 'login.html'}),
    3140    (r'^accounts/logout/$', 'django.contrib.auth.views.logout'),
    urlpatterns = patterns('',  
    3746
    3847# Date-based generic views.
    3948urlpatterns += patterns('django.views.generic.date_based',
     49    (r'^date_based/archive_index/$', 'archive_index',
     50        archive_index_disallow_empty_info_dict),
     51    (r'^date_based/archive_index/allow_empty/$', 'archive_index',
     52        archive_index_info_dict),
    4053    (r'^date_based/object_detail/(?P<year>\d{4})/(?P<month>\d{1,2})/(?P<day>\d{1,2})/(?P<slug>[-\w]+)/$',
    4154        'object_detail',
    4255        dict(slug_field='slug', **date_based_info_dict)),
  • tests/regressiontests/views/tests/__init__.py

    diff --git a/tests/regressiontests/views/tests/__init__.py b/tests/regressiontests/views/tests/__init__.py
    index 04537a2..132fb77 100644
    a b from .debug import (DebugViewTests, ExceptionReporterTests,  
    66from .defaults import DefaultsTests
    77from .generic.create_update import (UpdateDeleteObjectTest, CreateObjectTest,
    88    PostSaveRedirectTests, NoPostSaveNoAbsoluteUrl, AbsoluteUrlNoPostSave)
    9 from .generic.date_based import MonthArchiveTest, ObjectDetailTest, DayArchiveTests
     9from .generic.date_based import ArchiveIndexTest, MonthArchiveTest, ObjectDetailTest, DayArchiveTests
    1010from .generic.object_list import ObjectListTest
    1111from .generic.simple import RedirectToTest
    1212from .i18n import JsI18NTests, I18NTests, JsI18NTestsMultiPackage
  • tests/regressiontests/views/tests/generic/date_based.py

    diff --git a/tests/regressiontests/views/tests/generic/date_based.py b/tests/regressiontests/views/tests/generic/date_based.py
    index 96555e5..de19f10 100644
    a b from django.test import TestCase  
    55from datetime import datetime, date
    66from datetime import timedelta
    77from regressiontests.views.models import Article, Author, DateArticle
     8from django.utils.functional import Promise
     9
     10class ArchiveIndexTest(TestCase):
     11    fixtures=['testdata.json']
     12    urls = 'regressiontests.views.generic_urls'
     13
     14    def setUp(self):
     15        self.save_warnings_state()
     16        warnings.filterwarnings('ignore', category=DeprecationWarning,
     17                                module='django.views.generic.date_based')
     18
     19    def tearDown(self):
     20        self.restore_warnings_state()
     21
     22    def test_get_archive_view(self):
     23        res = self.client.get('/date_based/archive_index/')
     24        assert isinstance(res.context['date_list'], Promise)
     25        self.assertEqual(
     26            res.context['date_list'],
     27            Article.objects.dates('date_created', 'year')[::-1]
     28        )
     29
     30    def test_empty_archive_view(self):
     31        Article.objects.all().delete()
     32        res = self.client.get('/date_based/archive_index/')
     33        self.assertEqual(res.status_code, 404)
     34
     35    def test_allow_empty_archive_view(self):
     36        Article.objects.all().delete()
     37        res = self.client.get('/date_based/archive_index/allow_empty/')
     38        self.assertEqual(res.status_code, 200)
     39        self.assertEqual(list(res.context['date_list']), [])
     40        self.assertTemplateUsed(res, 'views/article_archive.html')
    841
    942class ObjectDetailTest(TestCase):
    1043    fixtures = ['testdata.json']
  • new file tests/templates/views/article_archive.html

    diff --git a/tests/templates/views/article_archive.html b/tests/templates/views/article_archive.html
    new file mode 100644
    index 0000000..bd2d67f
    - +  
     1This template intentionally left blank
Back to Top