diff options
author | Elena ``of Valhalla'' Grandi <valhalla@trueelena.org> | 2021-02-08 16:39:39 +0100 |
---|---|---|
committer | Elena ``of Valhalla'' Grandi <valhalla@trueelena.org> | 2021-02-08 16:39:39 +0100 |
commit | a4433548c49da3140f3cda42d300803c583e7e36 (patch) | |
tree | e05b82e688e7e58dcc62ec483320fad48856cfac | |
parent | 4b87d38ed0300ba207faf3890123d26fa4af2609 (diff) |
Add the option to autofill date and datetime fields at creation time.
-rw-r--r-- | CHANGELOG.rst | 1 | ||||
-rw-r--r-- | docs/source/user/settings.rst | 10 | ||||
-rw-r--r-- | lesana/types.py | 6 | ||||
-rw-r--r-- | tests/data/complex/settings.yaml | 2 | ||||
-rw-r--r-- | tests/test_collection.py | 7 | ||||
-rw-r--r-- | tests/test_types.py | 33 |
6 files changed, 58 insertions, 1 deletions
diff --git a/CHANGELOG.rst b/CHANGELOG.rst index cab6eb1..ea33fcb 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -10,6 +10,7 @@ Unreleased * New option default_sort for collection, to sort search results by default. * Added support to sort the list of all entries. +* Add the option to autofill date and datetime fields at creation time. 0.7.0 ===== diff --git a/docs/source/user/settings.rst b/docs/source/user/settings.rst index 7c84e39..2ef9d02 100644 --- a/docs/source/user/settings.rst +++ b/docs/source/user/settings.rst @@ -67,3 +67,13 @@ Some field types may add other custom properties. you can use the ``yaml`` generic type, or write your own derivative with an additional type). +``date`` and ``datetime`` properties +------------------------------------ + +``auto``: + automatic manipulation of the field contents. + + At the moment only the value ``creation`` is supported, to autofill + the field at creation time with the current UTC time (``datetime``) + or local zone day (``date``). + diff --git a/lesana/types.py b/lesana/types.py index 0b278d6..500f21e 100644 --- a/lesana/types.py +++ b/lesana/types.py @@ -212,6 +212,9 @@ class LesanaDatetime(LesanaType): ) def empty(self): + if self.field.get('auto', False) == 'creation': + return datetime.datetime.now(datetime.timezone.utc) + return None @@ -236,6 +239,9 @@ class LesanaDate(LesanaType): ) def empty(self): + if self.field.get('auto', False) == 'creation': + return datetime.date.today() + return None diff --git a/tests/data/complex/settings.yaml b/tests/data/complex/settings.yaml index 0bdcabb..1c48b52 100644 --- a/tests/data/complex/settings.yaml +++ b/tests/data/complex/settings.yaml @@ -41,5 +41,7 @@ fields: sortable: true - name: created type: datetime + auto: creation - name: epoch type: datetime + auto: false diff --git a/tests/test_collection.py b/tests/test_collection.py index 9f83655..4fa292e 100644 --- a/tests/test_collection.py +++ b/tests/test_collection.py @@ -319,7 +319,12 @@ class testComplexCollection(unittest.TestCase): self.assertIn('amount: 0', entry.yaml_data) self.assertIn("tags: []", entry.yaml_data) self.assertIn("exists:\n", entry.yaml_data) - self.assertIn("created:\n", entry.yaml_data) + # we just check that created starts with a date and ends with + # the utc timezone to keep the regex short and manageable + self.assertRegex( + entry.yaml_data, + r"created: [\d]{4,4}-[\d]{2,2}-[\d]{2,2} .*\+00\:00" + ) self.assertIn("epoch:\n", entry.yaml_data) def test_load_field_loaders(self): diff --git a/tests/test_types.py b/tests/test_types.py index 6f0c33e..9663288 100644 --- a/tests/test_types.py +++ b/tests/test_types.py @@ -176,6 +176,23 @@ class testTypes(unittest.TestCase): v = checker.load(None) self.assertEqual(v, None) + def test_datetime_auto(self): + field_def = self._get_field_def('datetime') + field_def['auto'] = 'creation' + checker = types.LesanaDatetime(field_def, {}) + + now = datetime.datetime.now() + v = checker.empty() + self.assertIsInstance(v, datetime.datetime) + self.assertEqual(v.tzinfo, datetime.timezone.utc) + self.assertEqual(v.year, now.year) + + field_def['auto'] = False + checker = types.LesanaDatetime(field_def, {}) + + v = checker.empty() + self.assertEqual(v, None) + def test_date(self): checker = types.LesanaDate(self._get_field_def('date'), {}) @@ -205,6 +222,22 @@ class testTypes(unittest.TestCase): v = checker.load(None) self.assertEqual(v, None) + def test_date_auto(self): + field_def = self._get_field_def('date') + field_def['auto'] = 'creation' + checker = types.LesanaDate(field_def, {}) + + today = datetime.date.today() + v = checker.empty() + self.assertIsInstance(v, datetime.date) + self.assertEqual(v, today) + + field_def['auto'] = False + checker = types.LesanaDate(field_def, {}) + + v = checker.empty() + self.assertEqual(v, None) + def test_boolean(self): checker = types.LesanaBoolean(self._get_field_def('boolean'), {}) |