|
2 | 2 | import json |
3 | 3 | import logging |
4 | 4 | import re |
5 | | -from typing import Any, Final |
| 5 | +from typing import TYPE_CHECKING, Any, Final |
6 | 6 | from collections.abc import Callable |
7 | 7 | from collections.abc import Iterable, Mapping |
8 | 8 |
|
|
27 | 27 | from openlibrary.core.bookshelves import Bookshelves |
28 | 28 | from openlibrary.core.lending import ( |
29 | 29 | get_availability_of_ocaids, |
| 30 | + get_items_and_add_availability, |
30 | 31 | s3_loan_api, |
31 | 32 | ) |
32 | 33 | from openlibrary.core.observations import Observations |
|
45 | 46 | from openlibrary.plugins.upstream import borrow, forms, utils |
46 | 47 | from openlibrary.utils.dateutil import elapsed_time |
47 | 48 |
|
| 49 | + |
| 50 | +if TYPE_CHECKING: |
| 51 | + from openlibrary.core.models import Edition |
| 52 | + |
48 | 53 | logger = logging.getLogger("openlibrary.account") |
49 | 54 |
|
50 | 55 | CONFIG_IA_DOMAIN: Final = config.get('ia_base_url', 'https://archive.org') |
@@ -1184,7 +1189,7 @@ def process_goodreads_csv(i): |
1184 | 1189 | return books, books_wo_isbns |
1185 | 1190 |
|
1186 | 1191 |
|
1187 | | -def get_loan_history_data(page: int, mb: "MyBooksTemplate") -> dict[str, str | int]: |
| 1192 | +def get_loan_history_data(page: int, mb: "MyBooksTemplate") -> dict[str, Any]: |
1188 | 1193 | """ |
1189 | 1194 | Retrieve IA loan history data for page `page` of the patron's history. |
1190 | 1195 |
|
@@ -1219,43 +1224,28 @@ def get_loan_history_data(page: int, mb: "MyBooksTemplate") -> dict[str, str | i |
1219 | 1224 | loan_history.pop() |
1220 | 1225 |
|
1221 | 1226 | ocaids = [loan_record['identifier'] for loan_record in loan_history] |
1222 | | - ocaid_availability = get_availability_of_ocaids(ocaids) |
1223 | 1227 | loan_history_map = { |
1224 | 1228 | loan_record['identifier']: loan_record for loan_record in loan_history |
1225 | 1229 | } |
1226 | | - editions = web.ctx.site.get_many( |
1227 | | - [ |
1228 | | - f"/books/{item.get('openlibrary_edition')}" |
1229 | | - for item in ocaid_availability.values() |
1230 | | - if item.get('openlibrary_edition') |
1231 | | - ] |
1232 | | - ) |
1233 | 1230 |
|
1234 | | - # Create 'placeholder' editions for items in the Internet Archive loan |
1235 | | - # history, but absent from Open Library. |
1236 | | - ia_only_loans = [ |
1237 | | - loan |
1238 | | - for loan in ocaid_availability.values() |
1239 | | - if not loan.get('openlibrary_edition') |
1240 | | - ] |
1241 | | - |
1242 | | - # Attach availability and loan history info (for sorting) to both editions and |
1243 | | - # ia-only items. |
1244 | | - for ed in editions: |
1245 | | - if ed.ocaid in ocaids: |
1246 | | - ed.availability = ocaid_availability.get(ed.ocaid) |
1247 | | - ed.last_loan_date = loan_history_map[ed.ocaid].get('updatedate') |
1248 | | - |
1249 | | - for ia_only in ia_only_loans: |
1250 | | - # ia_only['loan'] isn't set because `LoanStatus.html` reads it as a |
1251 | | - # current loan. No apparenty way to distinguish between current and |
1252 | | - # past loans from this API call. |
1253 | | - loan = loan_history_map[ia_only['identifier']] |
1254 | | - ia_only['last_loan_date'] = loan.get('updatedate', '') |
| 1231 | + # Get editions and attach their loan history. |
| 1232 | + editions_map = get_items_and_add_availability(ocaids=ocaids) |
| 1233 | + for edition in editions_map.values(): |
| 1234 | + edition.last_loan_date = loan_history_map[edition.ocaid].get('updatedate') |
| 1235 | + |
| 1236 | + # Create 'placeholders' for items in the Internet Archive loan history, but |
| 1237 | + # absent from Open Library, and then add loan history. |
| 1238 | + # ia_only['loan'] isn't set because `LoanStatus.html` reads it as a current |
| 1239 | + # loan. No apparenty way to distinguish between current and past loans with |
| 1240 | + # this API call. |
| 1241 | + ia_only_loans = [{'ocaid': ocaid} for ocaid in ocaids if ocaid not in editions_map] |
| 1242 | + for ia_only_loan in ia_only_loans: |
| 1243 | + loan_data = loan_history_map[ia_only_loan['ocaid']] |
| 1244 | + ia_only_loan['last_loan_date'] = loan_data.get('updatedate', '') |
1255 | 1245 | # Determine the macro to load for loan-history items only. |
1256 | | - ia_only['ia_only'] = True # type: ignore[typeddict-unknown-key] |
| 1246 | + ia_only_loan['ia_only'] = True # type: ignore[typeddict-unknown-key] |
1257 | 1247 |
|
1258 | | - editions_and_ia_loans = editions + ia_only_loans |
| 1248 | + editions_and_ia_loans = list(editions_map.values()) + ia_only_loans |
1259 | 1249 | editions_and_ia_loans.sort( |
1260 | 1250 | key=lambda item: item.get('last_loan_date', ''), reverse=True |
1261 | 1251 | ) |
|
0 commit comments