diff options
author | Elena ``of Valhalla'' Grandi <valhalla@trueelena.org> | 2021-02-03 11:24:46 +0100 |
---|---|---|
committer | Elena ``of Valhalla'' Grandi <valhalla@trueelena.org> | 2021-02-03 11:24:46 +0100 |
commit | 23de3297bbd74980a2f6da326acc5da63ddef2fe (patch) | |
tree | fc670e596e96a2147ee0f27a838072ba98645e25 | |
parent | de1c03d81615e6fd50540008fe6ca46feb012c30 (diff) |
Add a default_sort option to collections
-rw-r--r-- | docs/source/user/settings.rst | 4 | ||||
-rw-r--r-- | lesana/collection.py | 3 | ||||
-rw-r--r-- | tests/data/complex/items/0b33e2b72add4ccab93a8cb7e2014b10.yaml | 1 | ||||
-rw-r--r-- | tests/data/complex/items/5084bc6e94f24dc6976629282ef30419.yaml | 1 | ||||
-rw-r--r-- | tests/data/complex/items/5be0a92b6ad745fc9ffced106c94d221.yaml | 1 | ||||
-rw-r--r-- | tests/data/complex/items/8e9fa1ed3c1b4a30a6be7a98eda0cfa7.yaml | 1 | ||||
-rw-r--r-- | tests/data/complex/items/b4b1feb620aa46f5b6784fbc608e4cd8.yaml | 1 | ||||
-rw-r--r-- | tests/data/complex/settings.yaml | 6 | ||||
-rw-r--r-- | tests/test_collection.py | 16 |
9 files changed, 32 insertions, 2 deletions
diff --git a/docs/source/user/settings.rst b/docs/source/user/settings.rst index 0d2eec9..7c84e39 100644 --- a/docs/source/user/settings.rst +++ b/docs/source/user/settings.rst @@ -16,6 +16,10 @@ It is a yaml file with a dict of properties and their values. a jinja2 template used to show an entry in the interface; beside the entry fields two useful variables are ``eid`` for the full entry ID and ``short_id`` for the short version. +``default_sort``: + a list of field names (possibly prefixed by + or -) that are used by + default to sort results of searches in the collection. + The fields must be marked as sortable in their definition, see below. ``fields``: The list of fields used by the collection, as described below. diff --git a/lesana/collection.py b/lesana/collection.py index efd329e..9c3ea07 100644 --- a/lesana/collection.py +++ b/lesana/collection.py @@ -335,6 +335,9 @@ class Collection(object): self._enquire = xapian.Enquire(cache) self._enquire.set_query(query) + if not sort_by and self.settings.get('default_sort', False): + sort_by = self.settings['default_sort'] + if sort_by: keymaker = xapian.MultiValueKeyMaker() for k in sort_by: diff --git a/tests/data/complex/items/0b33e2b72add4ccab93a8cb7e2014b10.yaml b/tests/data/complex/items/0b33e2b72add4ccab93a8cb7e2014b10.yaml index c9aece7..6fd18e1 100644 --- a/tests/data/complex/items/0b33e2b72add4ccab93a8cb7e2014b10.yaml +++ b/tests/data/complex/items/0b33e2b72add4ccab93a8cb7e2014b10.yaml @@ -8,3 +8,4 @@ keywords: [] exists: with_default: default value amount: 2 +order: charlie diff --git a/tests/data/complex/items/5084bc6e94f24dc6976629282ef30419.yaml b/tests/data/complex/items/5084bc6e94f24dc6976629282ef30419.yaml index 874833e..6abdd6d 100644 --- a/tests/data/complex/items/5084bc6e94f24dc6976629282ef30419.yaml +++ b/tests/data/complex/items/5084bc6e94f24dc6976629282ef30419.yaml @@ -12,4 +12,5 @@ keywords: [] exists: true with_default: default value amount: 1 +order: delta # and a comment at the end diff --git a/tests/data/complex/items/5be0a92b6ad745fc9ffced106c94d221.yaml b/tests/data/complex/items/5be0a92b6ad745fc9ffced106c94d221.yaml index c57b5f0..18ff2c9 100644 --- a/tests/data/complex/items/5be0a92b6ad745fc9ffced106c94d221.yaml +++ b/tests/data/complex/items/5be0a92b6ad745fc9ffced106c94d221.yaml @@ -8,3 +8,4 @@ keywords: [] exists: with_default: default value amount: 10 +order: alpha diff --git a/tests/data/complex/items/8e9fa1ed3c1b4a30a6be7a98eda0cfa7.yaml b/tests/data/complex/items/8e9fa1ed3c1b4a30a6be7a98eda0cfa7.yaml index e23a563..da82d8d 100644 --- a/tests/data/complex/items/8e9fa1ed3c1b4a30a6be7a98eda0cfa7.yaml +++ b/tests/data/complex/items/8e9fa1ed3c1b4a30a6be7a98eda0cfa7.yaml @@ -10,3 +10,4 @@ keywords: [] exists: with_default: 'default value' amount: 0 +order: diff --git a/tests/data/complex/items/b4b1feb620aa46f5b6784fbc608e4cd8.yaml b/tests/data/complex/items/b4b1feb620aa46f5b6784fbc608e4cd8.yaml index 50c57e2..6661167 100644 --- a/tests/data/complex/items/b4b1feb620aa46f5b6784fbc608e4cd8.yaml +++ b/tests/data/complex/items/b4b1feb620aa46f5b6784fbc608e4cd8.yaml @@ -8,3 +8,4 @@ keywords: [] exists: with_default: default value amount: 20 +order: zucchini diff --git a/tests/data/complex/settings.yaml b/tests/data/complex/settings.yaml index e6781b5..1fd36b5 100644 --- a/tests/data/complex/settings.yaml +++ b/tests/data/complex/settings.yaml @@ -1,6 +1,8 @@ name: "Fully featured lesana collection" lang: 'english' entry_label: '{{ eid }}: {{ name }} ({{ tags }})' +default_sort: + - order fields: - name: name type: string @@ -33,3 +35,7 @@ fields: type: integer index: field sortable: true + - name: order + type: string + index: field + sortable: true diff --git a/tests/test_collection.py b/tests/test_collection.py index 6a6e5e3..4c95c2b 100644 --- a/tests/test_collection.py +++ b/tests/test_collection.py @@ -289,9 +289,9 @@ class testComplexCollection(unittest.TestCase): self.collection.settings['name'], "Fully featured lesana collection", ) - self.assertEqual(len(self.collection.settings['fields']), 9) + self.assertEqual(len(self.collection.settings['fields']), 10) self.assertIsNotNone(self.collection.stemmer) - self.assertEqual(len(self.collection.indexed_fields), 7) + self.assertEqual(len(self.collection.indexed_fields), 8) def test_index(self): indexed = self.collection.update_cache() @@ -379,6 +379,18 @@ class testComplexCollection(unittest.TestCase): self.assertEqual(matches[2].data['amount'], 10) self.assertEqual(matches[3].data['amount'], 2) + def test_default_sorted_search(self): + # search in ascending order + self.collection.start_search('Amount') + res = self.collection.get_search_results() + matches = list(res) + self.assertEqual(len(matches), 4) + print([m.data['order'] for m in matches]) + self.assertEqual(matches[0].data['order'], None) + self.assertEqual(matches[1].data['order'], 'alpha') + self.assertEqual(matches[2].data['order'], 'charlie') + self.assertEqual(matches[3].data['order'], 'zucchini') + class testCollectionWithErrors(unittest.TestCase): def setUp(self): |