summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorElena ``of Valhalla'' Grandi <valhalla@trueelena.org>2017-01-02 18:59:29 +0100
committerElena ``of Valhalla'' Grandi <valhalla@trueelena.org>2017-01-02 18:59:29 +0100
commit76849e6374f2ebf2a1df2ac142a150f82bf2e472 (patch)
tree56a73b48146d1bdfd07a754331855b1c0b885479
parent97a6ff92674a74a92050ae29e3606cb219b3ed93 (diff)
Custom label for entries
-rw-r--r--lesana/collection.py9
-rw-r--r--setup.py1
-rw-r--r--tests/test_collection.py14
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']:
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