def circulation_resolver(document_pid): """Return circulation info for the given Document.""" # loans past_loans_count = get_past_loans_by_doc_pid(document_pid).count() active_loans_count = get_active_loans_by_doc_pid(document_pid).count() pending_loans_count = get_pending_loans_by_doc_pid( document_pid ).count() overdue_loans_count = get_overdue_loans_by_doc_pid( document_pid ).count() # items item_search = current_app_ils.item_search_cls() items_count = item_search.search_by_document_pid(document_pid).count() unavailable_items_count = item_search.get_unavailable_items_by_document_pid( document_pid ).count() has_items_for_loan = ( items_count - active_loans_count - unavailable_items_count ) has_items_for_reference_only_count = item_search.get_for_reference_only_by_document_pid( document_pid ).count() circulation = { "active_loans": active_loans_count, "can_circulate_items_count": items_count - unavailable_items_count, "has_items_for_loan": has_items_for_loan, "overbooked": pending_loans_count > has_items_for_loan, "overdue_loans": overdue_loans_count, "past_loans_count": past_loans_count, "pending_loans": pending_loans_count, "has_items_on_site": has_items_for_reference_only_count, } if ( circulation["overbooked"] or circulation["active_loans"] >= circulation["has_items_for_loan"] ): next_available_loans = ( get_loan_next_available_date(document_pid).execute().hits ) total = ( next_available_loans.total if lt_es7 else next_available_loans.total.value ) if total > 0: next_date = next_available_loans[0].end_date circulation["next_available_date"] = next_date return circulation
def items_resolver(document_pid): """Search and return the total number of items.""" items = [] by_location = {} item_search = current_app_ils.item_search_cls() for hit in item_search.search_by_document_pid(document_pid).scan(): item = hit.to_dict() circulation = item.get("circulation", {}) obj = { "pid": item.get("pid"), "isbn": item.get("isbn"), "internal_location_pid": item.get("internal_location_pid"), "circulation_restriction": item.get("circulation_restriction"), "barcode": item.get("barcode"), "medium": item.get("medium"), "status": item.get("status"), "description": item.get("description"), "shelf": item.get("shelf"), "internal_location": { "name": item.get("internal_location", {}).get("name", ""), "location": { "name": item.get("internal_location", {}).get("location", {}).get("name", "") }, }, } if circulation: include_circulation_keys = ["state"] obj["circulation"] = {} for key in include_circulation_keys: obj["circulation"][key] = circulation.get(key) items.append(obj) # grouping by location (can circulate and not on loan) location_name = (item.get("internal_location", {}).get("location", {}).get("name", "")) internal_location_name = item.get("internal_location", {}).get("name", "") if location_name not in by_location: by_location[location_name] = { internal_location_name: [], "total": 0, } if internal_location_name not in by_location[location_name]: by_location[location_name][internal_location_name] = [] del obj["description"] by_location[location_name][internal_location_name].append(obj) by_location[location_name]["total"] += 1 return {"total": len(items), "hits": items, "on_shelf": by_location}
def delete(self, **kwargs): """Delete Location record.""" item_search = current_app_ils.item_search_cls() item_search_res = item_search.search_by_internal_location_pid( internal_location_pid=self["pid"]) if item_search_res.count(): raise RecordHasReferencesError( record_type="Internal Location", record_id=self["pid"], ref_type="Item", ref_ids=sorted([res["pid"] for res in item_search_res.scan()]), ) return super().delete(**kwargs)
def stock_resolver(document_pid): """Search and return the mediums of the document.""" item_search = current_app_ils.item_search_cls() eitem_search = current_app_ils.eitem_search_cls() search = item_search.search_by_document_pid(document_pid) search.aggs.bucket("mediums", "terms", field="medium") search_response = search.execute() mediums = [ bucket.key for bucket in search_response.aggregations.mediums.buckets ] eitems_count = eitem_search.search_by_document_pid( document_pid).count() if eitems_count > 0: mediums.append("E-BOOK") return { "mediums": mediums, }
def circulation_resolver(document_pid): """Return circulation info for the given Document.""" # loans loan_search = current_circulation.loan_search_cls() past_loans_count = loan_search.get_past_loans_by_doc_pid( document_pid).count() active_loans_count = loan_search.get_active_loans_by_doc_pid( document_pid).count() pending_loans_count = loan_search.get_pending_loans_by_doc_pid( document_pid).count() overdue_loans_count = loan_search.get_overdue_loans_by_doc_pid( document_pid).count() # items ItemSearch = current_app_ils.item_search_cls() items_count = ItemSearch.search_by_document_pid(document_pid).count() unavailable_items_count = ItemSearch. \ get_unavailable_items_by_document_pid(document_pid).count() has_items_for_loan = items_count - \ active_loans_count - unavailable_items_count has_items_for_reference_only_count = ItemSearch \ .get_for_reference_only_by_document_pid(document_pid).count() circulation = { "active_loans": active_loans_count, "can_circulate_items_count": items_count - unavailable_items_count, "has_items_for_loan": has_items_for_loan, "overbooked": pending_loans_count > has_items_for_loan, "overdue_loans": overdue_loans_count, "past_loans_count": past_loans_count, "pending_loans": pending_loans_count, "has_items_on_site": has_items_for_reference_only_count } if circulation["overbooked"] or circulation["active_loans"] >= \ circulation["has_items_for_loan"]: next_available_loans = loan_search.get_loan_next_available_date( document_pid).execute() if next_available_loans: circulation["next_available_date"] = \ next_available_loans.hits[0].end_date return circulation
def get_item_by_barcode(barcode, raise_exception=True): """Retrieve item object by barcode.""" search = current_app_ils.item_search_cls().query( "bool", filter=[ Q("term", barcode=barcode), ], ) result = search.execute() hits_total = result.hits.total.value if not result.hits or hits_total < 1: click.secho("no item found with barcode {}".format(barcode), fg="red") if raise_exception: raise ItemMigrationError( "no item found with barcode {}".format(barcode)) elif hits_total > 1: raise ItemMigrationError( "found more than one item with barcode {}".format(barcode)) else: return current_app_ils.item_record_cls.get_record_by_pid( result.hits[0].pid)
def delete(self, **kwargs): """Delete Document record.""" loan_search_res = search_by_pid( document_pid=self["pid"], filter_states=["PENDING"] + current_app.config["CIRCULATION_STATES_LOAN_ACTIVE"], ) if loan_search_res.count(): raise RecordHasReferencesError( record_type="Document", record_id=self["pid"], ref_type="Loan", ref_ids=sorted([res["pid"] for res in loan_search_res.scan()]), ) item_search = current_app_ils.item_search_cls() item_search_res = item_search.search_by_document_pid( document_pid=self["pid"]) if item_search_res.count(): raise RecordHasReferencesError( record_type="Document", record_id=self["pid"], ref_type="Item", ref_ids=sorted([res["pid"] for res in item_search_res.scan()]), ) req_search = current_app_ils.document_request_search_cls() req_search_res = req_search.search_by_document_pid( document_pid=self["pid"]) if req_search_res.count(): raise RecordHasReferencesError( record_type="Document", record_id=self["pid"], ref_type="DocumentRequest", ref_ids=sorted([res["pid"] for res in req_search_res.scan()]), ) return super().delete(**kwargs)
def delete(self, **kwargs): """Delete Document record.""" loan_search_res = search_by_pid( document_pid=self["pid"], filter_states=["PENDING"] + current_app.config["CIRCULATION_STATES_LOAN_ACTIVE"], ) if loan_search_res.count(): raise RecordHasReferencesError( record_type="Document", record_id=self["pid"], ref_type="Loan", ref_ids=sorted([res["pid"] for res in loan_search_res.scan()]), ) item_search = current_app_ils.item_search_cls() item_search_res = item_search.search_by_document_pid( document_pid=self["pid"]) if item_search_res.count(): raise RecordHasReferencesError( record_type="Document", record_id=self["pid"], ref_type="Item", ref_ids=sorted([res["pid"] for res in item_search_res.scan()]), ) eitem_search = current_app_ils.eitem_search_cls() eitem_search_res = eitem_search.search_by_document_pid( document_pid=self["pid"]) if eitem_search_res.count(): raise RecordHasReferencesError( record_type="Document", record_id=self["pid"], ref_type="EItem", ref_ids=sorted([res["pid"] for res in eitem_search_res.scan()]), ) req_search = current_app_ils.document_request_search_cls() req_search_res = req_search.search_by_document_pid( document_pid=self["pid"]) if req_search_res.count(): raise RecordHasReferencesError( record_type="Document", record_id=self["pid"], ref_type="DocumentRequest", ref_ids=sorted([res["pid"] for res in req_search_res.scan()]), ) order_search = current_ils_acq.order_search_cls() orders_refs_search = order_search.search_by_document_pid( document_pid=self["pid"], ) if orders_refs_search.count(): raise RecordHasReferencesError( record_type="Document", record_id=self["pid"], ref_type="AcquisitionOrder", ref_ids=sorted( [res["pid"] for res in orders_refs_search.scan()]), ) brw_req_search = current_ils_ill.borrowing_request_search_cls() brw_req_refs_search = brw_req_search.search_by_document_pid( document_pid=self["pid"], ) if brw_req_refs_search.count(): raise RecordHasReferencesError( record_type="Document", record_id=self["pid"], ref_type="BorrowingRequest", ref_ids=sorted( [res["pid"] for res in brw_req_refs_search.scan()]), ) return super().delete(**kwargs)