def create_loan(params, should_force_checkout): """Create a loan on behalf of a user.""" if "patron_pid" not in params: raise MissingRequiredParameterError( description="'patron_pid' is required when creating a loan") if "item_pid" not in params: raise MissingRequiredParameterError( description="'item_pid' is required when creating a loan") if patron_has_active_loan_on_item(patron_pid=params["patron_pid"], item_pid=params["item_pid"]): raise PatronHasLoanOnItemError(params["patron_pid"], params["item_pid"]) if "document_pid" not in params: document_pid = Item.get_document_pid(params["item_pid"]) if document_pid: params["document_pid"] = document_pid if should_force_checkout: _ensure_item_can_circulate(params['item_pid']) # create a new loan record_uuid = uuid.uuid4() new_loan = {"item_pid": params["item_pid"]} pid = loan_pid_minter(record_uuid, data=new_loan) loan = Loan.create(data=new_loan, id_=record_uuid) # trigger the first transition loan = current_circulation.circulation.trigger( loan, **dict(params, trigger="checkout")) return pid, loan
def request_loan(params): """Create a loan and trigger the first transition to create a request.""" if "patron_pid" not in params: raise MissingRequiredParameterError( description="'patron_pid' is required on loan request") if "document_pid" not in params: raise MissingRequiredParameterError( description="'document_pid' is required on loan request") if patron_has_active_loan_on_item(patron_pid=params["patron_pid"], document_pid=params["document_pid"]): raise PatronHasLoanOnDocumentError(params["patron_pid"], params["document_pid"]) # create a new loan record_uuid = uuid.uuid4() new_loan = { "document_pid": params["document_pid"], "patron_pid": params["patron_pid"], } pid = loan_pid_minter(record_uuid, data=new_loan) loan = Loan.create(data=new_loan, id_=record_uuid) # trigger the first transition loan = current_circulation.circulation.trigger( loan, **dict(params, trigger="request")) return pid, loan
def search_by_internal_location_pid( self, internal_location_pid=None, filter_states=None, exclude_states=None ): """Retrieve items based on the given internal location pid.""" search = self if internal_location_pid: search = search.filter( "term", internal_location_pid=internal_location_pid ) else: raise MissingRequiredParameterError( description="internal_location_pid is required" ) if filter_states: search = search.filter("terms", status=filter_states) elif exclude_states: search = search.exclude("terms", status=exclude_states) return search
def _validate_delivery(delivery): """Validate `delivery` param.""" methods = list( current_app.config.get("ILS_CIRCULATION_DELIVERY_METHODS", {}).keys()) if methods: if not delivery or delivery["method"] not in methods: raise MissingRequiredParameterError( description="A valid 'delivery' is required on loan request")
def search_by_provider_pid(self, provider_pid=None): """Search by provider pid.""" search = self if provider_pid: search = search.filter("term", provider_pid=provider_pid) else: raise MissingRequiredParameterError( description="provider_pid is required") return search
def get_ongoing_orders_by_patron_pid(self, patron_pid=None): """Get pending and ordered orders by patron pid.""" active_states = ["PENDING", "ORDERED"] if patron_pid: search = self.search_by_patron_pid(patron_pid).filter( "terms", status=active_states) else: raise MissingRequiredParameterError( description="patron_pid is required") return search
def search_by_patron_pid(self, patron_pid=None): """Search by patron pid.""" search = self if patron_pid: search = search.filter("term", order_lines__patron_pid=patron_pid) else: raise MissingRequiredParameterError( description="patron_pid is required") return search
def search_by_document_pid(self, document_pid=None): """Search by document pid.""" search = self if document_pid: search = search.filter("term", document_pid=document_pid) else: raise MissingRequiredParameterError( description="document_pid is required") return search
def search_by_library_pid(self, library_pid=None): """Retrieve BorrowingRequests with the given library_pid.""" search = self if library_pid: search = search.filter("term", library_pid=library_pid) else: raise MissingRequiredParameterError( description="library_pid is required") return search
def search_by_vendor_pid(self, vendor_pid=None): """Search by vendor pid.""" search = self if vendor_pid: search = search.filter("term", vendor_pid=vendor_pid) else: raise MissingRequiredParameterError( description="vendor_pid is required" ) return search
def search_by_document_request_pid(self, document_request_pid=None): """Retrieve document requests with the given document_request_pid.""" search = self if document_request_pid: search = search.filter("term", request__pid=document_request_pid) else: raise MissingRequiredParameterError( description="document_request_pid is required" ) return search
def search_by_series_pid(self, series_pid=None): """Retrieve documents with the given series_pid.""" search = self if series_pid: search = search.filter("term", series__pid=series_pid) else: raise MissingRequiredParameterError( description="series_pid is required" ) return search
def filter_by_tag_pid(cls, search, tag_pid, filter_states=None, exclude_states=None): """Filter a search by tag PID.""" if tag_pid: search = search.filter("term", tag_pids=tag_pid) else: raise MissingRequiredParameterError( description="tag_pid is required" ) if filter_states: search = search.filter("terms", state=filter_states) elif exclude_states: search = search.exclude("terms", state=exclude_states) return search
def search_by_document_pid(self, document_pid=None, filter_states=None, exclude_states=None): """Retrieve items based on the given document pid.""" search = self if document_pid: search = search.filter("term", document_pid=document_pid) else: raise MissingRequiredParameterError( description="document_pid is required" ) if filter_states: search = search.filter("terms", status=filter_states) elif exclude_states: search = search.exclude("terms", status=exclude_states) return search
def send_email_after_loan_change(_, prev_loan, loan, trigger): """Send email notification when the loan changes.""" _datastore = current_app.extensions["security"].datastore patron_pid = loan["patron_pid"] patron = _datastore.get_user(patron_pid) if not patron: raise PatronNotFoundError(patron_pid) if not patron.email: msg = "Patron with PID {} has no email address".format(patron_pid) raise MissingRequiredParameterError(description=msg) send_ils_mail(loan_message_factory(), prev_loan, loan, trigger, recipients=[patron.email])
def search_by_bucket_id(self, bucket_id=None): """Search EItems by bucket id.""" search = self if bucket_id: search = search.filter("term", bucket_id=bucket_id) else: raise MissingRequiredParameterError( description="bucket_id is required") results = search.execute() if len(results) != 1: # There should always be one bucket associated with an eitem when # downloading a file. msg = "found 0 or multiple records with bucket {0}".format( bucket_id) current_app.logger.warning(msg) return results