-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
7311/feature/loan history code for rendering loan_history page #8375
7311/feature/loan history code for rendering loan_history page #8375
Conversation
See also: https://github.com/internetarchive/openlibrary/wiki/Developing-The-Reading-Log The goal is to:
First, get the loan_history and extract a list of archive.org identifiers (we call ocaids)
Next, get the availability of these ocaids, which will also give us the openlibrary edition keys that we can use to fetch the records on Open Library:
Next, fetch the editions corresponding to these books:
availability is already a dictionary that maps ocaid to its availability so it's easy to loop over each edition, lookup its availability by ocaid, and attach the availability to the edition. loan_history_map = {} Now, we loop over each edition, and we use the edition's ocaid (if it exists) to look up the availability from the availability map and the loan_record from the loan_history_map:
All together, including the fetch of patron s3 credentials, the code looks something like:
|
4877270
to
ed4b30b
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi, @anujamerwade. Thanks for this!
I tested it out, and there are a few things we should probably still address prior to merging this PR.
- pagination shows the same results on each page;
- each page shows all the results, even if the
results_per_page
value causes pagination to display (e.g. if there are 55 results, 3 pages will display by default, with a theoretical 25 per page, but each page will have all 55 results displayed); - book availability not working properly -- books that should say 'borrow' say 'read' for some reason;
- the IA loan API is returning all loans, even those that aren't in OL. So the API might return 100 results, but, say, 93 might show up in OL, because we account for the fact that not every result from the IA loan API will be in OL. But this means the "only 100 results" message won't display, as it will see only 93 results, rather than the 100 returned.
Possible (partial) solutions
- to address the pagination issue,
$:macros.Pager(current_page, doc_count, results_per_page=results_per_page)
could be commented out inloan_history.html
until we deal with pagination properly. - a non-technical response to the difference between the number of OCAIDs returned from the IA loan-history endpoint and the results that actually show up in
loan_history_map
might be to change the language about the result limit inloan_history.html
to sayLoan history currently limited to at most 100 records, and then edit
loan_history.htmlto show that message when if
doc_count == 100, (as opposed to comparing
len(docs)` to 100.)
I am not sure why the borrow
buttons (almost) all show read
, but that is probably also something we should fix prior to merging. I looked a bit and didn't immediately see a solution, but let me know if you'd like more help. :)
Finally, there is some commented out code that we should probably remove, e.g., in openlibrary/plugins/upstream/mybooks.py
.
Hi @scottbarnes , thank you for pointing out the errors. I will work on them this week and get back to you in case of any issues. |
I looked at this a bit more and the buttons showing
rather than
That said, at least in the development environment, these buttons don't appear to work correctly on the reading log or in the loan history, insofar as ensuring |
@scottbarnes and I have tried debugging the reason why we face challenges while moving through pages on the
@mekarpeles your suggestion would be helpful. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just a few suggestions.
00141c8
to
ef82426
Compare
I updated this to handle the case where there are no loan history results from Internet Archive that are in in Open Library for a given page of loans -- this logic will keep advancing pages until there is at least 1 loan from Internet Archive that is in Open Library. However, there are at least two issues with this approach. First, the code isn't currently tracking the state, so when the page renders the This leads to the second limitation. Advancing through 'empty' pages where no item in the Internet Archive history is in Open Library is forward-looking only. This means that if someone clicks I am unsure of the best way to address this, but I was thinking of adding another URL parameter that is something like As for the case where the final page in the loan history is one that has no items in Open Library, the page will simply display Thoughts? Granted, the right way to handle this would be modify the IA API to return only items with OL editions, as Drini suggested. I'll investigate this more today. Finally, when @mekarpeles's |
5420f6a
to
afd0315
Compare
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## master #8375 +/- ##
=======================================
Coverage 16.62% 16.62%
=======================================
Files 88 88
Lines 4698 4698
Branches 838 838
=======================================
Hits 781 781
Misses 3399 3399
Partials 518 518 ☔ View full report in Codecov by Sentry. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
# 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 item: item.get('last_loan_date', ''), reverse=True | ||
) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's check against the add_availability
function to see if this may diverge (and if there's a chance to DRY this code so we don't have to re-implement the add_availability logic)
The update to `mybooks-list.less` is to stop the length of IA identifiers from causing an overflow issue.
The `loans` logic is in `account.py`. Move tho `loan-history` logic there as well.
0e85b88
to
a48c37b
Compare
576d5cb
to
d840b57
Compare
One shortcoming here is that if Internet Archive has scanned a single edition multiple times, it's possible both copies of the Internet Archive item/edition will point to a single Open Library edition, and that Open Library edition will only point to one of the Internet Archive items. This creates a problem when a patron has borrowed the Internet Archive item that Open Library doesn't point to. See, e.g., climbersguidetoh0000rope and climbersguidetot00rope. They both point to OL5214872M, quite rightly. However, As I checked out the former, but Open Library points to the latter, it causes the loan history to show However, when I follow the link to the Open Library item, I see the book is borrowable (as climbersguidetot00rope, rather than the one I borrowed.: Not sure there is a good solution to this, as currently Open Library can only be associated with one Thoughts? |
d840b57
to
7114cc2
Compare
4d1c548
to
939a621
Compare
|
||
Returns a dict of the form: `{"ocaid1": edition1, "ocaid2": edition2, ...}` | ||
""" | ||
ocaid_availability = get_availability_of_ocaids(ocaids=ocaids) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We could possibly modify get_availability_of_ocaids
to pass in the patron's loans (or have a boolean flag to use the currently logged in patron's s3 keys if we want to know what a book is actively being borrowed by this patron.
for ed in editions: | ||
if ed.ocaid in ocaids: | ||
ed.availability = availability.get(ed.ocaid) | ||
ed.loan = loan_history_map[ed.ocaid] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This was presumably causing a problem because in LoanStatus, we check a book method called ed.loan to fetch any loan that may be attached to the book and what we're doing here (which is attaching a loan history record to the book) was technically conflating two different loan types.
The possible solutions are either to:
- not attach the loan to the edition (if it's not being used)
- attach the loan to the edition under a different name like
loan_history
- if the
loan_history
record has enough info associated to determine if it's active (e.g. an expiration date) and also assuming the ed.loan_history is in the same format as ed.loan else where on openlibrary.org. then consider modifying LoanStatus.html's check ~L37 to consider the expiry when determining if a book should show up as read.
@anujamerwade + @scottbarnes congratulations on getting this feature over the finish line |
Closes #7311
Feature: This is a draft PR to create a borrow history page on openlibrary.org instead of being redirected to the internetarchive.org currently.
Technical
Testing
Screenshot
Stakeholders