def loan_created(app): """Minimal Loan object.""" record_uuid = uuid.uuid4() new_loan = {} loan_pid_minter(record_uuid, data=new_loan) loan = Loan.create(data=new_loan, id_=record_uuid) db.session.commit() yield loan
def loan_created(app): """Minimal Loan object.""" record_uuid = uuid.uuid4() new_loan = {} loan_pid_minter(record_uuid, data=new_loan) loan = Loan.create(data=new_loan, id_=record_uuid) db.session.commit() yield loan
def create_loan(params): """Create a loan for behalf of a user.""" if "patron_pid" not in params or "item_pid" not in params: raise CirculationException( "Patron or item not defined on loan request.") if patron_has_active_loan_on_item(patron_pid=params["patron_pid"], item_pid=params["item_pid"]): raise CirculationException( "Patron has already a request or active loan on this item.") if "document_pid" not in params: document_pid = circulation_document_retriever(params["item_pid"]) if document_pid: params["document_pid"] = document_pid # create a new loan record_uuid = uuid.uuid4() new_loan = {} 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 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 request_loan(document_pid, patron_pid, transaction_location_pid, transaction_user_pid, **kwargs): """Create a new loan and trigger the first transition to PENDING.""" if patron_has_request_on_document(patron_pid=patron_pid, document_pid=document_pid): raise PatronHasRequestOnDocumentError(patron_pid, document_pid) _validate_delivery(kwargs.get("delivery")) # create a new loan record_uuid = uuid.uuid4() new_loan = dict( patron_pid=patron_pid, transaction_location_pid=transaction_location_pid, transaction_user_pid=transaction_user_pid, ) pid = loan_pid_minter(record_uuid, data=new_loan) loan = Loan.create(data=new_loan, id_=record_uuid) params = deepcopy(loan) params.update(document_pid=document_pid, **kwargs) # trigger the transition to request loan = current_circulation.circulation.trigger( loan, **dict(params, trigger="request")) return pid, loan
def checkout_loan(item_pid, patron_pid, transaction_location_pid, transaction_user_pid, force=False, **kwargs): """Create a new loan and trigger the first transition to ITEM_ON_LOAN.""" if patron_has_active_loan_on_item(patron_pid=patron_pid, item_pid=item_pid): raise PatronHasLoanOnItemError(patron_pid, item_pid) optional_delivery = kwargs.get("delivery") if optional_delivery: _validate_delivery(optional_delivery) if force: _set_item_to_can_circulate(item_pid) # create a new loan record_uuid = uuid.uuid4() new_loan = dict( patron_pid=patron_pid, transaction_location_pid=transaction_location_pid, transaction_user_pid=transaction_user_pid, ) pid = loan_pid_minter(record_uuid, data=new_loan) loan = Loan.create(data=new_loan, id_=record_uuid) params = deepcopy(loan) params.update(item_pid=item_pid, **kwargs) # trigger the transition to request loan = current_circulation.circulation.trigger( loan, **dict(params, trigger="checkout")) return pid, loan
def loans(): """Load test data fixture.""" record_uuid = uuid.uuid4() new_loan = {} pid = loan_pid_minter(record_uuid, data=new_loan) Loan.create(data=new_loan, id_=record_uuid) db.session.commit() click.secho("Loan #{} created.".format(pid.pid_value), fg="green")
def loans(): """Load test data fixture.""" record_uuid = uuid.uuid4() new_loan = {} pid = loan_pid_minter(record_uuid, data=new_loan) Loan.create(data=new_loan, id_=record_uuid) db.session.commit() click.secho("Loan #{} created.".format(pid.pid_value), fg="green")
def checkout_loan(item_pid, patron_pid, transaction_location_pid, transaction_user_pid=None, force=False, **kwargs): """Create a new loan and trigger the first transition to ITEM_ON_LOAN. :param item_pid: a dict containing `value` and `type` fields to uniquely identify the item. :param patron_pid: the PID value of the patron :param transaction_location_pid: the PID value of the location where the checkout is performed :param transaction_user_pid: the PID value of the user that performed the checkout :param force: if True, ignore the current status of the item and do perform the checkout. If False, the checkout will fail when the item cannot circulate. """ if patron_has_active_loan_on_item(patron_pid=patron_pid, item_pid=item_pid): raise PatronHasLoanOnItemError(patron_pid, item_pid) optional_delivery = kwargs.get("delivery") if optional_delivery: _validate_delivery(optional_delivery) if force: _set_item_to_can_circulate(item_pid) transaction_user_pid = transaction_user_pid or str(current_user.id) # create a new loan record_uuid = uuid.uuid4() new_loan = dict( patron_pid=patron_pid, transaction_location_pid=transaction_location_pid, transaction_user_pid=transaction_user_pid, ) pid = loan_pid_minter(record_uuid, data=new_loan) loan = Loan.create(data=new_loan, id_=record_uuid) params = deepcopy(loan) params.update(item_pid=item_pid, **kwargs) # trigger the transition to request loan = current_circulation.circulation.trigger( loan, **dict(params, trigger="checkout")) return pid, loan
def request_loan(document_pid, patron_pid, transaction_location_pid, transaction_user_pid=None, **kwargs): """Create a new loan and trigger the first transition to PENDING.""" search_result, loan_found = patron_has_active_loan_or_request_on_document( patron_pid, document_pid) if loan_found: if (search_result.hits[0].state in current_app.config["CIRCULATION_STATES_LOAN_REQUEST"]): raise PatronHasRequestOnDocumentError(patron_pid, document_pid) raise PatronHasLoanOnDocumentError(patron_pid, document_pid) _validate_delivery(kwargs.get("delivery")) transaction_user_pid = transaction_user_pid or str(current_user.id) # create a new loan record_uuid = uuid.uuid4() new_loan = dict( patron_pid=patron_pid, transaction_location_pid=transaction_location_pid, transaction_user_pid=transaction_user_pid, ) pid = loan_pid_minter(record_uuid, data=new_loan) loan = Loan.create(data=new_loan, id_=record_uuid) params = deepcopy(loan) params.update(document_pid=document_pid, **kwargs) # trigger the transition to request loan = current_circulation.circulation.trigger( loan, **dict(params, trigger="request")) return pid, loan