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) last_day = first_day.replace(year=first_day.year + 1, month=1)
else: else:
last_day = first_day.replace(month=first_day.month + 1) 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. # 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: 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. # Calculate first and last day of week, for use in a date-range lookup.
first_day = date first_day = date
last_day = date + datetime.timedelta(days=7) 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. # 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: 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) title = models.CharField(max_length=100)
slug = models.SlugField() slug = models.SlugField()
author = models.ForeignKey(Author) author = models.ForeignKey(Author)
date_created = models.DateTimeField()
class Meta: class Meta:
abstract = True abstract = True
@ -30,11 +29,20 @@ class BaseArticle(models.Model):
return self.title return self.title
class Article(BaseArticle): class Article(BaseArticle):
pass date_created = models.DateTimeField()
class UrlArticle(BaseArticle): class UrlArticle(BaseArticle):
""" """
An Article class with a get_absolute_url defined. An Article class with a get_absolute_url defined.
""" """
date_created = models.DateTimeField()
def get_absolute_url(self): def get_absolute_url(self):
return '/urlarticles/%s/' % self.slug 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

@ -2,7 +2,7 @@
from django.test import TestCase from django.test import TestCase
from datetime import datetime from datetime import datetime
from datetime import timedelta from datetime import timedelta
from regressiontests.views.models import Article, Author from regressiontests.views.models import Article, Author, DateArticle
class ObjectDetailTest(TestCase): class ObjectDetailTest(TestCase):
fixtures = ['testdata.json'] fixtures = ['testdata.json']
@ -68,4 +68,25 @@ class MonthArchiveTest(TestCase):
response = self.client.get('/views/date_based/archive_month/2004/02/') response = self.client.get('/views/date_based/archive_month/2004/02/')
self.assertEqual(response.status_code, 404) 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)

View File

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

View File

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