Repository navigation
Conversation
|
@gauranshahuja please rebase and resolve conflicts |
c3e5d03 to
be82d15
Compare
|
Rebased onto master now that #13578 has merged, conflicts resolved, CI green. Two things changed beyond the plain rebase:
Taking it out of draft. |
RayBB
left a comment
There was a problem hiding this comment.
Can you please try to simplify your changes and follow the patters now done in the other PRs? For example, one method to gather the data to be passed to the template instead of a function that directly renders the template.
Trying to give quick feedback before I look in depth soon
be82d15 to
15be594
Compare
|
Also, a few more conflicts popped up to resolve |
df660e1 to
1de83c9
Compare
1de83c9 to
a44e38a
Compare
Shows one book carousel and shows a carousel for a query now use a macro. - Move book carousel HTML into macro book_carousel(show, title, url, key, grid, compact, loadjs, config_json, cards) in books/custom_carousel.html.jinja. Keep direct render with guard 'if show is defined' so Templetor callers still work. - Change RawQueryCarousel.html.jinja to import the macro and call it with explicit arguments. Remove shared-scope include. This prevents outer scope pollution. - Shorten headers to one line per jinja.md best practice. This follows docs/wiki/developers/frontend/jinja.md: use a macro for a reusable piece, do not pollute outer scope, keep macro files short.
Fix 1: Add per-card try/except in get_book_carousel_data. One bad card now does not stop the full carousel. This matches 13578. Fix 2: Add full_path input to get_book_carousel_data, get_query_carousel_data and LazyCarouselPartial.generate_async. FastAPI now passes full_path with Depends(get_fullpath). Templetor callers now pass web.ctx.fullpath explicitly. Remove use of global web.ctx.fullpath inside the helper. This makes the dependency clear and testable. This matches 13578. Update callers: home/index.html, account/loans.html, account/mybooks.html, macros/QueryCarousel.html.
…arams - Rename partials.get_carousel_data to get_book_carousel_data so it no longer collides with openlibrary.core.carousels.get_carousel_data, which is a different function with a different signature. - Remove the no-op use_cache and lazy params from QueryCarousel and ListCarousel: the cached-eager path they selected was removed when the partial moved to Jinja, so both params have been ignored. - Drop the now-meaningless use_cache=False at the one caller that set it.
RayBB
left a comment
There was a problem hiding this comment.
This one really was a sprawling one. You really gave me a PR in good shape so nice work with that. I did end up pushing some more refactoring to clean it up but that's it.
Thanks a lot for your work you're doing great!
I tested this pretty well locally and on testing by using various carousels. There are whitespace differences (in the html) but that's it.
Would you like to take on #13569 if so just leave a comment there.
Port NearbyBooksPartial onto the Jinja carousel (RawQueryCarousel and custom_carousel were converted upstream in internetarchive#13590).
Closes #13571
Refactor: converts the LazyCarousel partial (
macros/RawQueryCarousel.htmlandbooks/custom_carousel.html, the carousel every site carousel renders through) from Templetor to Jinja, following #13563 / #13577 and the epic #13566 playbook. No behavior change: the parsed DOM is identical.Draft until #13578 (CarouselCard) lands: the card is still the Templetor
custom_carousel_card, bridged from Python in one place (_render_carousel_card), which then becomes a call to that PR'srender_carousel_card().Technical
openlibrary/macros/RawQueryCarousel.html.jinjaandopenlibrary/templates/books/custom_carousel.html.jinja, ports of the two Templetor files (deleted;scripts/check_unused_templates.pypasses). The templates are now only markup: the logic they carried moved to Python inopenlibrary/plugins/openlibrary/partials.py, next toLazyCarouselPartial:render_query_carousel()(was theRawQueryCarouselmacro) owns thesafe_modequery rewrite, the lazy-loaddata-configJSON, the/search?URL, the Solr call when nobooks_datawas pre-fetched (gather_lazy_carousel_data, unchanged) and theload_moreconfig. The template does not do IO any more.render_book_carousel()(wasbooks/custom_carousel.html) owns themin_books/testgate, the carouseldata-configJSON, and the per-book shape resolution (first edition from a list or adocsdict, Thing kept as-is vs dict →web.storage, authors and loan from the work record), verbatim from the template.@public, the way Convert SearchFacets partial to Jinja #13577'srender_search_facets()is, so the Templetor pages that embed carousels call them directly:macros/QueryCarousel.html(stays Templetor, as the issue says),home/index.html,account/loans.html,account/mybooks.html. All four callers of the deletedcustom_carousel.htmlare switched in this PR.QueryCarousel.html'suse_cachebranch calledCacheableMacro("RawQueryCarousel", ...), which memoized the Templetor macro throughrender_cached_macro. It now callsrender_cached_query_carousel(), which memoizesrender_query_carouselwith the same key prefix (macro name + language, print-disabled, sfw, bot) and the same five minute timeout. To share that, the prefix builder is extracted fromrender_cached_macrointocached_macro_key_prefix()inplugins/upstream/utils.py(withCACHED_MACRO_TIMEOUT);render_cached_macroitself is unchanged in behaviour and keeps its test.macros/CacheableMacro.htmlhad no other caller and is deleted.LoadingIndicatoris bridged (not converted), rendered from Python throughrender_macrolike Convert CarouselCard partial to Jinja (#13570) #13578 does forLoanStatus.LazyCarouselPartial.generate_async()callsrender_query_carousel(..., lazy=False, books_data=...)instead ofrender_macro("RawQueryCarousel", ...); the{"partials": html}contract is unchanged.public(gather_lazy_carousel_data)is dropped: its only template caller was the deleted macro._()msgids are unchanged;messages.potonly changes location comments (Loading carouselandLoading...are now extracted frompartials.py).One thing I kept as-is rather than fix, since this is a pure engine swap:
QueryCarousel.html'suse_cache and not lazybranch does not passlazythrough, so the cached render is the lazy placeholder (the macro's default). That is what it did before too. Happy to fix in a follow-up if wanted.Testing
Rendered the old Templetor macro/template (from master) and the new Python + Jinja path with the same fixtures, and compared the parsed DOM (tags, decoded attribute values, text nodes), not bytes. 17 scenarios, all identical:
custom_carousel: no books (renders nothing), no books withtest, fewer thanmin_books, 10 mixed books (Solr docs witheditionsas a list / adocsdict / an emptydocslist / no editions, a Thing-like record with.key, a hostile<script> & "quotes"title, a loan,authors) in compact mode with a fullload_more, grid layout withsecondary_action, title without url,load_more={}and a partialload_more. The card was stubbed on both sides to serialise exactly what it is handed, so the edition/Thing/storage resolution,lazypast the sixth card,authorsandloanare compared too.RawQueryCarousel: lazy placeholder minimal / with every option /fallback=True/ title only; eager with pre-fetched books and the search form; eager without a url (the/search?urlencodepath) in grid layout; eager with nobooks_data(the Solr call, stubbed to echo its arguments into the output, soquery,sort,limit,has_fulltext_only,safe_modeare compared); eager with no results, with and without the search form.Byte-level differences only: djlint attribute layout and
"vs", as the epic describes.lazy-carousel.jsandcarousel.jsreaddata-configthroughdatasetandJSON.parse, so the quote encoding is safe.djlint
--lint/--reformat(v1.43.2), ruff, mypy,scripts/check_unused_templates.py,scripts/detect_missing_i18n.py: clean.test_templates.py,test_jinja.py,test_partials.py,test_home.py,upstream/tests/test_utils.py: same results as master (thetest_all_jinja_templates_render_valid_htmlsubtest and the threetest_hometemplates fail on my Windows checkout on master as well; environment, not this change).Endpoints to check on a dev instance:
GET /partials/LazyCarousel.json?query=subject:romance&title=Romance&key=romance(the lazy-loaded carousels on the home page) and the home page itself, which renders the non-lazy "Books We Love" / "Recently Returned" carousels throughrender_book_carousel.I have not run
make test-py-uvin Docker locally; relying on CI for the full run.Screenshot
N/A — the partial's HTML is DOM-identical (see Testing).
Stakeholders
@RayBB