Skip to content

Commit

Permalink
Merge pull request #6574 from cdrini/feature/classics-carousel
Browse files Browse the repository at this point in the history
Make Classics carousel show only classic books
  • Loading branch information
mekarpeles authored May 27, 2022
2 parents 8e6b1f7 + 6a2eb8a commit 0f3b754
Show file tree
Hide file tree
Showing 3 changed files with 2 additions and 62 deletions.
44 changes: 0 additions & 44 deletions openlibrary/plugins/openlibrary/home.py
Original file line number Diff line number Diff line change
Expand Up @@ -762,50 +762,6 @@ def generic_carousel(
return storify(books) if books else books


@public
def readonline_carousel():
"""Return template code for books pulled from search engine.
TODO: If problems, use stock list.
"""
try:
data = random_ebooks()
if len(data) > 30:
data = lending.add_availability(random.sample(data, 30))
data = [d for d in data if d['availability'].get('is_readable')]
return storify(data)

except Exception:
logger.error("Failed to compute data for readonline_carousel", exc_info=True)
return None


def random_ebooks(limit=2000):
solr = search.get_solr()
sort = "edition_count desc"
result = solr.select(
query='has_fulltext:true -public_scan_b:false',
rows=limit,
sort=sort,
fields=[
'has_fulltext',
'key',
'ia',
"title",
"cover_edition_key",
"author_key",
"author_name",
],
)

return [format_work_data(doc) for doc in result.get('docs', []) if doc.get('ia')]


# cache the results of random_ebooks in memcache for 15 minutes
random_ebooks = cache.memcache_memoize(
random_ebooks, "home.random_ebooks", timeout=15 * 60
)


def format_list_editions(key):
"""Formats the editions of a list suitable for display in carousel."""
if 'env' not in web.ctx:
Expand Down
17 changes: 0 additions & 17 deletions openlibrary/plugins/openlibrary/tests/test_home.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,22 +74,6 @@ def test_stats_template(self, render_template):
html = str(render_template("home/stats"))
assert html == ""

def test_read_template(self, render_template, monkeypatch):
# getting read-online books fails because solr is not defined.
# Empty list should be returned when there is error.
monkeypatch.setattr(home, 'random_ebooks', lambda: None)
books = home.readonline_carousel()
html = str(
render_template(
"books/custom_carousel",
books=books,
title="Classic Books",
url="/read",
key="public_domain",
)
)
assert html.strip() == ""

def test_home_template(self, render_template, mock_site, monkeypatch):
self.setup_monkeypatch(monkeypatch)
docs = [
Expand Down Expand Up @@ -150,7 +134,6 @@ def spoofed_generic_carousel(*args, **kwargs):
"Kids",
"Thrillers",
"Romance",
"Classic Books",
"Textbooks",
]
for h in headers:
Expand Down
3 changes: 2 additions & 1 deletion openlibrary/templates/home/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@

$:render_template("home/welcome", test=test)

$:render_template("books/custom_carousel", books=readonline_carousel(), title=_('Classic Books'), url="/read", key="public_domain", test=test)
$if not test:
$:macros.QueryCarousel(query="ddc:8* first_publish_year:[* TO 1950] publish_year:[2000 TO *] -public_scan_b:false", title=_('Classic Books'), key="public_domain", url="/read", sort='random.hourly')

$if monthly_reads and not test:
$:macros.QueryCarousel(query=monthly_reads['query'], title=monthly_reads['title'], key="monthly_reads", url=monthly_reads['url'], sort='random.hourly')
Expand Down

0 comments on commit 0f3b754

Please sign in to comment.