summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorElena ``of Valhalla'' Grandi <valhalla@trueelena.org>2021-02-09 16:56:07 +0100
committerElena ``of Valhalla'' Grandi <valhalla@trueelena.org>2021-02-09 16:56:07 +0100
commite6c4eefc1d55d6643c4455072df36fd35bfe5c3d (patch)
treedcd252d6dba06d6679a2f17e543f00d751b673c5
parent2c42531afff5e4fcd6d28ab8230011b7621bae84 (diff)
Update fields before editing an entry, including autofill on update of date(time) fields. refs: #1
-rw-r--r--docs/source/user/settings.rst17
-rw-r--r--lesana/collection.py13
-rw-r--r--lesana/command.py4
-rw-r--r--tests/data/complex/items/5084bc6e94f24dc6976629282ef30419.yaml1
-rw-r--r--tests/data/complex/items/8e9fa1ed3c1b4a30a6be7a98eda0cfa7.yaml1
-rw-r--r--tests/data/complex/settings.yaml3
-rw-r--r--tests/test_collection.py30
7 files changed, 65 insertions, 4 deletions
diff --git a/docs/source/user/settings.rst b/docs/source/user/settings.rst
index 2ef9d02..fead752 100644
--- a/docs/source/user/settings.rst
+++ b/docs/source/user/settings.rst
@@ -73,7 +73,18 @@ Some field types may add other custom 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``).
+ The following values are supported.
+
+ ``creation``
+ autofill the field at creation time with the current UTC time
+ (``datetime``) or local zone day (``date``).
+ ``update``
+ autofill the field when it is updated with the current UTC time
+ (``datetime``) or local zone day (``date``).
+
+ The reference client will run this update before editing an entry,
+ allowing further changes from the user; a command line user can
+ then decide to abort this change through the usual git commands.
+
+ Other clients may decide to use a different workflow.
diff --git a/lesana/collection.py b/lesana/collection.py
index 50e8299..cb43d1d 100644
--- a/lesana/collection.py
+++ b/lesana/collection.py
@@ -118,6 +118,19 @@ class Entry(object):
except jinja2.exceptions.TemplateSyntaxError as e:
raise TemplatingError('Template Syntax Error: ' + str(e))
+ def update(self):
+ """
+ Update all fields of this entry, as required by the field settings.
+
+ This is called by the reference client before an edit, so that
+ the user can make further changes.
+
+ Note that the stored file is not changed: if you need it you
+ need to save the entry yourself.
+ """
+ for name, field in self.collection.fields.items():
+ self.data[name] = field.update(self.data.get(name, None))
+
class Collection(object):
"""
diff --git a/lesana/command.py b/lesana/command.py
index bf86b95..b25fb3d 100644
--- a/lesana/command.py
+++ b/lesana/command.py
@@ -159,6 +159,10 @@ class Edit(Command):
self.args.eid
)
entry = entries[0]
+ # update the entry before editing it
+ entry.update()
+ collection.save_entries([entry])
+ # and then edit the updated file
filepath = os.path.join(collection.itemdir, entry.fname)
if edit_file_in_external_editor(filepath):
collection.update_cache([filepath])
diff --git a/tests/data/complex/items/5084bc6e94f24dc6976629282ef30419.yaml b/tests/data/complex/items/5084bc6e94f24dc6976629282ef30419.yaml
index d4cdaba..bb97781 100644
--- a/tests/data/complex/items/5084bc6e94f24dc6976629282ef30419.yaml
+++ b/tests/data/complex/items/5084bc6e94f24dc6976629282ef30419.yaml
@@ -14,5 +14,6 @@ with_default: default value
amount: 1
order: delta
created:
+updated:
epoch:
# and a comment at the end
diff --git a/tests/data/complex/items/8e9fa1ed3c1b4a30a6be7a98eda0cfa7.yaml b/tests/data/complex/items/8e9fa1ed3c1b4a30a6be7a98eda0cfa7.yaml
index ab9dc70..44d8b89 100644
--- a/tests/data/complex/items/8e9fa1ed3c1b4a30a6be7a98eda0cfa7.yaml
+++ b/tests/data/complex/items/8e9fa1ed3c1b4a30a6be7a98eda0cfa7.yaml
@@ -12,4 +12,5 @@ with_default: 'default value'
amount: 0
order:
created:
+updated:
epoch:
diff --git a/tests/data/complex/settings.yaml b/tests/data/complex/settings.yaml
index 1c48b52..bf64934 100644
--- a/tests/data/complex/settings.yaml
+++ b/tests/data/complex/settings.yaml
@@ -42,6 +42,9 @@ fields:
- name: created
type: datetime
auto: creation
+ - name: updated
+ type: datetime
+ auto: update
- name: epoch
type: datetime
auto: false
diff --git a/tests/test_collection.py b/tests/test_collection.py
index 4fa292e..527acf9 100644
--- a/tests/test_collection.py
+++ b/tests/test_collection.py
@@ -1,3 +1,4 @@
+import datetime
import logging
import os.path
import shutil
@@ -117,6 +118,13 @@ class testEntries(unittest.TestCase):
self.assertIn("name: ''", entry.yaml_data)
self.assertIn('quantity: 0', entry.yaml_data)
+ def test_update_entry(self):
+ eid = '11189ee47ddf4796b718a483b379f976'
+ entry = self.collection.entry_from_eid(eid)
+ old_data = entry.data.copy()
+ entry.update()
+ self.assertEqual(old_data, entry.data)
+
class testEmptyCollection(unittest.TestCase):
def setUp(self):
@@ -289,7 +297,7 @@ class testComplexCollection(unittest.TestCase):
self.collection.settings['name'],
"Fully featured lesana collection",
)
- self.assertEqual(len(self.collection.settings['fields']), 12)
+ self.assertEqual(len(self.collection.settings['fields']), 13)
self.assertIsNotNone(self.collection.stemmer)
self.assertEqual(len(self.collection.indexed_fields), 8)
@@ -412,6 +420,26 @@ class testComplexCollection(unittest.TestCase):
self.assertEqual(matches[7].data['order'], 'delta')
self.assertEqual(matches[8].data['order'], 'zucchini')
+ def test_update_entry(self):
+ eid = '5084bc6e94f24dc6976629282ef30419'
+ entry = self.collection.entry_from_eid(eid)
+ # we keep the old data, and check that the updated field is
+ # empty
+ old_data = entry.data.copy()
+ self.assertEqual(entry.data['updated'], None)
+
+ entry.update()
+
+ # after the update, fields that were not supposed to be updated
+ # are equal to what they were before, while updated has been
+ # changed to a datetime in this year (we don't check too deeply
+ # to avoid breaking tests too often with race conditions).
+ for field in ('created', 'epoch'):
+ self.assertEqual(old_data[field], entry.data[field])
+ now = datetime.datetime.now(datetime.timezone.utc)
+ self.assertIsInstance(entry.data['updated'], datetime.datetime)
+ self.assertEqual(entry.data['updated'].year, now.year)
+
class testCollectionWithErrors(unittest.TestCase):
def setUp(self):