Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix web book toast message not matching the web book read #9663

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 35 additions & 4 deletions openlibrary/book_providers.py
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ def get_access(

def get_acquisitions(
self,
edition: Edition,
edition: Edition | web.Storage,
) -> list[Acquisition]:
if edition.providers:
return [Acquisition.from_json(dict(p)) for p in edition.providers]
Expand Down Expand Up @@ -299,6 +299,20 @@ def get_access(
else:
return EbookAccess.PUBLIC

def get_acquisitions(
self,
edition: Edition,
) -> list[Acquisition]:
return [
Acquisition(
access='open-access',
format='web',
price=None,
url=f'https://archive.org/details/{self.get_best_identifier(edition)}',
provider_name=self.short_name,
)
]


class LibriVoxProvider(AbstractBookProvider):
short_name = 'librivox'
Expand Down Expand Up @@ -425,13 +439,30 @@ def solr_key(self):
return None

def get_identifiers(self, ed_or_solr: Edition | dict) -> list[str]:
# It's an edition
if ed_or_solr.get('providers'):
return [
"""
Note: This will only work for solr records if the provider field was fetched
in the solr request. (Note: this field is populated from db)
"""
if providers := ed_or_solr.get('providers', []):
identifiers = [
provider.url
for provider in map(Acquisition.from_json, ed_or_solr['providers'])
if provider.ebook_access >= EbookAccess.PRINTDISABLED
]
to_remove = set()
for tbp in PROVIDER_ORDER:
# Avoid infinite recursion.
if isinstance(tbp, DirectProvider):
continue
if not tbp.get_identifiers(ed_or_solr):
continue
for acq in tbp.get_acquisitions(ed_or_solr):
to_remove.add(acq.url)

return [
identifier for identifier in identifiers if identifier not in to_remove
]

else:
# TODO: Not implemented for search/solr yet
return []
Expand Down
12 changes: 9 additions & 3 deletions openlibrary/templates/book_providers/direct_read_button.html
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
$def with(acquisition, domain)
$# :param Acquisition acquisition:
$# :param domain str:
$ clean_domain = domain.replace(".", "")

$if acquisition.access == 'open-access':
<div class="cta-button-group">
Expand All @@ -10,7 +11,7 @@
class="cta-btn cta-btn--available cta-btn--read cta-btn--external cta-btn--direct"
target="_blank"
aria-haspopup="true"
aria-controls="direct-provider-toast"
aria-controls="direct-provider-toast-$(clean_domain)"
>$_('Read')</a>
</div>

Expand All @@ -23,8 +24,13 @@
>$_('Preview')</a>
</div>

$if render_once('direct-provider-toast'):
<div class="toast toast--book-provider" data-toast-trigger=".cta-btn--direct" id="direct-provider-toast" style="display:none">
$if render_once('direct-provider-toast-' + clean_domain):
<div
class="toast toast--book-provider"
data-toast-trigger="[aria-controls=direct-provider-toast-$(clean_domain)]"
id="direct-provider-toast-$(clean_domain)"
style="display:none"
>
<div class="toast__body">
$:_('This book is freely available from <a href="%s">%s</a>, an external third-party book provider.', acquisition.url, domain)
<a href="/trusted-book-providers#web-books">$_('Learn more')<a>
Expand Down
Loading