diff options
-rw-r--r-- | lesana/collection.py | 9 | ||||
-rw-r--r-- | setup.py | 1 | ||||
-rw-r--r-- | tests/test_collection.py | 14 |
3 files changed, 24 insertions, 0 deletions
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']: @@ -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 |