Ticket #7602: 7602.patch
File 7602.patch, 10.4 KB (added by , 16 years ago) |
---|
-
django/views/generic/date_based.py
129 129 last_day = first_day.replace(year=first_day.year + 1, month=1) 130 130 else: 131 131 last_day = first_day.replace(month=first_day.month + 1) 132 lookup_kwargs = {'%s__range' % date_field: (first_day, last_day)} 132 lookup_kwargs = { 133 '%s__gte' % date_field: first_day, 134 '%s__lt' % date_field: last_day, 135 } 133 136 134 137 # Only bother to check current date if the month isn't in the past and future objects are requested. 135 138 if last_day >= now.date() and not allow_future: … … 188 191 # Calculate first and last day of week, for use in a date-range lookup. 189 192 first_day = date 190 193 last_day = date + datetime.timedelta(days=7) 191 lookup_kwargs = {'%s__range' % date_field: (first_day, last_day)} 194 lookup_kwargs = { 195 '%s__gte' % date_field: first_day, 196 '%s__lt' % date_field: last_day, 197 } 192 198 193 199 # Only bother to check current date if the week isn't in the past and future objects aren't requested. 194 200 if last_day >= now.date() and not allow_future: -
tests/regressiontests/views/tests/generic/date_based.py
1 # coding: utf-8 2 from django.test import TestCase 3 from datetime import datetime 4 from datetime import timedelta 5 from regressiontests.views.models import Article, Author 1 # coding: utf-8 2 from django.test import TestCase 3 from datetime import datetime 4 from datetime import timedelta 5 from regressiontests.views.models import Article, Author, DateArticle 6 6 7 7 class ObjectDetailTest(TestCase): 8 fixtures = ['testdata.json'] 9 def setUp(self): 8 fixtures = ['testdata.json'] 9 def setUp(self): 10 10 # Correct the date for the current article 11 11 current_article = Article.objects.get(title="Current Article") 12 current_article.date_created = datetime.now() 13 current_article.save() 14 15 def test_finds_past(self): 12 current_article.date_created = datetime.now() 13 current_article.save() 14 15 def test_finds_past(self): 16 16 "date_based.object_detail can view a page in the past" 17 response = self.client.get('/views/date_based/object_detail/2001/01/01/old_article/') 18 self.assertEqual(response.status_code, 200) 19 self.assertEqual(response.context['object'].title, "Old Article") 20 21 def test_object_detail_finds_today(self): 17 response = self.client.get('/views/date_based/object_detail/2001/01/01/old_article/') 18 self.assertEqual(response.status_code, 200) 19 self.assertEqual(response.context['object'].title, "Old Article") 20 21 def test_object_detail_finds_today(self): 22 22 "date_based.object_detail can view a page from today" 23 today_url = datetime.now().strftime('%Y/%m/%d') 24 response = self.client.get('/views/date_based/object_detail/%s/current_article/' % today_url) 25 self.assertEqual(response.status_code, 200) 26 self.assertEqual(response.context['object'].title, "Current Article") 27 28 def test_object_detail_ignores_future(self): 23 today_url = datetime.now().strftime('%Y/%m/%d') 24 response = self.client.get('/views/date_based/object_detail/%s/current_article/' % today_url) 25 self.assertEqual(response.status_code, 200) 26 self.assertEqual(response.context['object'].title, "Current Article") 27 28 def test_object_detail_ignores_future(self): 29 29 "date_based.object_detail can view a page from the future, but only if allowed." 30 response = self.client.get('/views/date_based/object_detail/3000/01/01/future_article/') 31 self.assertEqual(response.status_code, 404) 30 response = self.client.get('/views/date_based/object_detail/3000/01/01/future_article/') 31 self.assertEqual(response.status_code, 404) 32 32 33 def test_object_detail_allowed_future_if_enabled(self): 33 def test_object_detail_allowed_future_if_enabled(self): 34 34 "date_based.object_detail can view a page from the future if explicitly allowed." 35 35 response = self.client.get('/views/date_based/object_detail/3000/01/01/future_article/allow_future/') 36 self.assertEqual(response.status_code, 200) 37 self.assertEqual(response.context['object'].title, "Future Article") 38 39 class MonthArchiveTest(TestCase): 40 def test_archive_month_includes_only_month(self): 36 self.assertEqual(response.status_code, 200) 37 self.assertEqual(response.context['object'].title, "Future Article") 38 39 class MonthArchiveTest(TestCase): 40 def test_archive_month_includes_only_month(self): 41 41 "Regression for #3031: Archives around Feburary include only one month" 42 42 author = Author(name="John Smith") 43 43 author.save() 44 44 45 # 2004 was a leap year, so it should be weird enough to not cheat 46 first_second_of_feb = datetime(2004, 2, 1, 0, 0, 1) 47 first_second_of_mar = datetime(2004, 3, 1, 0, 0, 1) 48 two_seconds = timedelta(0, 2, 0) 49 article = Article(title="example", author=author) 45 # 2004 was a leap year, so it should be weird enough to not cheat 46 first_second_of_feb = datetime(2004, 2, 1, 0, 0, 1) 47 first_second_of_mar = datetime(2004, 3, 1, 0, 0, 1) 48 two_seconds = timedelta(0, 2, 0) 49 article = Article(title="example", author=author) 50 50 51 article.date_created = first_second_of_feb 52 article.save() 53 response = self.client.get('/views/date_based/archive_month/2004/02/') 54 self.assertEqual(response.status_code, 200) 55 56 article.date_created = first_second_of_feb-two_seconds 57 article.save() 58 response = self.client.get('/views/date_based/archive_month/2004/02/') 59 self.assertEqual(response.status_code, 404) 51 article.date_created = first_second_of_feb 52 article.save() 53 response = self.client.get('/views/date_based/archive_month/2004/02/') 54 self.assertEqual(response.status_code, 200) 60 55 61 article.date_created = first_second_of_ mar-two_seconds62 article.save() 63 response = self.client.get('/views/date_based/archive_month/2004/02/') 64 self.assertEqual(response.status_code, 200)56 article.date_created = first_second_of_feb-two_seconds 57 article.save() 58 response = self.client.get('/views/date_based/archive_month/2004/02/') 59 self.assertEqual(response.status_code, 404) 65 60 66 article.date_created = first_second_of_mar 67 article.save() 68 response = self.client.get('/views/date_based/archive_month/2004/02/') 69 self.assertEqual(response.status_code, 404) 70 71 72 No newline at end of file 61 article.date_created = first_second_of_mar-two_seconds 62 article.save() 63 response = self.client.get('/views/date_based/archive_month/2004/02/') 64 self.assertEqual(response.status_code, 200) 65 66 article.date_created = first_second_of_mar 67 article.save() 68 response = self.client.get('/views/date_based/archive_month/2004/02/') 69 self.assertEqual(response.status_code, 404) 70 71 article2 = DateArticle(title="example", author=author) 72 73 article2.date_created = first_second_of_feb.date() 74 article2.save() 75 response = self.client.get('/views/date_based/datefield/archive_month/2004/02/') 76 self.assertEqual(response.status_code, 200) 77 78 article2.date_created = (first_second_of_feb-two_seconds).date() 79 article2.save() 80 response = self.client.get('/views/date_based/datefield/archive_month/2004/02/') 81 self.assertEqual(response.status_code, 404) 82 83 article2.date_created = (first_second_of_mar-two_seconds).date() 84 article2.save() 85 response = self.client.get('/views/date_based/datefield/archive_month/2004/02/') 86 self.assertEqual(response.status_code, 200) 87 88 article2.date_created = first_second_of_mar.date() 89 article2.save() 90 response = self.client.get('/views/date_based/datefield/archive_month/2004/02/') 91 self.assertEqual(response.status_code, 404) 92 -
tests/regressiontests/views/models.py
21 21 title = models.CharField(max_length=100) 22 22 slug = models.SlugField() 23 23 author = models.ForeignKey(Author) 24 date_created = models.DateTimeField()25 24 26 25 class Meta: 27 26 abstract = True … … 30 29 return self.title 31 30 32 31 class Article(BaseArticle): 33 pass32 date_created = models.DateTimeField() 34 33 35 34 class UrlArticle(BaseArticle): 36 35 """ 37 36 An Article class with a get_absolute_url defined. 38 37 """ 38 date_created = models.DateTimeField() 39 39 40 def get_absolute_url(self): 40 41 return '/urlarticles/%s/' % self.slug 42 43 class DateArticle(BaseArticle): 44 """ 45 An article Model with a DateField instead of DateTimeField, 46 for testing #7602 47 """ 48 date_created = models.DateField() -
tests/regressiontests/views/urls.py
20 20 'date_field': 'date_created', 21 21 'month_format': '%m', 22 22 } 23 date_based_datefield_info_dict = dict(date_based_info_dict, queryset=DateArticle.objects.all()) 23 24 24 25 urlpatterns = patterns('', 25 26 (r'^$', views.index_page), … … 48 49 (r'^date_based/archive_month/(?P<year>\d{4})/(?P<month>\d{1,2})/$', 49 50 'archive_month', 50 51 date_based_info_dict), 52 (r'^date_based/datefield/archive_month/(?P<year>\d{4})/(?P<month>\d{1,2})/$', 53 'archive_month', 54 date_based_datefield_info_dict), 51 55 ) 52 56 53 57 # crud generic views. -
tests/templates/views/datearticle_archive_month.html
1 This template intentionally left blank 2 No newline at end of file