Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

bug fix timestamp time asc #178

Merged
merged 1 commit into from
Apr 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion biblib/tests/unit_tests/test_webservices.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'])
Expand All @@ -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
Expand Down
12 changes: 7 additions & 5 deletions biblib/views/library_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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'):
Expand All @@ -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:
Expand All @@ -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()
Expand Down
Loading