Skip to content

Commit 0725acd

Browse files
saharnaz-rashidiSaharnaz Rashidi
andauthored
[improve] Respect jDateField's input_formats kwarg when parsing date (#257)
* [improve] Respect jDateField's input_formats kwarg when parsing date strings * [bugfix] Use python 3.8 compatible type annotation --------- Co-authored-by: Saharnaz Rashidi <[email protected]>
1 parent fda83a6 commit 0725acd

File tree

2 files changed

+71
-2
lines changed

2 files changed

+71
-2
lines changed

django_jalali/forms/__init__.py

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import re
2+
from typing import Union
23

34
import jdatetime
45
from django import forms
@@ -19,10 +20,10 @@ def __init__(self, input_formats=None, *args, **kwargs):
1920
super().__init__(*args, **kwargs)
2021
self.input_formats = input_formats
2122

22-
def to_python(self, value):
23+
def to_python(self, value) -> Union[jdatetime.date, None]:
2324
"""
2425
Validates that the input can be converted to a date. Returns a Python
25-
datetime.date object.
26+
jdatetime.date object.
2627
"""
2728
if value in validators.EMPTY_VALUES:
2829
return None
@@ -31,6 +32,13 @@ def to_python(self, value):
3132
if isinstance(value, jdatetime.date):
3233
return value
3334

35+
if self.input_formats:
36+
for input_format in self.input_formats:
37+
try:
38+
return jdatetime.datetime.strptime(value, input_format).date()
39+
except ValueError:
40+
pass
41+
3442
groups = re.search(
3543
r"(?P<year>[\d]{1,4})-(?P<month>[\d]{1,2})-(?P<day>[\d]{1,2})",
3644
value,
@@ -87,6 +95,13 @@ def to_python(self, value):
8795
return None
8896
value = "%s %s" % tuple(value)
8997

98+
if self.input_formats:
99+
for input_format in self.input_formats:
100+
try:
101+
return jdatetime.datetime.strptime(value, input_format).date()
102+
except ValueError:
103+
pass
104+
90105
groups = re.search(
91106
r"(?P<year>[\d]{1,4})-(?P<month>[\d]{1,2})-(?P<day>[\d]{1,2}) "
92107
r"(?P<hour>[\d]{1,2}):(?P<minute>[\d]{1,2})"

tests/test_forms.py

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,33 @@ def test_field(self):
1616
f = jDateField()
1717
self.assertEqual(f.clean(value), jdatetime.date(1400, 11, 27))
1818

19+
def test_field_with_one_input_format(self):
20+
tests = (
21+
"1400/11/27",
22+
jdatetime.date(1400, 11, 27),
23+
jdatetime.datetime(1400, 11, 27),
24+
)
25+
for value in tests:
26+
with self.subTest(value=value):
27+
f = jDateField(
28+
input_formats=[
29+
"%Y/%m/%d",
30+
]
31+
)
32+
self.assertEqual(f.clean(value), jdatetime.date(1400, 11, 27))
33+
34+
def test_field_with_multiple_input_formats(self):
35+
tests = (
36+
"1400/11/27",
37+
"1400, 11, 27",
38+
jdatetime.date(1400, 11, 27),
39+
jdatetime.datetime(1400, 11, 27),
40+
)
41+
for value in tests:
42+
with self.subTest(value=value):
43+
f = jDateField(input_formats=["%Y, %m, %d", "%Y/%m/%d"])
44+
self.assertEqual(f.clean(value), jdatetime.date(1400, 11, 27))
45+
1946

2047
class JDateTimeFieldTest(TestCase):
2148
def test_field(self):
@@ -29,3 +56,30 @@ def test_field(self):
2956
self.assertEqual(
3057
f.clean(value), jdatetime.datetime(1400, 11, 27, 12, 13, 20)
3158
)
59+
60+
def test_field_with_one_input_formats(self):
61+
tests = (
62+
"1400/11/27 12:13",
63+
jdatetime.datetime(1400, 11, 27, 12, 13, 20),
64+
)
65+
for value in tests:
66+
with self.subTest(value=value):
67+
f = jDateTimeField(
68+
input_formats=[
69+
"%Y/%m/%d %H:%M",
70+
]
71+
)
72+
self.assertEqual(
73+
f.clean(value), jdatetime.datetime(1400, 11, 27, 12, 13, 20)
74+
)
75+
76+
def test_field_with_multiple_input_formats(self):
77+
tests = (
78+
"1400/11/27 12-13",
79+
"1400, 11, 27 12:13",
80+
jdatetime.datetime(1400, 11, 27),
81+
)
82+
for value in tests:
83+
with self.subTest(value=value):
84+
f = jDateTimeField(input_formats=["%Y, %m, %d %H:%M", "%Y/%m/%d %H-%M"])
85+
self.assertEqual(f.clean(value), jdatetime.date(1400, 11, 27))

0 commit comments

Comments
 (0)