def validate_physical_item_provider(self, physical_item_provider): """Validate physical_item_provider existence.""" if physical_item_provider: pid = physical_item_provider.get("pid", None) pid_type = physical_item_provider.get("pid_type", None) if not pid or not pid_type: raise DocumentRequestError( "The physical item provider must have both pid_type and " "pid fields." ) if pid_type == ORDER_PID_TYPE: Provider = current_ils_acq.order_record_cls elif pid_type == BORROWING_REQUEST_PID_TYPE: Provider = current_ils_ill.borrowing_request_record_cls else: raise DocumentRequestError( "Unknown pid_type {}".format(pid_type) ) try: Provider.get_record_by_pid(pid) except PIDDoesNotExistError: raise DocumentRequestError( "Record with PID {0}:{1} doesn't exist".format( pid_type, pid ) )
def validate_rejection(self, document_pid, state, reject_reason): """Validate rejection is correct.""" if state == "REJECTED" and not reject_reason: raise DocumentRequestError( "Need to provide a reason when rejecting a request") if (state == "REJECTED" and reject_reason == "IN_CATALOG" and not document_pid): raise DocumentRequestError( "Document Request cannot be Rejected with reason IN_CATALOG " "without providing a document_pid.")
def validate_decline(self, document_pid, state, decline_reason): """Validate decline is correct.""" if state == "DECLINED" and not decline_reason: raise DocumentRequestError( "You have to provide a reason when declining a request" ) if ( state == "DECLINING" and decline_reason == "IN_CATALOG" and not document_pid ): raise DocumentRequestError( "Document Request cannot be declined with reason IN_CATALOG " "without providing a document_pid." )
def validate_acceptance(self, document_pid, physical_item_provider, state): """Validate rejection is correct.""" if state == "ACCEPTED" and (not document_pid or not physical_item_provider): raise DocumentRequestError( "Need to provide a document_pid and a physical_item_provider " "when accepting a request")
def validate_document_pid(self, document_pid, state, reject_reason): """Validate data for accepted state.""" # Requests must have a document and a provider (ILL, ACQ) if document_pid: try: search = DocumentRequestSearch() search.search_by_document_pid(document_pid) except PIDDoesNotExistError: # Missing document_pid raise DocumentRequestError( "State cannot be ACCEPTED because a document with " "PID {} doesn't exist".format(document_pid)) if state == "ACCEPTED" and not document_pid: raise DocumentRequestError( "State cannot be ACCEPTED without a document")
def post(self, pid, record, **kwargs): """Reject request post method.""" data = self.loader() reject_reason = data.get("reject_reason") document_pid = data.get("document_pid") if record["state"] != "PENDING": raise DocumentRequestError( "You cannot cancel a Document Request that " "is in state: {}".format(record["state"])) if not reject_reason: raise DocumentRequestError( "Missing required field: reject reason", errors=[ FieldError( field="reject_reason", message="Reject reason is required.", ) ], ) if reject_reason == "IN_CATALOG" and not document_pid: raise DocumentRequestError( "Document PID required for reject reason {}".format( reject_reason), errors=[ FieldError( field="document_pid", message="DocumentPID is required.", ) ], ) if reject_reason == "IN_CATALOG": record["document_pid"] = document_pid record["state"] = "REJECTED" record["reject_reason"] = reject_reason record.commit() db.session.commit() current_app_ils.document_request_indexer.index(record) send_document_request_mail(record, action="request_rejected") return self.make_response(pid, record, 202)
def post(self, pid, record, **kwargs): """Add or replace Provider in Document Request.""" data = self.loader() if record["state"] != "PENDING": raise DocumentRequestError( "You cannot add Provider when the Document Request " "is in state: {}".format(record["state"])) physical_item_provider = data.get("physical_item_provider") if not physical_item_provider: raise DocumentRequestError("physical_item_provider is required") record["physical_item_provider"] = physical_item_provider record.commit() db.session.commit() current_app_ils.document_request_indexer.index(record) return self.make_response(pid, record, 202)
def validate_document_pid(self, document_pid): """Validate document existence.""" if document_pid: try: Document = current_app_ils.document_record_cls Document.get_record_by_pid(document_pid) except PIDDoesNotExistError: raise DocumentRequestError( "The document with PID {} doesn't exist".format( document_pid ) )
def post(self, pid, record, **kwargs): """Add or replace a Document in Document Request.""" if record["state"] != "PENDING": raise DocumentRequestError( "You cannot add Document when the Document Request " "is in state: {}".format(record["state"])) data = self.loader() record["document_pid"] = data.get("document_pid") record.commit() db.session.commit() current_app_ils.document_request_indexer.index(record) return self.make_response(pid, record, 202)
def post(self, pid, record, **kwargs): """Accept request post method.""" if record["state"] != "PENDING": raise DocumentRequestError( "You cannot change state to 'ACCEPTED' when the Document " "Request is in state: {}".format(record["state"])) if "document_pid" not in record: raise DocumentRequestError( "document_pid is required for the Document Request to be " "accepted.") if "physical_item_provider" not in record: raise DocumentRequestError( "physical_item_provider is required for the Document Request " "to be accepted.") record["state"] = 'ACCEPTED' record.commit() db.session.commit() current_app_ils.document_request_indexer.index(record) return self.make_response(pid, record, 202)
def delete(self, pid, record, **kwargs): """Remove Provider from Document Request.""" if record["state"] != "PENDING": raise DocumentRequestError( "You cannot remove the Provider when the Document Request " "is in state: {}".format(record["state"])) if "physical_item_provider" in record: del record["physical_item_provider"] record.commit() db.session.commit() current_app_ils.document_request_indexer.index(record) return self.make_response(pid, record, 202)
def delete(self, pid, record, *kwargs): """Remove Document from Document Request.""" self.loader() if record["state"] != "PENDING": raise DocumentRequestError( "You cannot remove the Document when the Document Request " "is in state: {}".format(record["state"])) if "document_pid" in record: del record["document_pid"] record.commit() db.session.commit() current_app_ils.document_request_indexer.index(record) return self.make_response(pid, record, 202)
def document_request_resolver(document_pid): """Return the DocumentRequest for the given Document or raise.""" search = DocumentRequestSearch().search_by_document_pid(document_pid) results = search.execute() total = results.hits.total if lt_es7 else results.hits.total.value if total < 1: return {} elif total > 1: raise DocumentRequestError( "Found multiple requests associated with document {}".format( document_pid)) else: hit = results.hits[0].to_dict() return { "pid": hit.get("pid", None), "title": hit.get("title", None), "authors": hit.get("authors", None), "note": hit.get("note", None), "state": hit.get("state", None), "patron_pid": hit.get("patron_pid", None), }
def validate_state(self, state, valid_states): """Validate state.""" if state not in valid_states: raise DocumentRequestError("Invalid state: {}".format(state))