From 183b931e52633e453f5783b9232e136838a4d992 Mon Sep 17 00:00:00 2001 From: femalves Date: Wed, 10 Apr 2024 16:57:30 -0400 Subject: [PATCH] bug fix timestamp time asc --- biblib/tests/unit_tests/test_webservices.py | 8 +++++++- biblib/views/library_view.py | 12 +++++++----- 2 files changed, 14 insertions(+), 6 deletions(-) diff --git a/biblib/tests/unit_tests/test_webservices.py b/biblib/tests/unit_tests/test_webservices.py index 2a2fff3..f66303c 100644 --- a/biblib/tests/unit_tests/test_webservices.py +++ b/biblib/tests/unit_tests/test_webservices.py @@ -1425,6 +1425,9 @@ def test_timestamp_sort_returns_correct_order(self): query_string={'sort': 'time asc'} ) + + response_asc = response.json['documents'] + self.assertEqual(response.status_code, 200, response) self.assertEqual(full_bibcodes, response.json['documents']) @@ -1437,11 +1440,14 @@ def test_timestamp_sort_returns_correct_order(self): headers=stub_user.headers, query_string={"sort": 'time desc'} ) + + response_desc = response.json['documents'] self.assertEqual(response.status_code, 200, response) self.assertEqual(list(reversed(full_bibcodes)), response.json['documents']) - + self.assertEqual(response_asc, response_desc[::-1]) + def test_add_query_to_library(self): """ Test the /query/<> end point with POST to add a document diff --git a/biblib/views/library_view.py b/biblib/views/library_view.py index 5286007..9f4fbb5 100644 --- a/biblib/views/library_view.py +++ b/biblib/views/library_view.py @@ -210,13 +210,13 @@ def load_parameters(self, request): raw_library = False sort = request.args.get('sort', 'date desc') - #timestamp sorting is handled in biblib so we need to change the sort to something SOLR understands. + # timestamp sorting is handled in biblib so we need to change the sort to something SOLR understands. if sort in ['time asc', 'time desc']: current_app.logger.debug("sort order is set to {}".format(sort)) if sort == 'time desc': - add_sort = True + add_sort = 'desc' else: - add_sort = False + add_sort = 'asc' sort = 'date desc' else: add_sort = None @@ -275,6 +275,7 @@ def process_solr(self, library, start, rows, sort, fl, session, add_sort): .format(error)) solr = {'error': 'Could not parse solr data'} + reverse = True if add_sort == 'desc' else False # Now check if we can update the library database based on the # returned canonical bibcodes if solr.get('response'): @@ -285,7 +286,8 @@ def process_solr(self, library, start, rows, sort, fl, session, add_sort): session=session ) if add_sort: - solr = self.timestamp_sort(solr, library.id, reverse=add_sort) + + solr = self.timestamp_sort(solr, library.id, reverse=reverse) documents = [doc['bibcode'] for doc in solr['response']['docs']] else: @@ -299,7 +301,7 @@ def process_solr(self, library, start, rows, sort, fl, session, add_sort): # Find the specified library (we have to do this to have full access to the library) temp_library = session.query(Library).filter_by(id=library.id).one() sortable_list = [(bibcode, library.bibcode[bibcode]["timestamp"]) for bibcode in temp_library.get_bibcodes()] - sortable_list.sort(key = lambda stamped: stamped[1], reverse=add_sort) + sortable_list.sort(key = lambda stamped: stamped[1], reverse=reverse) documents = [doc[0] for doc in sortable_list] else: documents = library.get_bibcodes()