Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #21186: Fixed regression when using date fields in the admin's …

…list_filter.

Thanks to onlygoldi2201 for the report and to ramiro and apollo13
for the reviews.
  • Loading branch information...
commit 8f51ba669aba94eea684ea3f3429fd8e39e70679 1 parent 8a4f5de
Baptiste Mispelon authored September 28, 2013
9  django/contrib/admin/filters.py
@@ -303,6 +303,11 @@ def __init__(self, field, request, params, model, model_admin, field_path):
303 303
         else:       # field is a models.DateField
304 304
             today = now.date()
305 305
         tomorrow = today + datetime.timedelta(days=1)
  306
+        if today.month == 12:
  307
+            next_month = today.replace(year=today.year + 1, month=1, day=1)
  308
+        else:
  309
+            next_month = today.replace(month=today.month + 1, day=1)
  310
+        next_year = today.replace(year=today.year + 1, month=1, day=1)
306 311
 
307 312
         self.lookup_kwarg_since = '%s__gte' % field_path
308 313
         self.lookup_kwarg_until = '%s__lt' % field_path
@@ -318,11 +323,11 @@ def __init__(self, field, request, params, model, model_admin, field_path):
318 323
             }),
319 324
             (_('This month'), {
320 325
                 self.lookup_kwarg_since: str(today.replace(day=1)),
321  
-                self.lookup_kwarg_until: str(tomorrow),
  326
+                self.lookup_kwarg_until: str(next_month),
322 327
             }),
323 328
             (_('This year'), {
324 329
                 self.lookup_kwarg_since: str(today.replace(month=1, day=1)),
325  
-                self.lookup_kwarg_until: str(tomorrow),
  330
+                self.lookup_kwarg_until: str(next_year),
326 331
             }),
327 332
         )
328 333
         super(DateFieldListFilter, self).__init__(
13  tests/admin_filters/tests.py
@@ -143,6 +143,11 @@ def setUp(self):
143 143
         self.today = datetime.date.today()
144 144
         self.tomorrow = self.today + datetime.timedelta(days=1)
145 145
         self.one_week_ago = self.today - datetime.timedelta(days=7)
  146
+        if self.today.month == 12:
  147
+            self.next_month = self.today.replace(year=self.today.year + 1, month=1, day=1)
  148
+        else:
  149
+            self.next_month = self.today.replace(month=self.today.month + 1, day=1)
  150
+        self.next_year = self.today.replace(year=self.today.year + 1, month=1, day=1)
146 151
 
147 152
         self.request_factory = RequestFactory()
148 153
 
@@ -196,7 +201,7 @@ def test_datefieldlistfilter(self):
196 201
                                                 % (self.today, self.tomorrow))
197 202
 
198 203
         request = self.request_factory.get('/', {'date_registered__gte': self.today.replace(day=1),
199  
-                                                 'date_registered__lt': self.tomorrow})
  204
+                                                 'date_registered__lt': self.next_month})
200 205
         changelist = self.get_changelist(request, Book, modeladmin)
201 206
 
202 207
         # Make sure the correct queryset is returned
@@ -214,10 +219,10 @@ def test_datefieldlistfilter(self):
214 219
         self.assertEqual(choice['selected'], True)
215 220
         self.assertEqual(choice['query_string'], '?date_registered__gte=%s'
216 221
                                                  '&date_registered__lt=%s'
217  
-                                                % (self.today.replace(day=1), self.tomorrow))
  222
+                                                % (self.today.replace(day=1), self.next_month))
218 223
 
219 224
         request = self.request_factory.get('/', {'date_registered__gte': self.today.replace(month=1, day=1),
220  
-                                                 'date_registered__lt': self.tomorrow})
  225
+                                                 'date_registered__lt': self.next_year})
221 226
         changelist = self.get_changelist(request, Book, modeladmin)
222 227
 
223 228
         # Make sure the correct queryset is returned
@@ -235,7 +240,7 @@ def test_datefieldlistfilter(self):
235 240
         self.assertEqual(choice['selected'], True)
236 241
         self.assertEqual(choice['query_string'], '?date_registered__gte=%s'
237 242
                                                  '&date_registered__lt=%s'
238  
-                                                % (self.today.replace(month=1, day=1), self.tomorrow))
  243
+                                                % (self.today.replace(month=1, day=1), self.next_year))
239 244
 
240 245
         request = self.request_factory.get('/', {'date_registered__gte': str(self.one_week_ago),
241 246
                                                  'date_registered__lt': str(self.tomorrow)})

0 notes on commit 8f51ba6

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