Exemplo n.º 1
0
def item_exists(item_pid):
    """Return True if the Item exists given a PID."""
    try:
        resolve_item_from_loan(item_pid)
    except PersistentIdentifierError:
        return False
    return True
Exemplo n.º 2
0
def item_resolver(loan_pid):
    """Resolve an Item given a Loan PID."""
    Loan = current_circulation.loan_record_cls
    loan = Loan.get_record_by_pid(loan_pid)
    if not loan.get("item_pid"):
        return {}

    try:
        # can resolve to an Item or BorrowingRequest
        item = resolve_item_from_loan(loan["item_pid"])
    except PIDDeletedError:
        item = {}
    else:
        # if it the item is a BorrowingRequest, then some of these
        # fields might not be there
        item = pick(
            item,
            "barcode",
            "description",
            "document_pid",
            "medium",
            "pid",
        )

    return item
Exemplo n.º 3
0
def index_referenced_records(loan):
    """Index referenced records."""
    indexer = ReferencedRecordsIndexer()
    indexed = dict(pid_type=CIRCULATION_LOAN_PID_TYPE, record=loan)
    referenced = []

    # fetch and index the document
    document_cls = current_app_ils.document_record_cls
    document = document_cls.get_record_by_pid(loan["document_pid"])
    referenced.append(dict(pid_type=DOCUMENT_PID_TYPE, record=document))

    # fetch and index the item
    if loan.get("item_pid"):
        item = resolve_item_from_loan(loan["item_pid"])
        referenced.append(dict(pid_type=loan["item_pid"]["type"], record=item))

    # index the loan itself: this is needed because of the extra field
    # `available_items_for_loan_count` added when indexing.
    # To calculate the value of this field, a search on the `loans`
    # indexed is performed and this loan has to be already indexed
    # with its latest data.
    # At the first indexing, `available_items_for_loan_count` value might
    # be wrong and corrected at the second re-indexing.
    loan_class = current_circulation.loan_record_cls
    loan_record = loan_class.get_record_by_pid(loan["pid"])
    referenced.append(
        dict(pid_type=CIRCULATION_LOAN_PID_TYPE, record=loan_record))

    # index the loan and referenced records
    indexer.index(indexed, referenced)
Exemplo n.º 4
0
def index_after_loan_replace_item(_, old_item_pid, new_item_pid):
    """Register Circulation signal to index item."""
    if old_item_pid:
        rec = resolve_item_from_loan(old_item_pid)
        indexer = None
        if old_item_pid["type"] == ITEM_PID_TYPE:
            indexer = current_app_ils.item_indexer
        elif old_item_pid["type"] == BORROWING_REQUEST_PID_TYPE:
            indexer = current_ils_ill.borrowing_request_indexer_cls()
        indexer.index(rec)

    if new_item_pid:
        rec = resolve_item_from_loan(new_item_pid)
        indexer = None
        if new_item_pid["type"] == ITEM_PID_TYPE:
            indexer = current_app_ils.item_indexer
        elif new_item_pid["type"] == BORROWING_REQUEST_PID_TYPE:
            indexer = current_ils_ill.borrowing_request_indexer_cls()
        indexer.index(rec)
Exemplo n.º 5
0
def index_after_loan_replace_item(_, old_item_pid, new_item_pid):
    """Register Circulation signal to index item."""
    loan_index = current_circulation.loan_search_cls.Meta.index
    # When indexing items, the `circulation` field will perform a search on
    # loans index. Wait for the loan index to be refreshed.
    wait_es_refresh(loan_index)

    if old_item_pid:
        rec = resolve_item_from_loan(old_item_pid)
        indexer = None
        if old_item_pid["type"] == ITEM_PID_TYPE:
            indexer = current_app_ils.item_indexer
        elif old_item_pid["type"] == BORROWING_REQUEST_PID_TYPE:
            indexer = current_ils_ill.borrowing_request_indexer_cls()
        indexer.index(rec)

    if new_item_pid:
        rec = resolve_item_from_loan(new_item_pid)
        indexer = None
        if new_item_pid["type"] == ITEM_PID_TYPE:
            indexer = current_app_ils.item_indexer
        elif new_item_pid["type"] == BORROWING_REQUEST_PID_TYPE:
            indexer = current_ils_ill.borrowing_request_indexer_cls()
        indexer.index(rec)
Exemplo n.º 6
0
def index_referenced_records(loan):
    """Index referenced records."""
    indexer = ReferencedRecordsIndexer()
    indexed = dict(pid_type=CIRCULATION_LOAN_PID_TYPE, record=loan)
    referenced = []

    # fetch and index the document
    document_cls = current_app_ils.document_record_cls
    document = document_cls.get_record_by_pid(loan["document_pid"])
    referenced.append(dict(pid_type=DOCUMENT_PID_TYPE, record=document))

    # fetch and index the item
    if loan.get("item_pid"):
        item = resolve_item_from_loan(loan["item_pid"])
        referenced.append(dict(pid_type=loan["item_pid"]["type"], record=item))

    # index the document
    indexer.index(indexed, referenced)
Exemplo n.º 7
0
def get_document_pid_by_item_pid(item_pid):
    """Retrieve the Document PID of the given Item PID."""
    rec = resolve_item_from_loan(item_pid)
    return rec.get("document_pid")