Skip to content

Commit

Permalink
fixing library detached bug (#179)
Browse files Browse the repository at this point in the history
  • Loading branch information
femalves authored Apr 17, 2024
1 parent 27c609b commit 3bbdee4
Showing 1 changed file with 8 additions and 11 deletions.
19 changes: 8 additions & 11 deletions biblib/views/library_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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
Expand All @@ -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:
Expand Down

0 comments on commit 3bbdee4

Please sign in to comment.