$def with (doc, work_key=None, listen=True, allow_expensive_availability_check=False, secondary_action=False, check_sponsorship=False, sponsorship_help=False, check_loan_status=False, user_lists=None, post='') $# Takes following parameters: $# * doc - Can be a Work, Edition, or solr dict. $# * listen whether to display listen button $# * allow_expensive_availability_check whether to check the groundtruth availability if can't get availability from specified fields $# * secondary_action whether to display the preview/search inside button $# * check_sponsorship whether to check if the book is eligible for sponsorship $# * sponsorship_help whether to show the sponsorship help text $# * check_loan_status whether to check the user's loan status to determine read button $ loanstatus_start_time = time() $ availability = (doc.availability or {}) if hasattr(doc, 'availability') else {} $ ocaid = doc.get('ocaid') or availability.get('identifier') $ work_key = work_key or (doc.get('works') and doc.works[0].key) $ no_index = hasattr(doc, 'get_ia_meta_fields') and doc.get_ia_meta_fields().get('noindex', False) $ waiting_loan_start_time = time() $ waiting_loan = check_loan_status and ocaid and ctx.user and ctx.user.get_waiting_loan_for(ocaid) $ waiting_loan_total_time = time() - waiting_loan_start_time $ my_turn_to_borrow = waiting_loan and waiting_loan['status'] == 'available' and waiting_loan['position'] == 1 $if not waiting_loan: $ book_provider = get_book_provider(doc) $else: $ book_provider = get_book_provider.ia $# Replace existing, possibly inaccurate, availability with results ground truth availability API call: $if allow_expensive_availability_check and ocaid: $ availability.update(get_cached_groundtruth_availability(ocaid)) $ borrow_link = '/borrow/ia/%s' % ocaid $ get_loan_for_start_time = time() $ user_loan = check_loan_status and ocaid and ctx.user and ctx.user.get_loan_for(ocaid) $ get_loan_for_total_time = time() - get_loan_for_start_time $if user_loan: $:macros.ReadButton(ocaid, loan=user_loan, listen=listen) $ return_url = doc.url().rsplit('/', 1)[0] + '/do_return/borrow'
$if render_once('LoanStatus:return-book-js'): $elif book_provider and book_provider.short_name != 'ia': $:book_provider.render_read_button(doc) $elif availability.get('is_readable'): $:macros.ReadButton(ocaid, listen=listen) $if secondary_action: $:macros.BookSearchInside(ocaid) $elif ocaid and ctx.user and ctx.user.is_printdisabled(): $ std_borrow = availability and availability.get('is_lendable') $:macros.ReadButton(ocaid, borrow=std_borrow, printdisabled=not std_borrow, listen=listen) $elif availability.get('is_lendable'): $if availability.get("available_to_borrow") or availability.get("available_to_browse"): $:macros.ReadButton(ocaid, borrow=True, listen=listen) $elif availability.get('available_to_waitlist'): $if waiting_loan: $else: $# "JOIN-WAITLIST" button $ wlsize = availability.get('users_on_waitlist') or availability.get('num_waitlist') $else: $# Checked out $if secondary_action: $:macros.BookPreview(ocaid, linkback=not no_index) $elif ('eligibility' in doc) or (check_sponsorship and not ocaid and not availability.get('is_lendable')): $ sponsorship = doc.get('eligibility') if 'eligibility' in doc else qualifies_for_sponsorship(doc, donate_only=True) $if sponsorship.get('is_eligible'): $_('Donate Book') $if sponsorship_help:$_("We don't have this book yet. Can you donate it to the Lending Library?") $_('Learn More')
$elif ocaid and availability.get('is_previewable') and book_provider.short_name == 'ia': $:macros.BookPreview(ocaid, linkback=not no_index) $if secondary_action: $:macros.BookSearchInside(ocaid) $else: $:post $if query_param('debug'): $ loanstatus_end_time = time() - loanstatus_start_timeget_waiting_loan_for(ocaid) took $("%.2f" % waiting_loan_total_time) seconds
get_loan_for(ocaid) took $("%.2f" % get_loan_for_total_time) seconds
LoanStatus took $("%.2f" % loanstatus_end_time) seconds
$availability