Skip to content

Fix reading log carousel to check edition-level availability#11417

Merged
cdrini merged 9 commits intointernetarchive:masterfrom
Aayushdev18:11329/fix/edition-availability-carousel
Nov 29, 2025
Merged

Fix reading log carousel to check edition-level availability#11417
cdrini merged 9 commits intointernetarchive:masterfrom
Aayushdev18:11329/fix/edition-availability-carousel

Conversation

@Aayushdev18
Copy link
Contributor

When a specific edition is logged in reading logs, fetch availability at the edition level instead of work level. This ensures correct borrow buttons are shown instead of 'Locate' buttons when the logged edition is available.

Fixes #11329

Closes #11329

Fix

Technical

When a reading log entry has a logged edition (from link_editions_to_works), the code now:

  • Extracts the edition from the work doc's editions field (handles both list and dict formats)
  • Fetches availability using 'openlibrary_edition' mode instead of work-level
  • Attaches the edition availability to the work doc for display
  • Falls back to work-level availability when no edition is present

Testing

  1. Add a book with a specific edition to your reading log (want-to-read, currently-reading, or already-read)
  2. Visit /people/your-username/books to see the carousel
  3. Verify the borrow button shows correctly for the logged edition (not "Locate" when edition is available)
  4. Check that works without logged editions still show correct availability

@cdrini

When a specific edition is logged in reading logs, fetch availability
at the edition level instead of work level. This ensures correct borrow
buttons are shown instead of 'Locate' buttons when the logged edition
is available.

Fixes internetarchive#11329
@github-actions github-actions bot added the Priority: 2 Important, as time permits. [managed] label Nov 4, 2025
Comment on lines 131 to 133
want_to_read.docs = add_availability_with_edition_preference(
want_to_read.docs
)[:5]
Copy link
Collaborator

@cdrini cdrini Nov 5, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can do this more simply with something like

Suggested change
want_to_read.docs = add_availability_with_edition_preference(
want_to_read.docs
)[:5]
want_to_read.docs = add_availability(
[safeget(lambda: d['editions']['docs'][0], d) for d in want_to_read.docs if d.get('title')]
)[:5]

safeget is a helper method we have for situations just like these!

@cdrini
Copy link
Collaborator

cdrini commented Nov 5, 2025

Nice, great work @Aayushdev18 ! We can likely simplify this by calling add_availability on the edition directly. That should just work; we do something similar in a few other spots, eg:

$ add_availability([(w.get('editions') or [None])[0] or w for w in works])

The safeget helper should make it a bit more concise!

docs_without_editions = []

for doc in filtered_docs:
# editions can be a list [edition] or dict {'docs': [edition]}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh good catch! I expect this should only be the dict, but let me know if you find otherwise!

@cdrini cdrini added the Needs: Submitter Input Waiting on input from the creator of the issue/pr [managed] label Nov 5, 2025
Simplify the add_availability_with_edition_preference function by using
the safeget helper to extract editions directly, following the pattern
from work_search.html. This makes the code more concise and handles
both dict and list edition structures gracefully.
@github-actions github-actions bot removed the Needs: Submitter Input Waiting on input from the creator of the issue/pr [managed] label Nov 5, 2025
@Aayushdev18
Copy link
Contributor Author

@cdrini
Thanks for the feedback and suggestion. I've implemented the simplification using safeget as you recommended. Much cleaner reduced from ~50 lines to just 3 lines per carousel.

Regarding the editions structure: I checked the codebase and found that in link_editions_to_works (line 273 in bookshelves.py), it currently sets work_doc.editions = [edition] (as a list). However, I noticed the comment at line 255 mentions doc["editions"]["docs"], suggesting a dict format. So there might be an inconsistency in how editions are structured in different parts of the codebase.

The good news is that using safeget(lambda: d['editions']['docs'][0], d) handles both cases gracefully:
If editions is a dict with docs, it extracts the first edition
If editions is a list, missing, or in a different format, safeget catches the exception and falls back to the work itself (d)
This matches the pattern you showed in work_search.html. The implementation should work regardless of the structure, which is great. If you'd like me to investigate further or standardize the structure, I'm happy to look into it.

Thanks again for the guidance.

@github-actions github-actions bot added the Needs: Response Issues which require feedback from lead label Nov 6, 2025
@Aayushdev18 Aayushdev18 requested a review from cdrini November 10, 2025 03:51
@Aayushdev18
Copy link
Contributor Author

Hi @cdrini, just a gentle follow-up on this PR. All checks have passed , would you be able to take a look when you have time? Thanks!

@cdrini
Copy link
Collaborator

cdrini commented Nov 13, 2025

Hi @Aayushdev18 ! I tested this, and it seemed to alas not fix the issue! The buttons still displayed "locate". This might need some more debugging, I reckon.

@cdrini cdrini added Needs: Submitter Input Waiting on input from the creator of the issue/pr [managed] and removed Needs: Response Issues which require feedback from lead labels Nov 13, 2025
The previous implementation was calling add_availability on editions,
but the template reads availability from work documents. This fix:
- Extracts editions using safeget
- Gets edition-level availability using get_availability('openlibrary_edition')
- Attaches availability to work documents (not editions)
- Falls back to work-level availability when no edition is available

This should fix the 'Locate' button issue by properly setting
availability on work docs that the template expects.
@github-actions github-actions bot removed the Needs: Submitter Input Waiting on input from the creator of the issue/pr [managed] label Nov 13, 2025
pre-commit-ci bot and others added 3 commits November 13, 2025 05:42
The link_editions_to_works function sets editions as a list [edition],
but the code was only checking for dict format {'docs': [edition]}.
This fix handles both cases to properly extract editions and get
edition-level availability.
@Aayushdev18
Copy link
Contributor Author

Hi @cdrini! Thanks for testing. I found the issue: link_editions_to_works sets editions as a list [edition], but the code was only checking the dict format {'docs': [edition]}.
I've updated the code to handle both formats:
If editions is a list, it extracts editions[0]
If editions is a dict, it extracts editions['docs'][0]
This should correctly extract editions, get edition-level availability, and attach it to the work documents. The fix is pushed and ready for testing.
Could you test again when you have a chance? Thanks!

@github-actions github-actions bot added the Needs: Response Issues which require feedback from lead label Nov 13, 2025
@cdrini
Copy link
Collaborator

cdrini commented Nov 29, 2025

Hi @Aayushdev18 thanks for the update! I spent some time on this one, and man what a headache to debug! I think you're exactly correct, the editions on the works here are stored as a raw list, not as a {docs: list[dict]} object like in most other places throughout the site. Not sure why the previous solutions didn't fix it, but I tweaked the PR to create a get_editions helper method to specifically handle that ambiguous format. Now it seems to be working on testing; lgtm!

@cdrini cdrini merged commit 427e6c3 into internetarchive:master Nov 29, 2025
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Needs: Response Issues which require feedback from lead Priority: 2 Important, as time permits. [managed]

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bad availability information in borrow buttons on reading log carousels

2 participants