Ticket #17830: 17830.diff
File 17830.diff, 9.6 KB (added by , 13 years ago) |
---|
-
tests/regressiontests/admin_filters/tests.py
10 10 from django.contrib.auth.models import User 11 11 from django.core.exceptions import ImproperlyConfigured 12 12 from django.test import TestCase, RequestFactory 13 from django.test.utils import override_settings 13 14 from django.utils.encoding import force_unicode 14 15 15 16 from .models import Book … … 115 116 116 117 def setUp(self): 117 118 self.today = datetime.date.today() 119 self.tomorrow = self.today + datetime.timedelta(days=1) 118 120 self.one_week_ago = self.today - datetime.timedelta(days=7) 119 121 120 122 self.request_factory = RequestFactory() … … 143 145 request = self.request_factory.get('/') 144 146 changelist = self.get_changelist(request, Book, modeladmin) 145 147 146 request = self.request_factory.get('/', {'date_registered__year': self.today.year, 147 'date_registered__month': self.today.month, 148 'date_registered__day': self.today.day}) 148 request = self.request_factory.get('/', {'date_registered__gte': self.today, 149 'date_registered__lt': self.tomorrow}) 149 150 changelist = self.get_changelist(request, Book, modeladmin) 150 151 151 152 # Make sure the correct queryset is returned … … 157 158 self.assertEqual(force_unicode(filterspec.title), u'date registered') 158 159 choice = select_by(filterspec.choices(changelist), "display", "Today") 159 160 self.assertEqual(choice['selected'], True) 160 self.assertEqual(choice['query_string'], '?date_registered__day=%s' 161 '&date_registered__month=%s' 162 '&date_registered__year=%s' 163 % (self.today.day, self.today.month, self.today.year)) 161 self.assertEqual(choice['query_string'], '?date_registered__gte=%s' 162 '&date_registered__lt=%s' 163 % (self.today, self.tomorrow)) 164 164 165 request = self.request_factory.get('/', {'date_registered__ year': self.today.year,166 'date_registered__ month': self.today.month})165 request = self.request_factory.get('/', {'date_registered__gte': self.today.replace(day=1), 166 'date_registered__lt': self.tomorrow}) 167 167 changelist = self.get_changelist(request, Book, modeladmin) 168 168 169 169 # Make sure the correct queryset is returned … … 179 179 self.assertEqual(force_unicode(filterspec.title), u'date registered') 180 180 choice = select_by(filterspec.choices(changelist), "display", "This month") 181 181 self.assertEqual(choice['selected'], True) 182 self.assertEqual(choice['query_string'], '?date_registered__ month=%s'183 '&date_registered__ year=%s'184 % (self.today. month, self.today.year))182 self.assertEqual(choice['query_string'], '?date_registered__gte=%s' 183 '&date_registered__lt=%s' 184 % (self.today.replace(day=1), self.tomorrow)) 185 185 186 request = self.request_factory.get('/', {'date_registered__year': self.today.year}) 186 request = self.request_factory.get('/', {'date_registered__gte': self.today.replace(month=1, day=1), 187 'date_registered__lt': self.tomorrow}) 187 188 changelist = self.get_changelist(request, Book, modeladmin) 188 189 189 190 # Make sure the correct queryset is returned … … 199 200 self.assertEqual(force_unicode(filterspec.title), u'date registered') 200 201 choice = select_by(filterspec.choices(changelist), "display", "This year") 201 202 self.assertEqual(choice['selected'], True) 202 self.assertEqual(choice['query_string'], '?date_registered__year=%s' 203 % (self.today.year)) 203 self.assertEqual(choice['query_string'], '?date_registered__gte=%s' 204 '&date_registered__lt=%s' 205 % (self.today.replace(month=1, day=1), self.tomorrow)) 204 206 205 207 request = self.request_factory.get('/', {'date_registered__gte': str(self.one_week_ago), 206 'date_registered__lt e': str(self.today)})208 'date_registered__lt': str(self.tomorrow)}) 207 209 changelist = self.get_changelist(request, Book, modeladmin) 208 210 209 211 # Make sure the correct queryset is returned … … 216 218 choice = select_by(filterspec.choices(changelist), "display", "Past 7 days") 217 219 self.assertEqual(choice['selected'], True) 218 220 self.assertEqual(choice['query_string'], '?date_registered__gte=%s' 219 '&date_registered__lt e=%s'220 % (str(self.one_week_ago), str(self.to day)))221 '&date_registered__lt=%s' 222 % (str(self.one_week_ago), str(self.tomorrow))) 221 223 224 @override_settings(USE_TZ=True) 225 def test_datefieldlistfilter_with_time_zone_support(self): 226 # Regression for #17830 227 self.test_datefieldlistfilter() 228 222 229 def test_allvaluesfieldlistfilter(self): 223 230 modeladmin = BookAdmin(Book, site) 224 231 -
django/contrib/admin/filters.py
11 11 from django.core.exceptions import ImproperlyConfigured 12 12 from django.utils.encoding import smart_unicode 13 13 from django.utils.translation import ugettext_lazy as _ 14 from django.utils import timezone 14 15 15 16 from django.contrib.admin.util import (get_model_from_relation, 16 17 reverse_field_path, get_limit_choices_to_from_path, prepare_lookup_value) … … 282 283 self.field_generic = '%s__' % field_path 283 284 self.date_params = dict([(k, v) for k, v in params.items() 284 285 if k.startswith(self.field_generic)]) 285 today = datetime.date.today() 286 one_week_ago = today - datetime.timedelta(days=7) 287 today_str = str(today) 286 287 now = timezone.now() 288 # When time zone support is enabled, convert "now" to the user's time 289 # zone so that the definition of "Today" matches what the user expect. 290 if now.tzinfo is not None: 291 current_tz = timezone.get_current_timezone() 292 now = now.astimezone(current_tz) 293 if hasattr(current_tz, 'normalize'): 294 # available for pytz time zones 295 now = current_tz.normalize(now) 296 288 297 if isinstance(field, models.DateTimeField): 289 today_str += ' 23:59:59' 290 self.lookup_kwarg_year = '%s__year' % field_path 291 self.lookup_kwarg_month = '%s__month' % field_path 292 self.lookup_kwarg_day = '%s__day' % field_path 293 self.lookup_kwarg_past_7_days_gte = '%s__gte' % field_path 294 self.lookup_kwarg_past_7_days_lte = '%s__lte' % field_path 298 today = now.replace(hour=0, minute=0, second=0, microsecond=0) 299 else: # field is a models.DateField 300 today = now.date() 301 tomorrow = today + datetime.timedelta(days=1) 302 303 self.lookup_kwarg_since = '%s__gte' % field_path 304 self.lookup_kwarg_until = '%s__lt' % field_path 295 305 self.links = ( 296 306 (_('Any date'), {}), 297 307 (_('Today'), { 298 self.lookup_kwarg_year: str(today.year), 299 self.lookup_kwarg_month: str(today.month), 300 self.lookup_kwarg_day: str(today.day), 308 self.lookup_kwarg_since: str(today), 309 self.lookup_kwarg_until: str(tomorrow), 301 310 }), 302 311 (_('Past 7 days'), { 303 self.lookup_kwarg_ past_7_days_gte: str(one_week_ago),304 self.lookup_kwarg_ past_7_days_lte: today_str,312 self.lookup_kwarg_since: str(today - datetime.timedelta(days=7)), 313 self.lookup_kwarg_until: str(tomorrow), 305 314 }), 306 315 (_('This month'), { 307 self.lookup_kwarg_ year: str(today.year),308 self.lookup_kwarg_ month: str(today.month),316 self.lookup_kwarg_since: str(today.replace(day=1)), 317 self.lookup_kwarg_until: str(tomorrow), 309 318 }), 310 319 (_('This year'), { 311 self.lookup_kwarg_year: str(today.year), 320 self.lookup_kwarg_since: str(today.replace(month=1, day=1)), 321 self.lookup_kwarg_until: str(tomorrow), 312 322 }), 313 323 ) 314 324 super(DateFieldListFilter, self).__init__( 315 325 field, request, params, model, model_admin, field_path) 316 326 317 327 def expected_parameters(self): 318 return [ 319 self.lookup_kwarg_year, self.lookup_kwarg_month, 320 self.lookup_kwarg_day, self.lookup_kwarg_past_7_days_gte, 321 self.lookup_kwarg_past_7_days_lte 322 ] 328 return [self.lookup_kwarg_since, self.lookup_kwarg_until] 323 329 324 330 def choices(self, cl): 325 331 for title, param_dict in self.links: