def get_internal_location_by_legacy_recid(legacy_recid): """Search for internal location by legacy id.""" search = InternalLocationSearch().query( "bool", filter=[Q("term", legacy_ids=legacy_recid)] ) result = search.execute() hits_total = result.hits.total.value if not result.hits or hits_total < 1: click.secho( "no internal location found with legacy id {}".format( legacy_recid ), fg="red", ) raise ItemMigrationError( "no internal location found with legacy id {}".format(legacy_recid) ) elif hits_total > 1: raise ItemMigrationError( "found more than one internal location with legacy id {}".format( legacy_recid ) ) else: return InternalLocation.get_record_by_pid(result.hits[0].pid)
def clean_circulation_restriction(record): """Translates circulation restrictions of the item.""" record["circulation_restriction"] = "NO_RESTRICTION" intloc_record_cls = current_app_ils.internal_location_record_cls intloc = intloc_record_cls.get_record_by_pid( record["internal_location_pid"] ) if "loan_period" in record: options = {"1 week": "ONE_WEEK", "4 weeks": "NO_RESTRICTION"} # Only update status to "FOR_REFERENCE_ONLY" if item belongs to # LSL library (legacy_id = 9) and if item is not missing. if "9" in intloc["legacy_ids"] and record["status"] != "MISSING" and\ record["loan_period"].lower() == "reference": record["status"] = "FOR_REFERENCE_ONLY" elif record["loan_period"] == "" or \ record["loan_period"].lower() == "reference": record["circulation_restriction"] = "NO_RESTRICTION" else: try: record["circulation_restriction"] = options[ record["loan_period"] ] except KeyError: raise ItemMigrationError( "Unknown circulation restriction (loan period) on " "barcode {0}: {1}".format( record["barcode"], record["loan_period"] ) ) del record["loan_period"]
def get_item_by_barcode(barcode): """Retrieve item object by barcode.""" search = ItemSearch().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") 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 Item.get_record_by_pid(result.hits[0].pid)
def clean_item_status(record): """Translates item statuses.""" # possible values: # on shelf, missing, on loan, in binding, on order, # out of print, not published, not arrived, under review options = { "on shelf": "CAN_CIRCULATE", "missing": "MISSING", "on loan": "CAN_CIRCULATE", "in binding": "IN_BINDING", } try: record["status"] = options[record["status"]] except KeyError: raise ItemMigrationError( "Unknown item status {0} on barcode {1}".format( record["status"], record["barcode"]))
def clean_circulation_restriction(record): """Translates circulation restrictions of the item.""" record["circulation_restriction"] = "NO_RESTRICTION" if "loan_period" in record: options = {"1 week": "ONE_WEEK", "4 weeks": "NO_RESTRICTION"} if record["loan_period"].lower() == "reference": record["status"] = "FOR_REFERENCE_ONLY" elif record["loan_period"] == "": record["circulation_restriction"] = "NO_RESTRICTION" else: try: record["circulation_restriction"] = options[ record["loan_period"]] except KeyError: raise ItemMigrationError( "Unknown circulation restriction (loan period) on " "barcode {0}: {1}".format(record["barcode"], record["loan_period"])) del record["loan_period"]
def find_document_for_item(item_json): """Returns the document from an item.""" document = None document_cls = current_app_ils.document_record_cls try: document = get_record_by_legacy_recid(document_cls, item_json["id_bibrec"]) except PIDDoesNotExistError as e: try: document = get_document_by_barcode(item_json["barcode"]) except DocumentMigrationError as e: error_logger.error( "ITEM: {0} ERROR: Document {1} not found".format( item_json["barcode"], item_json["id_bibrec"])) raise e if document is None: error_msg = "ITEM: {0} ERROR: Document {1} not found".format( item_json["barcode"], item_json["id_bibrec"]) error_logger.error(error_msg) raise ItemMigrationError(error_msg) return document
def find_document_for_item(item_json): """Returns the document from an item.""" document = None document_cls = current_app_ils.document_record_cls legacy_pid_type = current_app.config["CDS_ILS_RECORD_LEGACY_PID_TYPE"] try: document = get_record_by_legacy_recid( document_cls, legacy_pid_type, item_json["id_bibrec"] ) except PIDDoesNotExistError as e: try: document = get_document_by_barcode(item_json["barcode"]) except DocumentMigrationError as e: pass if document is None: error_msg = ( f"Document {item_json['id_bibrec']} not found" f" for barcode {item_json['barcode']}" ) cli_logger.error(error_msg) raise ItemMigrationError(error_msg) return document