Ticket #17830: 17830.diff

File 17830.diff, 9.6 KB (added by Aymeric Augustin, 13 years ago)
  • tests/regressiontests/admin_filters/tests.py

     
    1010from django.contrib.auth.models import User
    1111from django.core.exceptions import ImproperlyConfigured
    1212from django.test import TestCase, RequestFactory
     13from django.test.utils import override_settings
    1314from django.utils.encoding import force_unicode
    1415
    1516from .models import Book
     
    115116
    116117    def setUp(self):
    117118        self.today = datetime.date.today()
     119        self.tomorrow = self.today + datetime.timedelta(days=1)
    118120        self.one_week_ago = self.today - datetime.timedelta(days=7)
    119121
    120122        self.request_factory = RequestFactory()
     
    143145        request = self.request_factory.get('/')
    144146        changelist = self.get_changelist(request, Book, modeladmin)
    145147
    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})
    149150        changelist = self.get_changelist(request, Book, modeladmin)
    150151
    151152        # Make sure the correct queryset is returned
     
    157158        self.assertEqual(force_unicode(filterspec.title), u'date registered')
    158159        choice = select_by(filterspec.choices(changelist), "display", "Today")
    159160        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))
    164164
    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})
    167167        changelist = self.get_changelist(request, Book, modeladmin)
    168168
    169169        # Make sure the correct queryset is returned
     
    179179        self.assertEqual(force_unicode(filterspec.title), u'date registered')
    180180        choice = select_by(filterspec.choices(changelist), "display", "This month")
    181181        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))
    185185
    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})
    187188        changelist = self.get_changelist(request, Book, modeladmin)
    188189
    189190        # Make sure the correct queryset is returned
     
    199200        self.assertEqual(force_unicode(filterspec.title), u'date registered')
    200201        choice = select_by(filterspec.choices(changelist), "display", "This year")
    201202        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))
    204206
    205207        request = self.request_factory.get('/', {'date_registered__gte': str(self.one_week_ago),
    206                                                  'date_registered__lte': str(self.today)})
     208                                                 'date_registered__lt': str(self.tomorrow)})
    207209        changelist = self.get_changelist(request, Book, modeladmin)
    208210
    209211        # Make sure the correct queryset is returned
     
    216218        choice = select_by(filterspec.choices(changelist), "display", "Past 7 days")
    217219        self.assertEqual(choice['selected'], True)
    218220        self.assertEqual(choice['query_string'], '?date_registered__gte=%s'
    219                                                  '&date_registered__lte=%s'
    220                                                 % (str(self.one_week_ago), str(self.today)))
     221                                                 '&date_registered__lt=%s'
     222                                                % (str(self.one_week_ago), str(self.tomorrow)))
    221223
     224    @override_settings(USE_TZ=True)
     225    def test_datefieldlistfilter_with_time_zone_support(self):
     226        # Regression for #17830
     227        self.test_datefieldlistfilter()
     228
    222229    def test_allvaluesfieldlistfilter(self):
    223230        modeladmin = BookAdmin(Book, site)
    224231
  • django/contrib/admin/filters.py

     
    1111from django.core.exceptions import ImproperlyConfigured
    1212from django.utils.encoding import smart_unicode
    1313from django.utils.translation import ugettext_lazy as _
     14from django.utils import timezone
    1415
    1516from django.contrib.admin.util import (get_model_from_relation,
    1617    reverse_field_path, get_limit_choices_to_from_path, prepare_lookup_value)
     
    282283        self.field_generic = '%s__' % field_path
    283284        self.date_params = dict([(k, v) for k, v in params.items()
    284285                                 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
    288297        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
    295305        self.links = (
    296306            (_('Any date'), {}),
    297307            (_('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),
    301310            }),
    302311            (_('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),
    305314            }),
    306315            (_('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),
    309318            }),
    310319            (_('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),
    312322            }),
    313323        )
    314324        super(DateFieldListFilter, self).__init__(
    315325            field, request, params, model, model_admin, field_path)
    316326
    317327    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]
    323329
    324330    def choices(self, cl):
    325331        for title, param_dict in self.links:
Back to Top