Fixed #7602 -- Corrected lookup keyword arguments in archive_month and archive_week to properly range when date_field is from DateField. Thanks nullie for the original patch and Colin Grady for the test coverage.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@8476 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Brian Rosner 2008-08-23 04:46:33 +00:00
parent 264771f1b6
commit a64dc39fb7
5 changed files with 101 additions and 61 deletions

View File

@ -129,7 +129,10 @@ def archive_month(request, year, month, queryset, date_field,
last_day = first_day.replace(year=first_day.year + 1, month=1)
else:
last_day = first_day.replace(month=first_day.month + 1)
lookup_kwargs = {'%s__range' % date_field: (first_day, last_day)}
lookup_kwargs = {
'%s__gte' % date_field: first_day,
'%s__lt' % date_field: last_day,
}
# Only bother to check current date if the month isn't in the past and future objects are requested.
if last_day >= now.date() and not allow_future:
@ -188,7 +191,10 @@ def archive_week(request, year, week, queryset, date_field,
# Calculate first and last day of week, for use in a date-range lookup.
first_day = date
last_day = date + datetime.timedelta(days=7)
lookup_kwargs = {'%s__range' % date_field: (first_day, last_day)}
lookup_kwargs = {
'%s__gte' % date_field: first_day,
'%s__lt' % date_field: last_day,
}
# Only bother to check current date if the week isn't in the past and future objects aren't requested.
if last_day >= now.date() and not allow_future:

View File

@ -21,7 +21,6 @@ class BaseArticle(models.Model):
title = models.CharField(max_length=100)
slug = models.SlugField()
author = models.ForeignKey(Author)
date_created = models.DateTimeField()
class Meta:
abstract = True
@ -30,11 +29,20 @@ class BaseArticle(models.Model):
return self.title
class Article(BaseArticle):
pass
date_created = models.DateTimeField()
class UrlArticle(BaseArticle):
"""
An Article class with a get_absolute_url defined.
"""
date_created = models.DateTimeField()
def get_absolute_url(self):
return '/urlarticles/%s/' % self.slug
class DateArticle(BaseArticle):
"""
An article Model with a DateField instead of DateTimeField,
for testing #7602
"""
date_created = models.DateField()

View File

@ -1,71 +1,92 @@
# coding: utf-8
from django.test import TestCase
from datetime import datetime
from datetime import timedelta
from regressiontests.views.models import Article, Author
# coding: utf-8
from django.test import TestCase
from datetime import datetime
from datetime import timedelta
from regressiontests.views.models import Article, Author, DateArticle
class ObjectDetailTest(TestCase):
fixtures = ['testdata.json']
def setUp(self):
fixtures = ['testdata.json']
def setUp(self):
# Correct the date for the current article
current_article = Article.objects.get(title="Current Article")
current_article.date_created = datetime.now()
current_article.save()
def test_finds_past(self):
"date_based.object_detail can view a page in the past"
response = self.client.get('/views/date_based/object_detail/2001/01/01/old_article/')
self.assertEqual(response.status_code, 200)
self.assertEqual(response.context['object'].title, "Old Article")
def test_object_detail_finds_today(self):
"date_based.object_detail can view a page from today"
today_url = datetime.now().strftime('%Y/%m/%d')
response = self.client.get('/views/date_based/object_detail/%s/current_article/' % today_url)
self.assertEqual(response.status_code, 200)
self.assertEqual(response.context['object'].title, "Current Article")
def test_object_detail_ignores_future(self):
"date_based.object_detail can view a page from the future, but only if allowed."
response = self.client.get('/views/date_based/object_detail/3000/01/01/future_article/')
self.assertEqual(response.status_code, 404)
current_article.date_created = datetime.now()
current_article.save()
def test_object_detail_allowed_future_if_enabled(self):
def test_finds_past(self):
"date_based.object_detail can view a page in the past"
response = self.client.get('/views/date_based/object_detail/2001/01/01/old_article/')
self.assertEqual(response.status_code, 200)
self.assertEqual(response.context['object'].title, "Old Article")
def test_object_detail_finds_today(self):
"date_based.object_detail can view a page from today"
today_url = datetime.now().strftime('%Y/%m/%d')
response = self.client.get('/views/date_based/object_detail/%s/current_article/' % today_url)
self.assertEqual(response.status_code, 200)
self.assertEqual(response.context['object'].title, "Current Article")
def test_object_detail_ignores_future(self):
"date_based.object_detail can view a page from the future, but only if allowed."
response = self.client.get('/views/date_based/object_detail/3000/01/01/future_article/')
self.assertEqual(response.status_code, 404)
def test_object_detail_allowed_future_if_enabled(self):
"date_based.object_detail can view a page from the future if explicitly allowed."
response = self.client.get('/views/date_based/object_detail/3000/01/01/future_article/allow_future/')
self.assertEqual(response.status_code, 200)
self.assertEqual(response.context['object'].title, "Future Article")
class MonthArchiveTest(TestCase):
def test_archive_month_includes_only_month(self):
self.assertEqual(response.status_code, 200)
self.assertEqual(response.context['object'].title, "Future Article")
class MonthArchiveTest(TestCase):
def test_archive_month_includes_only_month(self):
"Regression for #3031: Archives around Feburary include only one month"
author = Author(name="John Smith")
author.save()
# 2004 was a leap year, so it should be weird enough to not cheat
first_second_of_feb = datetime(2004, 2, 1, 0, 0, 1)
first_second_of_mar = datetime(2004, 3, 1, 0, 0, 1)
two_seconds = timedelta(0, 2, 0)
article = Article(title="example", author=author)
# 2004 was a leap year, so it should be weird enough to not cheat
first_second_of_feb = datetime(2004, 2, 1, 0, 0, 1)
first_second_of_mar = datetime(2004, 3, 1, 0, 0, 1)
two_seconds = timedelta(0, 2, 0)
article = Article(title="example", author=author)
article.date_created = first_second_of_feb
article.save()
response = self.client.get('/views/date_based/archive_month/2004/02/')
self.assertEqual(response.status_code, 200)
article.date_created = first_second_of_feb-two_seconds
article.save()
response = self.client.get('/views/date_based/archive_month/2004/02/')
self.assertEqual(response.status_code, 404)
article.date_created = first_second_of_feb
article.save()
response = self.client.get('/views/date_based/archive_month/2004/02/')
self.assertEqual(response.status_code, 200)
article.date_created = first_second_of_mar-two_seconds
article.save()
response = self.client.get('/views/date_based/archive_month/2004/02/')
self.assertEqual(response.status_code, 200)
article.date_created = first_second_of_feb-two_seconds
article.save()
response = self.client.get('/views/date_based/archive_month/2004/02/')
self.assertEqual(response.status_code, 404)
article.date_created = first_second_of_mar-two_seconds
article.save()
response = self.client.get('/views/date_based/archive_month/2004/02/')
self.assertEqual(response.status_code, 200)
article.date_created = first_second_of_mar
article.save()
response = self.client.get('/views/date_based/archive_month/2004/02/')
self.assertEqual(response.status_code, 404)
article2 = DateArticle(title="example", author=author)
article2.date_created = first_second_of_feb.date()
article2.save()
response = self.client.get('/views/date_based/datefield/archive_month/2004/02/')
self.assertEqual(response.status_code, 200)
article2.date_created = (first_second_of_feb-two_seconds).date()
article2.save()
response = self.client.get('/views/date_based/datefield/archive_month/2004/02/')
self.assertEqual(response.status_code, 404)
article2.date_created = (first_second_of_mar-two_seconds).date()
article2.save()
response = self.client.get('/views/date_based/datefield/archive_month/2004/02/')
self.assertEqual(response.status_code, 200)
article2.date_created = first_second_of_mar.date()
article2.save()
response = self.client.get('/views/date_based/datefield/archive_month/2004/02/')
self.assertEqual(response.status_code, 404)
article.date_created = first_second_of_mar
article.save()
response = self.client.get('/views/date_based/archive_month/2004/02/')
self.assertEqual(response.status_code, 404)

View File

@ -20,6 +20,7 @@ date_based_info_dict = {
'date_field': 'date_created',
'month_format': '%m',
}
date_based_datefield_info_dict = dict(date_based_info_dict, queryset=DateArticle.objects.all())
urlpatterns = patterns('',
(r'^$', views.index_page),
@ -48,6 +49,9 @@ urlpatterns += patterns('django.views.generic.date_based',
(r'^date_based/archive_month/(?P<year>\d{4})/(?P<month>\d{1,2})/$',
'archive_month',
date_based_info_dict),
(r'^date_based/datefield/archive_month/(?P<year>\d{4})/(?P<month>\d{1,2})/$',
'archive_month',
date_based_datefield_info_dict),
)
# crud generic views.

View File

@ -0,0 +1 @@
This template intentionally left blank