Пример #1
0
def get_borrow_status(itemid,
                      include_resources=True,
                      include_ia=True,
                      edition=None):
    """Returns borrow status for each of the sources and formats.

    If the optinal argument editions is provided, it uses that edition instead
    of finding edition from itemid. This is added for performance reasons.
    """
    loan = lending.get_loan(itemid)
    has_loan = bool(loan)

    if edition:
        editions = [edition]
    else:
        edition_keys = web.ctx.site.things({
            "type": "/type/edition",
            "ocaid": itemid
        })
        editions = web.ctx.site.get_many(edition_keys)
    has_waitinglist = editions and any(e.get_waitinglist_size() > 0
                                       for e in editions)

    d = {
        'identifier': itemid,
        'checkedout': has_loan or has_waitinglist,
        'has_loan': has_loan,
        'has_waitinglist': has_waitinglist,
    }
    if include_ia:
        ia_checkedout = lending.is_loaned_out_on_ia(itemid)
        d['checkedout'] = d['checkedout'] or ia_checkedout
        d['checkedout_on_ia'] = ia_checkedout

    if include_resources:
        d.update({
            'resource_bookreader': 'absent',
            'resource_pdf': 'absent',
            'resource_epub': 'absent',
        })
        if editions:
            resources = editions[0].get_lending_resources()
            resource_pattern = r'acs:(\w+):(.*)'
            for resource_urn in resources:
                if resource_urn.startswith('acs:'):
                    (resource_type,
                     resource_id) = re.match(resource_pattern,
                                             resource_urn).groups()
                else:
                    resource_type, resource_id = "bookreader", resource_urn
                resource_type = "resource_" + resource_type
                if is_loaned_out(resource_id):
                    d[resource_type] = 'checkedout'
                else:
                    d[resource_type] = 'available'
    return web.storage(d)
Пример #2
0
def get_borrow_status(itemid, include_resources=True, include_ia=True, edition=None):
    """Returns borrow status for each of the sources and formats.

    If the optinal argument editions is provided, it uses that edition instead
    of finding edition from itemid. This is added for performance reasons.
    """
    loan = lending.get_loan(itemid)
    has_loan = bool(loan)

    if edition:
        editions = [edition]
    else:
        edition_keys = web.ctx.site.things({"type": "/type/edition", "ocaid": itemid})
        editions = web.ctx.site.get_many(edition_keys)
    has_waitinglist = editions and any(e.get_waitinglist_size() > 0 for e in editions)

    d = {
        'identifier': itemid,
        'checkedout': has_loan or has_waitinglist,
        'has_loan': has_loan,
        'has_waitinglist': has_waitinglist,
    }
    if include_ia:
        ia_checkedout = lending.is_loaned_out_on_ia(itemid)
        d['checkedout'] = d['checkedout'] or ia_checkedout
        d['checkedout_on_ia'] = ia_checkedout

    if include_resources:
        d.update({
            'resource_bookreader': 'absent',
            'resource_pdf': 'absent',
            'resource_epub': 'absent',
        })
        if editions:
            resources = editions[0].get_lending_resources()
            resource_pattern = r'acs:(\w+):(.*)'
            for resource_urn in resources:
                if resource_urn.startswith('acs:'):
                    (resource_type, resource_id) = re.match(resource_pattern, resource_urn).groups()
                else:
                    resource_type, resource_id = "bookreader", resource_urn
                resource_type = "resource_" + resource_type
                if is_loaned_out(resource_id):
                    d[resource_type] = 'checkedout'
                else:
                    d[resource_type] = 'available'
    return web.storage(d)
Пример #3
0
def is_loaned_out(resource_id):
    # bookreader loan status is stored in the private data store

    # Check our local status
    loan_key = get_loan_key(resource_id)
    if not loan_key:
        # No loan recorded
        identifier = resource_id[len('bookreader:'):]
        return lending.is_loaned_out_on_ia(identifier)

    # Find the loan and check if it has expired
    loan = web.ctx.site.store.get(loan_key)
    if loan:
        if datetime_from_isoformat(loan['expiry']) < datetime.datetime.utcnow():
            return True

    return False
Пример #4
0
def is_loaned_out(resource_id):
    # bookreader loan status is stored in the private data store

    # Check our local status
    loan_key = get_loan_key(resource_id)
    if not loan_key:
        # No loan recorded
        identifier = resource_id[len('bookreader:'):]
        return lending.is_loaned_out_on_ia(identifier)

    # Find the loan and check if it has expired
    loan = web.ctx.site.store.get(loan_key)
    if loan:
        if datetime_from_isoformat(loan['expiry']) < datetime.datetime.utcnow():
            return True

    return False