diff options
| author | Aymeric Augustin <aymeric.augustin@m4x.org> | 2012-03-05 12:27:19 +0000 |
|---|---|---|
| committer | Aymeric Augustin <aymeric.augustin@m4x.org> | 2012-03-05 12:27:19 +0000 |
| commit | bf0abe0ea6cb492810c5b4f9306a19c8afe603bb (patch) | |
| tree | ab1b252edf2ab2a22bad3050a8128046a190be5c /django | |
| parent | f95141abcc8a56c533500befac7baf2ae25b5a5a (diff) | |
Fixed #17830 -- Modified list_filter on DateTimeFields to account for the new time zone support. Thanks Glenn Washburn for the report and Jannis Leidel for the review.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@17670 bcc190cf-cafb-0310-a4f2-bffc1f526a37
Diffstat (limited to 'django')
| -rw-r--r-- | django/contrib/admin/filters.py | 50 |
1 files changed, 28 insertions, 22 deletions
diff --git a/django/contrib/admin/filters.py b/django/contrib/admin/filters.py index 550683bdd9..b64445ebfd 100644 --- a/django/contrib/admin/filters.py +++ b/django/contrib/admin/filters.py @@ -11,6 +11,7 @@ from django.db import models from django.core.exceptions import ImproperlyConfigured from django.utils.encoding import smart_unicode from django.utils.translation import ugettext_lazy as _ +from django.utils import timezone from django.contrib.admin.util import (get_model_from_relation, reverse_field_path, get_limit_choices_to_from_path, prepare_lookup_value) @@ -282,44 +283,49 @@ class DateFieldListFilter(FieldListFilter): self.field_generic = '%s__' % field_path self.date_params = dict([(k, v) for k, v in params.items() if k.startswith(self.field_generic)]) - today = datetime.date.today() - one_week_ago = today - datetime.timedelta(days=7) - today_str = str(today) + + now = timezone.now() + # When time zone support is enabled, convert "now" to the user's time + # zone so Django's definition of "Today" matches what the user expects. + if now.tzinfo is not None: + current_tz = timezone.get_current_timezone() + now = now.astimezone(current_tz) + if hasattr(current_tz, 'normalize'): + # available for pytz time zones + now = current_tz.normalize(now) + if isinstance(field, models.DateTimeField): - today_str += ' 23:59:59' - self.lookup_kwarg_year = '%s__year' % field_path - self.lookup_kwarg_month = '%s__month' % field_path - self.lookup_kwarg_day = '%s__day' % field_path - self.lookup_kwarg_past_7_days_gte = '%s__gte' % field_path - self.lookup_kwarg_past_7_days_lte = '%s__lte' % field_path + today = now.replace(hour=0, minute=0, second=0, microsecond=0) + else: # field is a models.DateField + today = now.date() + tomorrow = today + datetime.timedelta(days=1) + + self.lookup_kwarg_since = '%s__gte' % field_path + self.lookup_kwarg_until = '%s__lt' % field_path self.links = ( (_('Any date'), {}), (_('Today'), { - self.lookup_kwarg_year: str(today.year), - self.lookup_kwarg_month: str(today.month), - self.lookup_kwarg_day: str(today.day), + self.lookup_kwarg_since: str(today), + self.lookup_kwarg_until: str(tomorrow), }), (_('Past 7 days'), { - self.lookup_kwarg_past_7_days_gte: str(one_week_ago), - self.lookup_kwarg_past_7_days_lte: today_str, + self.lookup_kwarg_since: str(today - datetime.timedelta(days=7)), + self.lookup_kwarg_until: str(tomorrow), }), (_('This month'), { - self.lookup_kwarg_year: str(today.year), - self.lookup_kwarg_month: str(today.month), + self.lookup_kwarg_since: str(today.replace(day=1)), + self.lookup_kwarg_until: str(tomorrow), }), (_('This year'), { - self.lookup_kwarg_year: str(today.year), + self.lookup_kwarg_since: str(today.replace(month=1, day=1)), + self.lookup_kwarg_until: str(tomorrow), }), ) super(DateFieldListFilter, self).__init__( field, request, params, model, model_admin, field_path) def expected_parameters(self): - return [ - self.lookup_kwarg_year, self.lookup_kwarg_month, - self.lookup_kwarg_day, self.lookup_kwarg_past_7_days_gte, - self.lookup_kwarg_past_7_days_lte - ] + return [self.lookup_kwarg_since, self.lookup_kwarg_until] def choices(self, cl): for title, param_dict in self.links: |
