Skip to content

Commit

Permalink
Merge pull request #1081 from internetarchive/hotfix/work-availability
Browse files Browse the repository at this point in the history
DRYing availability check of works, fixing work edition availability
  • Loading branch information
mekarpeles committed Sep 14, 2018
2 parents f758c16 + fa8072b commit 229feb6
Show file tree
Hide file tree
Showing 7 changed files with 82 additions and 75 deletions.
13 changes: 13 additions & 0 deletions openlibrary/core/lending.py
Expand Up @@ -252,6 +252,19 @@ def get_realtime_availability_of_ocaid(ocaid):
except Exception as e:
return {'error': 'request_timeout'}

@public
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'))]
availability = get_availability_of_ocaids(ocaids)
for i, ed in enumerate(editions):
if ed.get('ocaid') or ed.get('ia'):
ocaid = ed.get('ocaid') or ed.get('ia')[0]
editions[i]['availability'] = availability.get(ocaid)
return editions

@public
def get_availability_of_ocaid(ocaid):
"""Retrieves availability based on ocaid/archive.org identifier"""
Expand Down
4 changes: 2 additions & 2 deletions openlibrary/macros/SearchResultsWork.html
Expand Up @@ -66,8 +66,8 @@ <h3 class="booktitle">
</div>
$if cta:
<div class="searchResultItemCTA-lending">
$if availability:
$:macros.AvailabilityButton(doc, availability, user)
$if availability or doc.get('availability'):
$:macros.AvailabilityButton(doc, doc.get('availability') or availability, user)
</div>
</div>
</li>
2 changes: 1 addition & 1 deletion openlibrary/plugins/upstream/models.py
Expand Up @@ -630,7 +630,7 @@ def get_sorted_editions(self):

for book in books:
book.availability = availability.get(book.ocaid) or {"status": "error"}
return books
return books[::-1]
else:
return []

Expand Down
93 changes: 42 additions & 51 deletions openlibrary/templates/books/edition-sort.html
@@ -1,5 +1,7 @@
$def with (book)

$ availability = book.get('availability', {})

$ worldcat = "https://worldcat.org/isbn/XXX"
$ worldcatoclc = "https://worldcat.org/oclc/XXX"

Expand All @@ -25,14 +27,6 @@
$elif isbn_13:
$ isbn_10 = isbn_13_to_isbn_10(isbn_13)

$ library = None
$ collection = set()
$ meta_fields = book.get_ia_meta_fields()
$if meta_fields:
$ collection = meta_fields.get('collection', [])
$if 'inlibrary' in collection and 'inlibrary' in ctx.features:
$ library = get_library()

$ url = book.get_cover_url("S") or "/images/icons/avatar_book-sm.png"

<td class="book">
Expand Down Expand Up @@ -60,61 +54,58 @@
$if book.languages:
in $', '.join(l.name for l in book.languages)
$if book.edition_name:
-
$book.edition_name
- $book.edition_name
</div>
</div>
</td>

$if 'printdisabled' in collection or 'lendinglibrary' in collection:
<td class="icon read">
<div class="bbbb"></div>
$ availability_class = 'zzzz' if (not availability or availability.get('status') == 'error') else 'aaaa' if (availability.get('status') == 'open') else 'bbbb' if (availability.get('status') == 'borrow_available') else 'cccc' if (availability.get('status') == 'borrow_unavailable') else 'dddd'
$ active = not (availability_class == 'zzzz')

<td class="icon read $('inact' if not active else '')">
<div class="$(availability_class)"></div>
<div class="links">
$if active:
<ul>
<li><a href="$book.url('/daisy')" class="plain"><span class="underline">DAISY</span> <img src="/images/icons/icon-encrypto-sm.png" width="11" height="14" alt="Protected DAISY"/></a></li>
$if availability.get('status') == 'open':
<li class="read-option"><strong><a href="$viewbook.replace('XXX', book.ocaid)" title="Read a scanned version in BookReader" class="read-btn">Read eBook</a></strong></li>

