Skip to content

Commit

Permalink
Fix crash on invalid date input
Browse files Browse the repository at this point in the history
  • Loading branch information
andreynovikov committed Dec 25, 2018
1 parent 8f44cd3 commit 7a388de
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 19 deletions.
21 changes: 12 additions & 9 deletions daterangefilter/filters.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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)
Expand Down
Binary file modified daterangefilter/locale/ru/LC_MESSAGES/django.mo
Binary file not shown.
25 changes: 15 additions & 10 deletions daterangefilter/locale/ru/LC_MESSAGES/django.po
Original file line number Diff line number Diff line change
Expand Up @@ -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 <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <[email protected]>\n"
Expand All @@ -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 "Завтра"

Expand Down Expand Up @@ -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 "Другой диапазон"

0 comments on commit 7a388de

Please sign in to comment.