Skip to content
Permalink
Browse files

Fixing subjects carousels, adds correct availability to API (#1978)

* fixing subjects carousels with correct  availability
  • Loading branch information...
mekarpeles committed Mar 12, 2019
1 parent 8e964b5 commit 95c98c90685b71a9442290ee51cb11db8b1715b3
@@ -248,6 +248,12 @@ def get_availability_of_editions(ol_edition_ids):
@public
def get_realtime_availability_of_ocaid(ocaid):
url = 'https://archive.org/metadata/%s?dontcache=1' % ocaid
statuses = {
'available': 'borrow_available',
'unavailable': 'borrow_unavailable',
'private': 'private',
'error': 'error'
}
try:
content = urllib2.urlopen(url=url, timeout=config_http_request_timeout).read()
metadata = simplejson.loads(content).get('metadata', {})
@@ -265,15 +271,15 @@ def get_realtime_availability_of_ocaid(ocaid):
def add_availability(editions):
"""Adds API v2 availability info to editions, e.g. for work's editions table
"""
ocaids = [
(ed.get('ocaid') or ed.ia[0]) for ed in editions if
(ed.get('ia') or ed.get('ocaid'))
]
def get_ocaid(ed):
if ed.get('ocaid') or ed.get('identifier') or ed.get('ia'):
return ed.get('ocaid') or ed.get('identifier') or (
ed.ia[0] if isinstance(ed.ia, list) else ed.ia)

ocaids = [ocaid for ocaid in [get_ocaid(ed) for ed in editions] if ocaid]
availability = get_availability_of_ocaids(ocaids)
for i, ed in enumerate(editions):
ocaid = None
if ed.get('ocaid') or ed.get('ia'):
ocaid = ed.get('ocaid') or ed.get('ia')[0]
ocaid = get_ocaid(ed)
editions[i]['availability'] = availability.get(ocaid) if ocaid else {
'status': 'error'
}
@@ -66,3 +66,7 @@
<form>
<input type="button" class="cta-btn--missing cta-btn" disabled value="No ebook available">
</form>
$if page.ia:
$ daisy_url = "/ia/%s/daisy" % page.ia[0]
$:macros.daisy(page, url=daisy_url, msg=_("Print-disabled access available"))
@@ -86,7 +86,7 @@


$if editions_page and page.get('ocaid'):
$:macros.daisy(page, block_name)
$:macros.daisy(page, block_name=block_name)

$# If waitlisted, show other possible editions after daisy download link
$if (availability_status == 'borrow_unavailable'):
@@ -1,17 +1,19 @@
$def with(page, block_name=None)
$def with(page, url='', msg='', block_name=None)
$# Takes following parameters:
$# * page
$# * block_name is a BEM block name

$ msg = msg or _("Download ebook for print-disabled")

$if block_name:
$ prefix = block_name + '__'
$else:
$ prefix = ''

<p class="$(prefix)print-disabled-download">
<a href="$page.url('/daisy')" title="Download Protected DAISY">
<a href="$(url or page.url('/daisy'))" title="Download Protected DAISY">
<meta itemprop="bookFormat" content="EBook/DAISY3"/>
Download ebook for print-disabled
$msg
<img src="/images/icons/icon-encrypto-sm.png" class="daisy-lock" width="8" height="12" alt="Download Protected DAISY"/>
</a>
</p>
@@ -15,6 +15,7 @@
from infogami.utils.view import render, render_template, safeint

from openlibrary.core.models import Subject
from openlibrary.core.lending import add_availability
from openlibrary.utils import str_to_key, finddict

__all__ = [
@@ -56,7 +57,13 @@ def GET(self, key):
if nkey != key:
raise web.redirect(nkey)

subj = get_subject(key, details=True)
# this needs to be updated to include:
#q=public_scan_b:true+OR+lending_edition_s:*
subj = get_subject(key, details=True, filters={
'public_scan_b': 'false',
'lending_edition_s': '*'
})

subj.v2 = True
delegate.context.setdefault('bodyid', 'subject')
if not subj or subj.work_count == 0:
@@ -96,6 +103,7 @@ def GET(self, key):
i = web.input(offset=0, limit=12, details='false', has_fulltext='true',
sort='editions', available='false')


filters = {}
if i.get('has_fulltext') == 'true':
filters['has_fulltext'] = 'true'
@@ -114,10 +122,11 @@ def GET(self, key):
i.limit = safeint(i.limit, 12)
i.offset = safeint(i.offset, 0)

subject = get_subject(key, offset=i.offset, limit=i.limit, sort=i.sort, details=i.details.lower() == 'true', **filters)
subject_results = get_subject(key, offset=i.offset, limit=i.limit, sort=i.sort,
details=i.details.lower() == 'true', **filters)
if i.has_fulltext:
subject['ebook_count'] = subject['work_count']
return json.dumps(subject)
subject_results['ebook_count'] = subject_results['work_count']
return json.dumps(subject_results)

def normalize_key(self, key):
return key.lower()
@@ -160,15 +169,22 @@ def GET(self, key):
i.limit = safeint(i.limit, 12)
i.offset = safeint(i.offset, 0)

subject = get_subject(key, offset=i.offset, limit=i.limit, details=False, **filters)
return json.dumps(subject)
results = get_subject(key, offset=i.offset, limit=i.limit, details=False, **filters)
return json.dumps(results)

def normalize_key(self, key):
return key.lower()

def process_key(self, key):
return key

def inject_availability(subject_results):
works = add_availability(subject_results.works)
for work in works:
ocaid = work.ia if work.ia else None
availability = work.get('availability', {}).get('status')
subject_results.works = works
return subject_results

def get_subject(key, details=False, offset=0, sort='editions', limit=12, **filters):
"""Returns data related to a subject.
@@ -240,7 +256,10 @@ def create_engine():
sort_order = sort_options.get(sort) or sort_options['editions']

engine = create_engine()
return engine.get_subject(key, details=details, offset=offset, sort=sort_order, limit=limit, **filters)
subject_results = engine.get_subject(
key, details=details, offset=offset, sort=sort_order,
limit=limit, **filters)
return inject_availability(subject_results)

class SubjectEngine:
def get_subject(self, key, details=False, offset=0, limit=12, sort='first_publish_year desc', **filters):
@@ -256,9 +275,11 @@ def get_subject(self, key, details=False, offset=0, limit=12, sort='first_publis
kw = {}

from search import work_search
result = work_search(q, offset=offset, limit=limit, sort=sort, **kw)
result = work_search(
q, offset=offset, limit=limit, sort=sort, **kw)
if not result:
return None

for w in result.docs:
w.ia = w.ia and w.ia[0] or None
w['checked_out'] = False
@@ -31,7 +31,8 @@ <h1 class="inline">
} );
//-->
</script>
<div class="contentBody" style="padding-top:0;">
<div class="contentBody">

$:render_template("books/custom_carousel", books=page.works, key="subjects")

<div class="head">

0 comments on commit 95c98c9

Please sign in to comment.
You can’t perform that action at this time.