Ticket #1453: issue_1453.diff
File issue_1453.diff, 8.5 KB (added by , 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 5 5 from django.core.exceptions import ObjectDoesNotExist 6 6 from django.core.xheaders import populate_xheaders 7 7 from django.db.models.fields import DateTimeField 8 from django.db.models.query import DateQuerySet 8 9 from django.http import Http404, HttpResponse 9 10 from django.utils import timezone 11 from django.utils.functional import lazy 10 12 11 13 import warnings 12 14 warnings.warn( … … def archive_index(request, queryset, date_field, num_latest=15, 33 35 model = queryset.model 34 36 if not allow_future: 35 37 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): 38 43 raise Http404("No %s available" % model._meta.verbose_name) 39 44 40 if date_listand num_latest:45 if queryset.count() and num_latest: 41 46 latest = queryset.order_by('-'+date_field)[:num_latest] 42 47 else: 43 48 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 4 4 from django.http import Http404 5 5 from django.utils.encoding import force_unicode 6 6 from django.utils.translation import ugettext as _ 7 from django.utils.functional import lazy 7 8 from django.utils import timezone 8 9 from django.views.generic.base import View 9 10 from django.views.generic.detail import BaseDetailView, SingleObjectTemplateResponseMixin … … class BaseDateListView(MultipleObjectMixin, DateMixin, View): 203 204 204 205 def get_date_list(self, queryset, date_type): 205 206 """ 206 Get a date list by calling `queryset.dates()`, checking along the way207 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. 208 209 """ 209 210 date_field = self.get_date_field() 210 211 allow_empty = self.get_allow_empty() 211 212 212 213 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): 214 215 name = force_unicode(queryset.model._meta.verbose_name_plural) 215 216 raise Http404(_(u"No %(verbose_name_plural)s available") % 216 217 {'verbose_name_plural': name}) 217 218 218 return date_list 219 220 return lazy( 221 lambda: queryset.dates(date_field, date_type)[::-1], 222 list, models.query.DateQuerySet 223 )() 224 219 225 220 226 def get_context_data(self, **kwargs): 221 227 """ … … class BaseArchiveIndexView(BaseDateListView): 242 248 qs = self.get_dated_queryset() 243 249 date_list = self.get_date_list(qs, 'year') 244 250 245 if date_list:251 if qs.count(): 246 252 object_list = qs.order_by('-' + self.get_date_field()) 247 253 else: 248 254 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 4 4 5 5 from django.core.exceptions import ImproperlyConfigured 6 6 from django.test import TestCase 7 from django.utils.functional import Promise 7 8 8 9 from .models import Book 9 10 … … class ArchiveIndexViewTests(TestCase): 23 24 def test_archive_view(self): 24 25 res = self.client.get('/dates/books/') 25 26 self.assertEqual(res.status_code, 200) 27 assert isinstance(res.context['date_list'], Promise) 26 28 self.assertEqual(res.context['date_list'], Book.objects.dates('pubdate', 'year')[::-1]) 27 29 self.assertEqual(list(res.context['latest']), list(Book.objects.all())) 28 30 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') 26 26 27 27 date_based_datefield_info_dict = dict(date_based_info_dict, queryset=DateArticle.objects.all()) 28 28 29 archive_index_info_dict = dict( 30 allow_future=True, **date_based_info_dict 31 ) 32 del archive_index_info_dict['month_format'] 33 34 archive_index_disallow_empty_info_dict = dict( 35 allow_empty=False, **archive_index_info_dict 36 ) 37 29 38 urlpatterns = patterns('', 30 39 (r'^accounts/login/$', 'django.contrib.auth.views.login', {'template_name': 'login.html'}), 31 40 (r'^accounts/logout/$', 'django.contrib.auth.views.logout'), … … urlpatterns = patterns('', 37 46 38 47 # Date-based generic views. 39 48 urlpatterns += 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), 40 53 (r'^date_based/object_detail/(?P<year>\d{4})/(?P<month>\d{1,2})/(?P<day>\d{1,2})/(?P<slug>[-\w]+)/$', 41 54 'object_detail', 42 55 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, 6 6 from .defaults import DefaultsTests 7 7 from .generic.create_update import (UpdateDeleteObjectTest, CreateObjectTest, 8 8 PostSaveRedirectTests, NoPostSaveNoAbsoluteUrl, AbsoluteUrlNoPostSave) 9 from .generic.date_based import MonthArchiveTest, ObjectDetailTest, DayArchiveTests9 from .generic.date_based import ArchiveIndexTest, MonthArchiveTest, ObjectDetailTest, DayArchiveTests 10 10 from .generic.object_list import ObjectListTest 11 11 from .generic.simple import RedirectToTest 12 12 from .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 5 5 from datetime import datetime, date 6 6 from datetime import timedelta 7 7 from regressiontests.views.models import Article, Author, DateArticle 8 from django.utils.functional import Promise 9 10 class 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') 8 41 9 42 class ObjectDetailTest(TestCase): 10 43 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
- + 1 This template intentionally left blank