def get_facet_author_name(self, record): """Prepare record for ``facet_author_name`` field.""" authors_with_record = list( InspireRecord.get_linked_records_from_dict_field( record, "authors.record")) found_authors_control_numbers = set([ author["control_number"] for author in authors_with_record if author.get("control_number") ]) authors_without_record = [ author for author in record.get("authors", []) if "record" not in author or int( PidStoreBase.get_pid_from_record_uri(author["record"].get( "$ref"))[1]) not in found_authors_control_numbers ] result = [] for author in authors_with_record: result.append(get_facet_author_name_for_author(author)) for author in authors_without_record: result.append("NOREC_{}".format( get_display_name_for_author_name(author["full_name"]))) return result
def _get_linked_pids_from_field(cls, data, path): """Return a list of (pid_type, pid_value) tuples for all records referenced in the field at the given path Args: data (dict): data from which records should be extracted path (str): the path of the linked records (where $ref is located). Returns: list: tuples containing (pid_type, pid_value) of the linked records Examples: >>> data = { 'references': [ { 'record': { '$ref': 'http://localhost/literature/1' } } ] } >>> record = InspireRecord(data) >>> records = record.get_linked_pids_from_field("references.record") ('lit', 1) """ full_path = ".".join([path, "$ref"]) pids = [ PidStoreBase.get_pid_from_record_uri(rec) for rec in flatten_list(get_value(data, full_path, [])) ] return pids
def redirect_pids(self, pids): if current_app.config.get("FEATURE_FLAG_ENABLE_REDIRECTION_OF_PIDS"): for pid in pids: pid_type, pid_value = PidStoreBase.get_pid_from_record_uri( pid["$ref"]) self.redirect_pid(pid_type, pid_value) return pids
def delete_records_from_deleted_records(cls, data): # Hack for migrator in case new record takes pids from other records # which should be deleted but they are not deleted yet. for pid in data.get("deleted_records", []): pid_type, pid_value = PidStoreBase.get_pid_from_record_uri(pid["$ref"]) try: record_to_delete = cls.get_record_by_pid_value( pid_value, pid_type, original_record=True ) except PIDDoesNotExistError: LOGGER.warning( "This pid is missing while still is marked as deleted by another record.", marked_by=data.get("control_number"), marked_to_delete=(pid_type, pid_value), ) else: record_to_delete.delete()
def resolve_conference_record_as_root(self, pub_info_item): conference_record = pub_info_item.get("conference_record") if conference_record is None: return {} _, recid = PidStoreBase.get_pid_from_record_uri( conference_record.get("$ref")) try: conference = InspireRecord.get_record_by_pid_value(pid_value=recid, pid_type="con") except PIDDoesNotExistError: return {} titles = conference.get("titles") if not titles: return {} pub_info_item.update(conference) return pub_info_item
def test_get_pid_from_record_uri(url, expected): data_result = PidStoreBase.get_pid_from_record_uri(url) assert expected == data_result