Skip to content

Commit

Permalink
Make QueryMacro load lazily
Browse files Browse the repository at this point in the history
  • Loading branch information
cdrini committed Sep 27, 2022
1 parent 85a570f commit d225707
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 32 deletions.
34 changes: 2 additions & 32 deletions openlibrary/macros/QueryCarousel.html
Original file line number Diff line number Diff line change
@@ -1,34 +1,4 @@
$def with(query, title=None, sort='new', key='', limit=20, search=False, has_fulltext_only=True, url=None)
$# Also expose query carousel as a macro

$# Takes following parameters
$# * query (str) -- Any arbitrary Open Library search query, e.g. subject:"Textbooks"
$# * title (str) -- A title to show above the carousel (links to /search?q=query)
$# * sort (str) -- optional sort param defined within work_search.py `work_search`
$# * key (str) -- unique name of the carousel in analytics
$# * limit (int) -- initial number of books to pull
$# * search (bool) -- whether to include search within collection

$# Set default for key
$ key = key or str(abs(hash(query)))

$# Enable search within this query
$if search:
<form action="/search" class="olform pagesearchbox">
<input type="hidden" name="q" value="$query"/>
$if has_fulltext_only:
<input type="hidden" name="has_fulltext" value="true"/>
<input type="text" placeholder="Search collection" name="q2"/>
<input type="submit"/>
</form>

$code:
params = { 'q': query, 'fields': '*,availability' }
url = url or "/search?" + urlencode(params)
if has_fulltext_only:
params['has_fulltext'] = 'true'

results = work_search(params, sort=sort, limit=limit)
books = [storage(b) for b in (results.get('docs', []))]
load_more = {"url": "/search.json?" + urlencode(params), "limit": limit }

$:render_template("books/custom_carousel", books=books, title=title, url=url, key=key, load_more=load_more)
$:render_template_lazy("books/query_carousel", query, title=title, sort=sort, key=key, limit=limit, search=search)
34 changes: 34 additions & 0 deletions openlibrary/templates/books/query_carousel.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
$def with(query, title=None, sort='new', key='', limit=20, search=False, has_fulltext_only=True, url=None)

$# Takes following parameters
$# * query (str) -- Any arbitrary Open Library search query, e.g. subject:"Textbooks"
$# * title (str) -- A title to show above the carousel (links to /search?q=query)
$# * sort (str) -- optional sort param defined within work_search.py `work_search`
$# * key (str) -- unique name of the carousel in analytics
$# * limit (int) -- initial number of books to pull
$# * search (bool) -- whether to include search within collection

$# Set default for key
$ key = key or str(abs(hash(query)))

$# Enable search within this query
$if search:
<form action="/search" class="olform pagesearchbox">
<input type="hidden" name="q" value="$query"/>
$if has_fulltext_only:
<input type="hidden" name="has_fulltext" value="true"/>
<input type="text" placeholder="Search collection" name="q2"/>
<input type="submit"/>
</form>

$code:
params = { 'q': query, 'fields': '*,availability' }
url = url or "/search?" + urlencode(params)
if has_fulltext_only:
params['has_fulltext'] = 'true'

results = work_search(params, sort=sort, limit=limit)
books = [storage(b) for b in (results.get('docs', []))]
load_more = {"url": "/search.json?" + urlencode(params), "limit": limit }

$:render_template("books/custom_carousel", books=books, title=title, url=url, key=key, load_more=load_more)

0 comments on commit d225707

Please sign in to comment.