Skip to content

Commit

Permalink
Improved transmogrifier.str_to_date with min and max
Browse files Browse the repository at this point in the history
  • Loading branch information
sgeulette committed Aug 25, 2023
1 parent 9f88c6c commit 1ce4359
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 3 deletions.
4 changes: 3 additions & 1 deletion CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@ Changelog
0.75 (unreleased)
-----------------

- Fixed setup.load_type_from_package when loading a Dexterity FTI because it fails to purge old values.
- Fixed `setup.load_type_from_package` when loading a Dexterity FTI because it fails to purge old values.
Purging is disabled for Dexterity FTI, we just manage removing actions manually so it is reloaded in correct order.
[gbastien]
- Improved `transmogrifier.str_to_date` with min and max
[sgeulette]

0.74 (2023-08-24)
-----------------
Expand Down
9 changes: 8 additions & 1 deletion src/imio/helpers/tests/test_transmogrifier.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,11 +140,18 @@ def test_str_to_date(self):
# str_to_date(item, key, log_method, fmt='%Y/%m/%d', can_be_empty=True, as_date=True, **log_params):
self.assertIsNone(str_to_date(dic, 'bad_key', logger))
self.assertRaises(TypeError, str_to_date, dic, 'bad_key', logger, can_be_empty=False)
self.assertIsNone(str_to_date(dic, 1, logger, min_val=datetime.date(2023, 4, 1)))
self.assertEquals(dic['errors'], 1)
self.assertIsNone(str_to_date(dic, 1, logger, max_val=datetime.date(2022, 4, 1)))
self.assertEquals(dic['errors'], 2)
self.assertIsNone(str_to_date(dic, 1, logger, min_val=datetime.date(2023, 4, 1),
max_val=datetime.date(2022, 4, 1)))
self.assertEquals(dic['errors'], 3)
ret = str_to_date(dic, 1, logger)
self.assertIsInstance(ret, datetime.date)
self.assertEqual(str(ret), '2023-03-03')
ret = str_to_date(dic, 2, logger, fmt='%Y/%m/%d %H:%M', as_date=False)
self.assertIsInstance(ret, datetime.datetime)
self.assertEqual(str(ret), '2023-03-03 09:29:00')
ret = str_to_date(dic, 2, logger, fmt='%Y/%m/%d %M:%H', as_date=False)
self.assertEquals(dic['errors'], 1)
self.assertEquals(dic['errors'], 4)
9 changes: 8 additions & 1 deletion src/imio/helpers/transmogrifier.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,8 @@ def split_text(text, length):
return part1, part2


def str_to_date(item, key, log_method, fmt='%Y/%m/%d', can_be_empty=True, as_date=True, **log_params):
def str_to_date(item, key, log_method, fmt='%Y/%m/%d', can_be_empty=True, as_date=True, min_val=None, max_val=None,
**log_params):
"""Changed to date or datetime the text value of item[key]
:param item: yielded item (usually dict)
Expand All @@ -188,6 +189,8 @@ def str_to_date(item, key, log_method, fmt='%Y/%m/%d', can_be_empty=True, as_dat
:param fmt: formatting date string
:param can_be_empty: value can be empty or None
:param as_date: return a date, otherwise a datetime
:param min_val: minimal date
:param max_val: maximum date
:param log_params: log method keyword parameters
:return: the date or datetime value
"""
Expand All @@ -198,6 +201,10 @@ def str_to_date(item, key, log_method, fmt='%Y/%m/%d', can_be_empty=True, as_dat
dt = datetime.strptime(val, fmt)
if as_date:
dt = dt.date()
if min_val and dt < min_val:
raise(ValueError(u"Given date '{}' < minimal value '{}' => set to None".format(val, min_val)))
if max_val and dt > max_val:
raise (ValueError(u"Given date '{}' > maximum value '{}' => set to None".format(val, max_val)))
except ValueError as ex:
log_method(item, u"not a valid date '{}' in key '{}': {}".format(val, key, ex), **log_params)
return None
Expand Down

0 comments on commit 1ce4359

Please sign in to comment.