From b3936147562d8a17e023b0ab4d8dad8c0571847c Mon Sep 17 00:00:00 2001 From: femalves Date: Wed, 17 Apr 2024 16:21:18 -0400 Subject: [PATCH] fixing library detached bug --- biblib/views/library_view.py | 19 ++++++++----------- 1 file changed, 8 insertions(+), 11 deletions(-) diff --git a/biblib/views/library_view.py b/biblib/views/library_view.py index 9f4fbb5..b2401e3 100644 --- a/biblib/views/library_view.py +++ b/biblib/views/library_view.py @@ -287,7 +287,7 @@ def process_solr(self, library, start, rows, sort, fl, session, add_sort): ) if add_sort: - solr = self.timestamp_sort(solr, library.id, reverse=reverse) + solr = self.timestamp_sort(solr, library, reverse=reverse) documents = [doc['bibcode'] for doc in solr['response']['docs']] else: @@ -447,7 +447,7 @@ def get_library_data(self, data): @staticmethod - def timestamp_sort(solr, library_id, reverse=False): + def timestamp_sort(solr, library, reverse=False): """ Take a solr response and sort it based on the timestamps contained in the library :input: response: response from SOLR bigquery @@ -458,15 +458,12 @@ def timestamp_sort(solr, library_id, reverse=False): """ if "error" not in solr['response'].keys(): try: - with current_app.session_scope() as session: - # Find the specified library - library = session.query(Library).filter_by(id=library_id).one() - #First we generate a list of timestamps for the valid bibcodes - timestamp = [library.bibcode[doc['bibcode']]['timestamp'] for doc in solr['response']['docs']] - #Then we sort the SOLR response by the generated timestamp list - solr['response']['docs'] = [\ - doc for (doc, timestamp) in sorted(zip(solr['response']['docs'], timestamp), reverse=reverse, key = lambda stamped: stamped[1])\ - ] + #First we generate a list of timestamps for the valid bibcodes + timestamp = [library.bibcode[doc['bibcode']]['timestamp'] for doc in solr['response']['docs']] + #Then we sort the SOLR response by the generated timestamp list + solr['response']['docs'] = [\ + doc for (doc, timestamp) in sorted(zip(solr['response']['docs'], timestamp), reverse=reverse, key = lambda stamped: stamped[1])\ + ] except Exception as e: current_app.logger.warn("Failed to retrieve timestamps for {} with exception: {}. Returning default sorting.".format(library.id, e)) else: