Use multiple date formats in DateRange validator
This commit is contained in:
parent
cb4f1281d3
commit
168436b156
12
atst/domain/date.py
Normal file
12
atst/domain/date.py
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
import pendulum
|
||||||
|
|
||||||
|
|
||||||
|
def parse_date(data):
|
||||||
|
date_formats = ["YYYY-MM-DD", "MM/DD/YYYY"]
|
||||||
|
for _format in date_formats:
|
||||||
|
try:
|
||||||
|
return pendulum.from_format(data, _format).date()
|
||||||
|
except (ValueError, pendulum.parsing.exceptions.ParserError):
|
||||||
|
pass
|
||||||
|
|
||||||
|
raise ValueError("Unable to parse string {}".format(data))
|
@ -1,20 +1,14 @@
|
|||||||
from wtforms.fields.html5 import DateField
|
from wtforms.fields.html5 import DateField
|
||||||
from wtforms.fields import Field
|
from wtforms.fields import Field
|
||||||
from wtforms.widgets import TextArea
|
from wtforms.widgets import TextArea
|
||||||
import pendulum
|
|
||||||
|
from atst.domain.date import parse_date
|
||||||
|
|
||||||
|
|
||||||
class DateField(DateField):
|
class DateField(DateField):
|
||||||
def _value(self):
|
def _value(self):
|
||||||
if self.data:
|
if self.data:
|
||||||
date_formats = ["YYYY-MM-DD", "MM/DD/YYYY"]
|
return parse_date(self.data)
|
||||||
for _format in date_formats:
|
|
||||||
try:
|
|
||||||
return pendulum.from_format(self.data, _format).date()
|
|
||||||
except (ValueError, pendulum.parsing.exceptions.ParserError):
|
|
||||||
pass
|
|
||||||
|
|
||||||
raise ValueError("Unable to parse string {}".format(self.data))
|
|
||||||
else:
|
else:
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
@ -2,18 +2,19 @@ import re
|
|||||||
from wtforms.validators import ValidationError
|
from wtforms.validators import ValidationError
|
||||||
import pendulum
|
import pendulum
|
||||||
|
|
||||||
|
from atst.domain.date import parse_date
|
||||||
|
|
||||||
|
|
||||||
def DateRange(lower_bound=None, upper_bound=None, message=None):
|
def DateRange(lower_bound=None, upper_bound=None, message=None):
|
||||||
def _date_range(form, field):
|
def _date_range(form, field):
|
||||||
now = pendulum.now().date()
|
now = pendulum.now().date()
|
||||||
|
date = parse_date(field.data)
|
||||||
|
|
||||||
if lower_bound is not None:
|
if lower_bound is not None:
|
||||||
date = pendulum.parse(field.data).date()
|
|
||||||
if (now - lower_bound) > date:
|
if (now - lower_bound) > date:
|
||||||
raise ValidationError(message)
|
raise ValidationError(message)
|
||||||
|
|
||||||
if upper_bound is not None:
|
if upper_bound is not None:
|
||||||
date = pendulum.parse(field.data).date()
|
|
||||||
if (now + upper_bound) < date:
|
if (now + upper_bound) < date:
|
||||||
raise ValidationError(message)
|
raise ValidationError(message)
|
||||||
|
|
||||||
|
21
tests/domain/test_date.py
Normal file
21
tests/domain/test_date.py
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
import pytest
|
||||||
|
import pendulum
|
||||||
|
|
||||||
|
from atst.domain.date import parse_date
|
||||||
|
|
||||||
|
|
||||||
|
def test_date_with_slashes():
|
||||||
|
date_str = "1/2/2020"
|
||||||
|
assert parse_date(date_str) == pendulum.date(2020, 1, 2)
|
||||||
|
|
||||||
|
|
||||||
|
def test_date_with_dashes():
|
||||||
|
date_str = "2020-1-2"
|
||||||
|
assert parse_date(date_str) == pendulum.date(2020, 1, 2)
|
||||||
|
|
||||||
|
|
||||||
|
def test_invalid_date():
|
||||||
|
date_str = "This is not a valid data"
|
||||||
|
with pytest.raises(ValueError):
|
||||||
|
parse_date(date_str)
|
||||||
|
|
Loading…
x
Reference in New Issue
Block a user