Skip to content

Commit

Permalink
Fix sort order and update cover URL for IA-only items
Browse files Browse the repository at this point in the history
  • Loading branch information
scottbarnes committed Dec 22, 2023
1 parent 86b79c6 commit 5420f6a
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 14 deletions.
7 changes: 4 additions & 3 deletions openlibrary/macros/IABook.html
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
$def with (doc, ia_base_url="https://archive.org")
$ ocaid = doc.get('identifier')
<li class="searchResultItem">
<span class="bookcover">
$ cover = "/images/icons/avatar_book-sm.png"
<a href="$ia_base_url/details/$doc.get('identifier')"><img
$ cover = "%s/services/img/%s" % (ia_base_url, ocaid)
<a href="$ia_base_url/details/$ocaid"><img
itemprop="image"
src="$cover"
/></a>
Expand All @@ -11,7 +12,7 @@
<div class="details">
<div class="resultTitle">
<h3 itemprop="name" class="booktitle">
<a itemprop="url" href="$ia_base_url/details/$doc.get('identifier')" class="results">Borrowed from Internet Archive: $doc.get('identifier')</a>
<a itemprop="url" href="$ia_base_url/details/$ocaid" class="results">Borrowed from Internet Archive: $doc.get('identifier')</a>
</h3>
</div>
</div>
Expand Down
29 changes: 18 additions & 11 deletions openlibrary/plugins/upstream/mybooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -662,21 +662,28 @@ def get_loan_history_data(page: int, mb: "MyBooksTemplate") -> dict[str, str | i
]
)

# Attach availability and loan to editions.
for i, ed in enumerate(editions):
if ed.ocaid in ocaids:
editions[i].availability = availability.get(ed.ocaid)
editions[i].loan = loan_history_map[ed.ocaid]

# Include 'placeholder' editions for items in the Internet Archive loan
# history, but absent from Open Library. Also, preserve the order.
# Create 'placeholder' editions for items in the Internet Archive loan
# history, but absent from Open Library.
ia_only_loans = [
loan for loan in availability.values() if not loan.get('openlibrary_edition')
]
for ia_loan in ia_only_loans:
ia_loan['ia_only'] = True

# Attach availability and loan to both editions and ia-only items.
for ed in editions:
if ed.ocaid in ocaids:
ed.availability = availability.get(ed.ocaid)
ed.loan = loan_history_map[ed.ocaid]
ed.last_loan_date = ed.loan.get('updatedate')

for ia_only in ia_only_loans:
loan = loan_history_map[ia_only['identifier']]
ia_only['last_loan_date'] = loan.get('updatedate', '')
ia_only['ia_only'] = True # Determines which macro to load.

editions_and_ia_loans = editions + ia_only_loans
editions_and_ia_loans.sort(key=lambda ed: ed.get('updatedate', ''))
editions_and_ia_loans.sort(
key=lambda item: item.get('last_loan_date', ''), reverse=True
)

result = {
'docs': editions_and_ia_loans,
Expand Down

0 comments on commit 5420f6a

Please sign in to comment.