diff --git a/daterangefilter/filters.py b/daterangefilter/filters.py index 5ac38b9..90a491b 100644 --- a/daterangefilter/filters.py +++ b/daterangefilter/filters.py @@ -1,7 +1,7 @@ import datetime from django.db import models -from django.contrib import admin +from django.contrib import admin, messages from django.utils import timezone from django.utils.translation import gettext_lazy as _ from django.conf import settings @@ -23,14 +23,17 @@ def __init__(self, field, request, params, model, model_admin, field_path): self.lookup_val = '{} - {}'.format(self.lookup_gte, self.lookup_lte) # if we are filtering DateTimeField we should add one day to final date if isinstance(model._meta.get_field(field_path), models.DateTimeField): - gte_date = datetime.datetime.strptime(self.lookup_gte, '%Y-%m-%d') - lte_date = datetime.datetime.strptime(self.lookup_lte, '%Y-%m-%d') - lte_date = lte_date + datetime.timedelta(seconds=3600*24-1) - if settings.USE_TZ: - gte_date = timezone.make_aware(gte_date, timezone.get_current_timezone()) - lte_date = timezone.make_aware(lte_date, timezone.get_current_timezone()) - params[self.lookup_kwarg_gte] = gte_date.strftime('%Y-%m-%d %H:%M:%S%z') - params[self.lookup_kwarg_lte] = lte_date.strftime('%Y-%m-%d %H:%M:%S%z') + try: + gte_date = datetime.datetime.strptime(self.lookup_gte, '%Y-%m-%d') + lte_date = datetime.datetime.strptime(self.lookup_lte, '%Y-%m-%d') + lte_date = lte_date + datetime.timedelta(seconds=3600*24-1) + if settings.USE_TZ: + gte_date = timezone.make_aware(gte_date, timezone.get_current_timezone()) + lte_date = timezone.make_aware(lte_date, timezone.get_current_timezone()) + params[self.lookup_kwarg_gte] = gte_date.strftime('%Y-%m-%d %H:%M:%S%z') + params[self.lookup_kwarg_lte] = lte_date.strftime('%Y-%m-%d %H:%M:%S%z') + except ValueError: + messages.add_message(request, messages.ERROR, _("Invalid date for '%(field_name)s' field range filter") % {'field_name': field.verbose_name}) else: self.lookup_val = '' super(DateRangeFilter, self).__init__(field, request, params, model, model_admin, field_path) diff --git a/daterangefilter/locale/ru/LC_MESSAGES/django.mo b/daterangefilter/locale/ru/LC_MESSAGES/django.mo index e6e69fa..5e93572 100644 Binary files a/daterangefilter/locale/ru/LC_MESSAGES/django.mo and b/daterangefilter/locale/ru/LC_MESSAGES/django.mo differ diff --git a/daterangefilter/locale/ru/LC_MESSAGES/django.po b/daterangefilter/locale/ru/LC_MESSAGES/django.po index 1c2fa1b..0ed4147 100644 --- a/daterangefilter/locale/ru/LC_MESSAGES/django.po +++ b/daterangefilter/locale/ru/LC_MESSAGES/django.po @@ -8,7 +8,7 @@ msgid "" msgstr "" "Project-Id-Version: PACKAGE VERSION\n" "Report-Msgid-Bugs-To: \n" -"POT-Creation-Date: 2018-12-13 14:51+0300\n" +"POT-Creation-Date: 2018-12-25 17:51+0300\n" "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n" "Last-Translator: FULL NAME \n" "Language-Team: LANGUAGE \n" @@ -24,18 +24,23 @@ msgstr "" msgid "Date Range Filter" msgstr "Фильтр диапазона дат" -#: templates/daterangefilter/_future_ranges.html:3 -#: templates/daterangefilter/_past_ranges.html:3 -msgid "Today" -msgstr "Сегодня" +#: filters.py:36 +#, python-format +msgid "Invalid date for '%(field_name)s' field range filter" +msgstr "Некорректная дата для фильтра диапазона дат поля '%(field_name)s'" -#: templates/daterangefilter/_future_ranges.html:4 +#: templates/daterangefilter/_future_ranges.html:3 #: templates/daterangefilter/_past_ranges.html:5 msgid "Yesterday" msgstr "Вчера" -#: templates/daterangefilter/_future_ranges.html:5 +#: templates/daterangefilter/_future_ranges.html:4 #: templates/daterangefilter/_past_ranges.html:4 +msgid "Today" +msgstr "Сегодня" + +#: templates/daterangefilter/_future_ranges.html:5 +#: templates/daterangefilter/_past_ranges.html:3 msgid "Tomorrow" msgstr "Завтра" @@ -68,12 +73,12 @@ msgstr "Предыдущая неделя" msgid "In the past" msgstr "В прошлом" -#: templates/daterangefilter/daterangefilter.html:11 +#: templates/daterangefilter/daterangefilter.html:10 #, python-format msgid " By %(filter_title)s " msgstr " %(filter_title)s " -#: templates/daterangefilter/daterangefilter.html:29 -#: templates/daterangefilter/suit_daterangefilter.html:25 +#: templates/daterangefilter/daterangefilter.html:31 +#: templates/daterangefilter/suit_daterangefilter.html:28 msgid "Custom Range" msgstr "Другой диапазон"