$elif availability.get('status') in ['borrow_available', 'borrow_unavailable']:
<li class="read-option">$:macros.LoanStatus(book, ctx.user)</li>

<li class="read-option">
<a href="$book.url('/daisy')" class="plain print-disabled-download">
<span class="underline">DAISY for print-disabled</span>
<img src="/images/icons/icon-encrypto-sm.png" class="daisy-lock" width="8" height="12" alt="Download ebook for print-disabled (DAISY)"/>
</a>
</li>

$if availability.get('status') == 'open':
<hr>
<ul>
<li><a href="$downloadbook.replace('XXX', book.ocaid)" title="Download a PDF">PDF</a></li>
<li><a href="$epub.replace('XXX', book.ocaid)" title="Download an ePub">ePub</a></li>
<li><a href="$mobi.replace('XXX', book.ocaid)" title="Download a MOBI file from Internet Archive">MOBI</a></li>
<li><a href="$djvutxt.replace('XXX', book.ocaid)" title="Download a plain text version">Plain text</a></li>
</ul>
</ul>
</div>
</td>
$elif book.ocaid:
<td class="icon read">
<div class="aaaa"></div>
</td>

<td class="icon locate inact">
<div class="zzzz"></div>
<div class="links">
$if oclc_numbers or isbn:
<ul>
<li><strong><a href="$viewbook.replace('XXX', book.ocaid)" title="Read a scanned version in BookReader" class="read-btn">Read online</a></strong><br/></li>
<li><a href="$downloadbook.replace('XXX', book.ocaid)" title="Download a PDF">PDF</a></li>
<li><a href="$djvutxt.replace('XXX', book.ocaid)" title="Download a plain text version">Plain text</a></li>
<li><a href="$book.url('/daisy')" title="Download a zipped DAISY file">DAISY</a></li>
<li><a href="$epub.replace('XXX', book.ocaid)" title="Download an ePub">ePub</a></li>
<li><a href="$mobi.replace('XXX', book.ocaid)" title="Download a MOBI file from Internet Archive">MOBI</a></li>
<li>
<a class="plain"
href="$macros.WorldcatUrl(isbn=isbn, oclc_numbers=oclc_numbers)"
title="Look for this edition at WorldCat">Find a Physical Copy</a>
<br /><span class="gray smaller">via WorldCat</span>
</li>
</ul>
</div>
</td>
$else:
<td class="icon read inact">
<div class="zzzz"></div>
<div class="links">
</div>
</td>

$if (isbn or oclc_numbers or 'lendinglibrary' in collection or library) and not private_collection_in(collection):
<td class="icon borrow">
<div class="aaaa"></div>
<div class="links">
<ul>
<li>$:macros.LoanStatus(book, ctx.user)</li>
$if oclc_numbers or isbn:
<li><a href="$macros.WorldcatUrl(isbn=isbn, oclc_numbers=oclc_numbers)"
title="Look for this edition at WorldCat">Find a Physical Copy</a><br /><span class="gray smaller">via WorldCat</span></li>
</ul>
</div>
</td>
$else:
<td class="icon borrow inact">
<div class="zzzz"></div>
<div class="links">
</div>
</td>

</td>

