def find_record_endpoint(pid, record_hit=None, **kwargs): """gets endpoint from pid type or from `$schema` if record_data is from search results, as all pid_types from search_result are `recid`. If both ways of resolving endpoint are not available gets it from pid_value - additional db query""" if pid.pid_type != "recid": return current_records_rest.default_endpoint_prefixes[pid.pid_type] elif record_hit and "$schema" in record_hit.get("_source", {}): return PidStoreBase.get_endpoint_from_pid_type( PidStoreBase.get_pid_type_from_schema( record_hit["_source"]["$schema"])) else: return PidStoreBase.get_endpoint_from_pid_type( PidStoreBase.get_pid_type_from_recid(pid.pid_value))
def get_linked_book(self, data): parent = get_parent_record(data) if parent and "titles" in parent and "control_number" in parent: endpoint = PidStoreBase.get_endpoint_from_pid_type( PidStoreBase.get_pid_type_from_schema(data["$schema"])) endpoint_item = f"invenio_records_rest.{endpoint}_item" ref = get_value(parent, "self.$ref") or url_for( endpoint_item, pid_value=parent["control_number"], _external=True) return {**parent["titles"][0], "record": {"$ref": ref}} return None
def _create_record(data, save_to_file=False): control_number = data["control_number"] click.echo(f"Creating record {control_number}.") record = InspireRecord.create_or_update(data) db.session.commit() record.index(delay=False) message = (f"Record created uuid:{record.id} with " f"pid:{control_number} has been created.") click.echo(click.style(message, fg="green")) if save_to_file: pid_type = PidStoreBase.get_pid_type_from_schema(data["$schema"]) endpoint = PidStoreBase.get_endpoint_from_pid_type(pid_type) file_path = os.path.join( f"data/records/{endpoint}/{control_number}.json") click.echo(click.style(f"Writing to {file_path}", fg="green")) with open(file_path, "w+") as file: file.write(orjson.dumps(data))
def test_get_endpoint_from_pid_type(mock_get_config_pid_types_to_endpoints, pid_type, expected): result = PidStoreBase.get_endpoint_from_pid_type(pid_type) assert expected == result
def get_ref_from_pid(pid_type, pid_value): """Return full $ref for record with pid_type and pid_value""" return get_record_ref(pid_value, PidStoreBase.get_endpoint_from_pid_type(pid_type))
def get_endpoint_from_schema(schema): pid_type = PidStoreBase.get_pid_type_from_schema(schema) return PidStoreBase.get_endpoint_from_pid_type(pid_type)