Skip to content

Move generic_carousel() from templates to Python handlers - #13466

Merged
RayBB merged 8 commits into
internetarchive:masterfrom
Sanket17052006:remove-network-calls-from-carousel-templates
Sep 1, 2026
Merged

Move generic_carousel() from templates to Python handlers#13466
RayBB merged 8 commits into
internetarchive:masterfrom
Sanket17052006:remove-network-calls-from-carousel-templates

Conversation

@Sanket17052006

Copy link
Copy Markdown
Contributor

Part of #13419

Moves generic_carousel() and compose_ia_url() network calls out of the Templetor template (custom_ia_carousel.html) into Python handlers (home.py, account.py). Templates now receive pre-fetched data instead of making network calls directly.

Technical

  • home.py - get_carousel_data() fetches books, URLs, and load_more configs; restores user_lang_only language filtering via get_request_lang(); removes @public from generic_carousel()
  • custom_ia_carousel.html - accepts pre-fetched books, title, url, load_more
  • home/index.html - passes translated titles and pre-built load_more configs from carousel_data
  • account.py andloans.html - reuses get_carousel_data() instead of duplicating carousel logic
  • test_home.py - updated for new data flow, removed dead mock code

Testing

Screenshot

N/A

Stakeholders

@RayBB

@RayBB RayBB self-assigned this Aug 26, 2026
@github-project-automation github-project-automation Bot moved this to Waiting Review/Merge from Staff in Ray's Project Aug 26, 2026
@RayBB
RayBB force-pushed the remove-network-calls-from-carousel-templates branch from 89dce90 to 89d1364 Compare August 26, 2026 22:32

@RayBB RayBB left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Looking pretty good. Two bits of feedback!

  1. get_carousel_data() merges two divergent queries, which empties the staff-picks carousel for non-English UI users. It builds 'languageSorter:("English")' + lang_filter, but the homepage previously used only the user-language filter (per #10691), and the loans page previously used only languageSorter:("English"). The AND of the two (languageSorter:("English") AND language:spa, etc.) matches almost nothing for non-English populated languages, so the carousel goes empty. I'd unify both on just the user-language filter (lang_filter).

  2. /account/loans fetches both carousels and discards recently_returned. Consider making get_carousel_data take the carousel name(s) it should build (e.g. a Literal["staff_picks", "recently_returned"], accepting one or an iterable), so loans only requests staff picks — and the loop becomes an easy asyncio.gather once the fetch goes async.

@github-project-automation github-project-automation Bot moved this from Waiting Review/Merge from Staff to Someone else is working on it in Ray's Project Aug 26, 2026
@Sanket17052006
Sanket17052006 force-pushed the remove-network-calls-from-carousel-templates branch from a3fc247 to 233c242 Compare August 27, 2026 06:48
@RayBB
RayBB force-pushed the remove-network-calls-from-carousel-templates branch from 233c242 to 0c7d4f1 Compare September 1, 2026 19:34
RayBB and others added 5 commits September 1, 2026 12:42
Extract the carousel family (get_carousel_data, generic_carousel,
get_ia_carousel_books, format_book_data, presets/config) out of the home
page controller into a new canonical openlibrary.core.carousels module, and
move caching_prethread into openlibrary.utils.request_context. The homepage
and loans page now both build carousels from openlibrary.core: this removes
the upstream->openlibrary-plugin import in account.py and the pre-existing
core->plugin inversions in lending.py and fulltext.py. The config map is
flattened to name->subject, and an explicit empty carousel list now yields
no carousels instead of silently falling back to all of them.

Generated with Codebuff 🤖
Co-Authored-By: Codebuff <noreply@codebuff.com>
Replace dict[str, dict[str, Any]] with CarouselData and
LoadMoreConfig TypedDicts in openlibrary/core/carousels.py
to make the contract explicit. No behavior change.

@RayBB RayBB left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

@Sanket17052006 ,as always, you did awesome on this one.

I ended up pushing some some refactors that make it so long term things are simplified by having a carousels.py file instead of being concerned about any loops. We were also able to remove custom_ia_carousel.html

To test this PR I compared the homepage and loans page before/after in english and in spanish.
As expected, the results were the same for the homepage and now the loan page sees language aware results (before it wasn't language aware).

@RayBB
RayBB merged commit acfa38d into internetarchive:master Sep 1, 2026
3 of 4 checks passed
@github-project-automation github-project-automation Bot moved this from Someone else is working on it to Done in Ray's Project Sep 1, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

2 participants