From 76849e6374f2ebf2a1df2ac142a150f82bf2e472 Mon Sep 17 00:00:00 2001 From: Elena ``of Valhalla'' Grandi Date: Mon, 2 Jan 2017 18:59:29 +0100 Subject: Custom label for entries --- lesana/collection.py | 9 +++++++++ setup.py | 1 + tests/test_collection.py | 14 ++++++++++++++ 3 files changed, 24 insertions(+) diff --git a/lesana/collection.py b/lesana/collection.py index 84845f5..f850a47 100644 --- a/lesana/collection.py +++ b/lesana/collection.py @@ -4,6 +4,7 @@ import uuid import ruamel.yaml import xapian +import jinja2 from pkg_resources import resource_string @@ -27,6 +28,14 @@ class Entry(object): self.uid = uuid.uuid4().hex self.fname = self.uid + '.yaml' + def __str__(self): + label = self.collection.settings.get('entry_label', None) + if label: + t = jinja2.Template(label) + return t.render(uid=self.uid, **self.data) + else: + return self.uid + def empty_data(self): data = '' for field in self.collection.settings['fields']: diff --git a/setup.py b/setup.py index d496e64..ce8e287 100644 --- a/setup.py +++ b/setup.py @@ -8,6 +8,7 @@ setup( install_require = [ 'xapian >= 1.4', 'ruamel.yaml', + 'jinja2', ], package_data={ diff --git a/tests/test_collection.py b/tests/test_collection.py index e732f9c..9e3df7c 100644 --- a/tests/test_collection.py +++ b/tests/test_collection.py @@ -109,6 +109,20 @@ class testEntries(unittest.TestCase): self.assertIsInstance(written['name'], str) os.remove(entry_fname) + def test_entry_representation(self): + uid = '11189ee47ddf4796b718a483b379f976' + entry = self.collection.entry_from_uid(uid) + self.assertEqual( + str(entry), + uid + ) + label = '{{ uid }}: {{ name }}' + self.collection.settings['entry_label'] = label + self.assertEqual( + str(entry), + '{uid}: {name}'.format(uid=uid, name='Another item') + ) + class testComplexCollection(unittest.TestCase): @classmethod -- cgit v1.2.3