<td class="icon buy inact">
<div class="zzzz"></div>
Expand Down
11 changes: 6 additions & 5 deletions openlibrary/templates/type/work/editions_datatable.html
Expand Up @@ -30,10 +30,10 @@ <h2>
else if (\$(this).hasClass('sorting_desc')) {\$(this).attr("title","Sort by editions to read");}
else {\$(this).attr("title","Available to read");};
});
\$('#editions th.borrow').mouseover(function(){
\$('#editions th.locate').mouseover(function(){
if (\$(this).hasClass('sorting_asc')) {\$(this).attr("title","Are you a member of your local library?");}
else if (\$(this).hasClass('sorting_desc')) {\$(this).attr("title","Bring books you can borrow to the top");}
else {\$(this).attr("title","Available to borrow");};
else if (\$(this).hasClass('sorting_desc')) {\$(this).attr("title","Sory by books likely to be at libraries near you");}
else {\$(this).attr("title","Locate this book");};
});
\$('#editions th.buy').mouseover(function(){
if (\$(this).hasClass('sorting_asc')) {\$(this).attr("title","Books for sale to the bottom");}
Expand Down Expand Up @@ -86,13 +86,14 @@ <h2>
<tr>
<th class="title" title=""><a href="javascript:;">Edition<span></span></a></th>
<th class="read" title=""><a href="javascript:;">Read<span></span></a></th>
<th class="borrow" title=""><a href="javascript:;">Borrow<span></span></a></th>
<th class="locate" title=""><a href="javascript:;">Locate<span></span></a></th>
<th class="buy" title=""><a href="javascript:;">Buy<span></span></a></th>
</tr>
</thead>
<tbody>
$ edition_list_start = time()
$for book in page.get_sorted_editions():
$ editions = page.get_sorted_editions()
$for book in editions:
$ book_keys.append(book['key'].replace('/books/', ''))
<tr>$:render_template("books/edition-sort", book)</tr>
$ edition_list_secs = time() - edition_list_start
Expand Down
23 changes: 7 additions & 16 deletions openlibrary/templates/work_search.html
Expand Up @@ -225,28 +225,19 @@
<div class="resultsContainer">
<div id="searchResults">
<ul id="siteSearch">
$ work_keys = []
$ works = []
$for result_doc in docs:
$ doc = get_doc(result_doc)
$ key = doc.key
$ work_keys.append(key)
$ works.append(doc)

$# filter by availability check
$ ocaids = [work.ia[0] for work in works if work.ia]
$ availability = get_availability_of_ocaids(ocaids)
$ works = add_availability([get_doc(d) for d in docs])

$ loans = ctx.user.get_loans() if ctx.user else []
$ waiting_loans = ctx.user.get_waitinglist() if ctx.user else []
$ user = {'loans': loans, 'waitlists': waiting_loans}

$for work in works:
$ ocaid = work.ia[0] if work.ia else None
$ work_availability = availability.get(ocaid)

$if 'has_fulltext' not in param or work_availability['status'] not in ['error', 'private']:
$:macros.SearchResultsWork(work, availability=work_availability, user=user)
$ availability = work.get('availability', {}).get('status')
$# if we're explicitly showing *everything*...
$# or if we're showing only things with ocaids which are available...
$if 'has_fulltext' not in param or (ocaid and availability and availability not in ['error', 'private']):
$:macros.SearchResultsWork(work, user=user)
</ul>
</div>

Expand Down Expand Up @@ -303,7 +294,7 @@ <h4 class="facetHead">$label</h4>
$if ctx.user and ctx.user.is_admin():
<div id="adminTiming" class="small sansserif clearfix"><br/><span class="adminOnly">Searching solr took $("%.2f" % search_secs) seconds</span></div>
$ readapi_percent = 100
$ wks = '|'.join(work_keys)
$ wks = '|'.join([w.key for w in works])
$ ol = 'openlibrary.org'
<script type="text/javascript">
<!--
Expand Down
11 changes: 11 additions & 0 deletions static/css/master.less
Expand Up @@ -4327,6 +4327,11 @@ select#select-id {
max-width: 300px;
}

.links ul li.read-option a {
display: block;
text-align: center;
}

.borrow-btn,
.read-btn,
.wait-btn,
Expand Down Expand Up @@ -8155,6 +8160,12 @@ p.print-disabled-download {
padding: 5px;
}

.links .print-disabled-download {
font-size: .8em;
text-align: center;
margin-top: 5px;
}

.Tools .print-disabled-download a {
font-size: .8em;
text-decoration: underline;
Expand Down

0 comments on commit 229feb6

Please sign in to comment.