aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorElena ``of Valhalla'' Grandi <valhalla@trueelena.org>2021-02-10 09:32:37 +0100
committerElena ``of Valhalla'' Grandi <valhalla@trueelena.org>2021-02-10 09:32:37 +0100
commit836b4106aeef9acd30a4fd4dbca00c5d90e9aef7 (patch)
treeeed85d2ef57efee4a6332755c8bdbfe72a7dc18e
parent8579962d017c4ec46d731b852353d38e4fb031df (diff)
Add the option to autoincrement integer values
-rw-r--r--CHANGELOG.rst1
-rw-r--r--docs/source/user/settings.rst19
-rw-r--r--lesana/types.py23
-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.yaml4
-rw-r--r--tests/test_collection.py9
-rw-r--r--tests/test_types.py29
8 files changed, 84 insertions, 3 deletions
diff --git a/CHANGELOG.rst b/CHANGELOG.rst
index dd24d2e..7485d5b 100644
--- a/CHANGELOG.rst
+++ b/CHANGELOG.rst
@@ -12,6 +12,7 @@ Unreleased
* Added support to sort the list of all entries.
* Add the option to autofill date and datetime fields at creation and
update time. (#1)
+* Add the option to autoincrement integer values.
0.7.0
=====
diff --git a/docs/source/user/settings.rst b/docs/source/user/settings.rst
index fead752..c781f11 100644
--- a/docs/source/user/settings.rst
+++ b/docs/source/user/settings.rst
@@ -67,6 +67,25 @@ Some field types may add other custom properties.
you can use the ``yaml`` generic type, or write your own derivative
with an additional type).
+``integer`` properties
+----------------------
+
+``auto``:
+ automatic manipulation of the field contents.
+
+ The value of ``increment`` will autoincrement the value at every
+ update.
+
+ 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.
+
+``increment``:
+ the amount by which an ``auto: increment`` field is incremented
+ (negative values are of course allowed).
+
``date`` and ``datetime`` properties
------------------------------------
diff --git a/lesana/types.py b/lesana/types.py
index 7f182ea..da67781 100644
--- a/lesana/types.py
+++ b/lesana/types.py
@@ -133,6 +133,29 @@ class LesanaInt(LesanaType):
"""
return xapian.sortable_serialise(value)
+ def update(self, value):
+ """
+ Return an updated value.
+
+ If the field settings ``auto`` is ``increment`` return the value
+ incremented by the value of the field setting ``increment``
+ (default 1).
+
+ """
+ if self.field.get('auto', False) == 'increment':
+ increment = self.field.get('increment', 1)
+ if int(increment) == increment:
+ return value + increment
+ else:
+ logging.warning(
+ "Invalid configuration value for increment in field %s: "
+ + "%s",
+ self.field['name'],
+ increment,
+ )
+
+ return value
+
class LesanaFloat(LesanaType):
"""
diff --git a/tests/data/complex/items/5084bc6e94f24dc6976629282ef30419.yaml b/tests/data/complex/items/5084bc6e94f24dc6976629282ef30419.yaml
index bb97781..3132884 100644
--- a/tests/data/complex/items/5084bc6e94f24dc6976629282ef30419.yaml
+++ b/tests/data/complex/items/5084bc6e94f24dc6976629282ef30419.yaml
@@ -16,4 +16,5 @@ order: delta
created:
updated:
epoch:
+version: 0
# and a comment at the end
diff --git a/tests/data/complex/items/8e9fa1ed3c1b4a30a6be7a98eda0cfa7.yaml b/tests/data/complex/items/8e9fa1ed3c1b4a30a6be7a98eda0cfa7.yaml
index 44d8b89..690b680 100644
--- a/tests/data/complex/items/8e9fa1ed3c1b4a30a6be7a98eda0cfa7.yaml
+++ b/tests/data/complex/items/8e9fa1ed3c1b4a30a6be7a98eda0cfa7.yaml
@@ -14,3 +14,4 @@ order:
created:
updated:
epoch:
+version: 2
diff --git a/tests/data/complex/settings.yaml b/tests/data/complex/settings.yaml
index bf64934..7aaf47b 100644
--- a/tests/data/complex/settings.yaml
+++ b/tests/data/complex/settings.yaml
@@ -48,3 +48,7 @@ fields:
- name: epoch
type: datetime
auto: false
+ - name: version
+ type: integer
+ auto: increment
+ increment: 2
diff --git a/tests/test_collection.py b/tests/test_collection.py
index 527acf9..84c535d 100644
--- a/tests/test_collection.py
+++ b/tests/test_collection.py
@@ -297,7 +297,7 @@ class testComplexCollection(unittest.TestCase):
self.collection.settings['name'],
"Fully featured lesana collection",
)
- self.assertEqual(len(self.collection.settings['fields']), 13)
+ self.assertEqual(len(self.collection.settings['fields']), 14)
self.assertIsNotNone(self.collection.stemmer)
self.assertEqual(len(self.collection.indexed_fields), 8)
@@ -424,21 +424,24 @@ class testComplexCollection(unittest.TestCase):
eid = '5084bc6e94f24dc6976629282ef30419'
entry = self.collection.entry_from_eid(eid)
# we keep the old data, and check that the updated field is
- # empty
+ # empty and the version field is 0
old_data = entry.data.copy()
self.assertEqual(entry.data['updated'], None)
+ self.assertEqual(entry.data['version'], 0)
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).
+ # to avoid breaking tests too often with race conditions) and
+ # version has grown to 2.
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)
+ self.assertEqual(entry.data['version'], 2)
class testCollectionWithErrors(unittest.TestCase):
diff --git a/tests/test_types.py b/tests/test_types.py
index 260167c..1efcde1 100644
--- a/tests/test_types.py
+++ b/tests/test_types.py
@@ -82,6 +82,35 @@ class testTypes(unittest.TestCase):
v = checker.update(10)
self.assertEqual(v, 10)
+ def test_datetime_auto_increment(self):
+ field_def = self._get_field_def('integer')
+ field_def['auto'] = 'increment'
+ checker = types.LesanaInt(field_def, {})
+
+ v = checker.empty()
+ self.assertEqual(v, 0)
+
+ v = checker.update(0)
+ self.assertEqual(v, 1)
+
+ field_def['increment'] = -1
+ checker = types.LesanaInt(field_def, {})
+ v = checker.update(0)
+ self.assertEqual(v, -1)
+
+ field_def['increment'] = 0.5
+ checker = types.LesanaInt(field_def, {})
+ with self.assertLogs() as cm:
+ v = checker.update(0)
+ self.assertIn('WARNING', cm.output[0])
+ self.assertIn('Invalid configuration value', cm.output[0])
+ self.assertEqual(v, 0)
+
+ field_def['auto'] = 'false'
+ checker = types.LesanaInt(field_def, {})
+ v = checker.update(0)
+ self.assertEqual(v, 0)
+
def test_float(self):
checker = types.LesanaFloat(self._get_field_def('float'), {})