Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #7602 -- Corrected lookup keyword arguments in archive_month an…

…d 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
  • Loading branch information...
commit a64dc39fb7c6787c83ea2e807c5bf2f94ad5909d 1 parent 264771f
Brian Rosner authored
10  django/views/generic/date_based.py
@@ -129,7 +129,10 @@ def archive_month(request, year, month, queryset, date_field,
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,7 +191,10 @@ def archive_week(request, year, week, queryset, date_field,
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:
12  tests/regressiontests/views/models.py
@@ -21,7 +21,6 @@ class BaseArticle(models.Model):
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,11 +29,20 @@ def __unicode__(self):
30 29
         return self.title
31 30
 
32 31
 class Article(BaseArticle):
33  
-    pass
  32
+    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()
135  tests/regressiontests/views/tests/generic/date_based.py
... ...
@@ -1,71 +1,92 @@
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) 
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) 
60  
-
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  
-	 
  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
+
  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)
  60
+
  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
+
4  tests/regressiontests/views/urls.py
@@ -20,6 +20,7 @@
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,6 +49,9 @@
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.
1  tests/templates/views/datearticle_archive_month.html
... ...
@@ -0,0 +1 @@
  1
+This template intentionally left blank

0 notes on commit a64dc39

Please sign in to comment.
Something went wrong with that request. Please try again.