Skip to content

Commit

Permalink
[improve] Respect jDateField's input_formats kwarg when parsing date (#…
Browse files Browse the repository at this point in the history
…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]>
  • Loading branch information
saharnaz-rashidi and Saharnaz Rashidi authored Aug 20, 2024
1 parent fda83a6 commit 0725acd
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 2 deletions.
19 changes: 17 additions & 2 deletions django_jalali/forms/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import re
from typing import Union

import jdatetime
from django import forms
Expand All @@ -19,10 +20,10 @@ def __init__(self, input_formats=None, *args, **kwargs):
super().__init__(*args, **kwargs)
self.input_formats = input_formats

def to_python(self, value):
def to_python(self, value) -> Union[jdatetime.date, None]:
"""
Validates that the input can be converted to a date. Returns a Python
datetime.date object.
jdatetime.date object.
"""
if value in validators.EMPTY_VALUES:
return None
Expand All @@ -31,6 +32,13 @@ def to_python(self, value):
if isinstance(value, jdatetime.date):
return value

if self.input_formats:
for input_format in self.input_formats:
try:
return jdatetime.datetime.strptime(value, input_format).date()
except ValueError:
pass

groups = re.search(
r"(?P<year>[\d]{1,4})-(?P<month>[\d]{1,2})-(?P<day>[\d]{1,2})",
value,
Expand Down Expand Up @@ -87,6 +95,13 @@ def to_python(self, value):
return None
value = "%s %s" % tuple(value)

if self.input_formats:
for input_format in self.input_formats:
try:
return jdatetime.datetime.strptime(value, input_format).date()
except ValueError:
pass

groups = re.search(
r"(?P<year>[\d]{1,4})-(?P<month>[\d]{1,2})-(?P<day>[\d]{1,2}) "
r"(?P<hour>[\d]{1,2}):(?P<minute>[\d]{1,2})"
Expand Down
54 changes: 54 additions & 0 deletions tests/test_forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,33 @@ def test_field(self):
f = jDateField()
self.assertEqual(f.clean(value), jdatetime.date(1400, 11, 27))

def test_field_with_one_input_format(self):
tests = (
"1400/11/27",
jdatetime.date(1400, 11, 27),
jdatetime.datetime(1400, 11, 27),
)
for value in tests:
with self.subTest(value=value):
f = jDateField(
input_formats=[
"%Y/%m/%d",
]
)
self.assertEqual(f.clean(value), jdatetime.date(1400, 11, 27))

def test_field_with_multiple_input_formats(self):
tests = (
"1400/11/27",
"1400, 11, 27",
jdatetime.date(1400, 11, 27),
jdatetime.datetime(1400, 11, 27),
)
for value in tests:
with self.subTest(value=value):
f = jDateField(input_formats=["%Y, %m, %d", "%Y/%m/%d"])
self.assertEqual(f.clean(value), jdatetime.date(1400, 11, 27))


class JDateTimeFieldTest(TestCase):
def test_field(self):
Expand All @@ -29,3 +56,30 @@ def test_field(self):
self.assertEqual(
f.clean(value), jdatetime.datetime(1400, 11, 27, 12, 13, 20)
)

def test_field_with_one_input_formats(self):
tests = (
"1400/11/27 12:13",
jdatetime.datetime(1400, 11, 27, 12, 13, 20),
)
for value in tests:
with self.subTest(value=value):
f = jDateTimeField(
input_formats=[
"%Y/%m/%d %H:%M",
]
)
self.assertEqual(
f.clean(value), jdatetime.datetime(1400, 11, 27, 12, 13, 20)
)

def test_field_with_multiple_input_formats(self):
tests = (
"1400/11/27 12-13",
"1400, 11, 27 12:13",
jdatetime.datetime(1400, 11, 27),
)
for value in tests:
with self.subTest(value=value):
f = jDateTimeField(input_formats=["%Y, %m, %d %H:%M", "%Y/%m/%d %H-%M"])
self.assertEqual(f.clean(value), jdatetime.date(1400, 11, 27))

0 comments on commit 0725acd

Please sign in to comment.