class ReferenceItemSchemaV1(Schema): authors = ListWithLimit(NestedWithoutEmptyObjects(AuthorSchemaV1, dump_only=True, default=[]), limit=10) control_number = fields.Raw() label = fields.Raw() publication_info = fields.List( NestedWithoutEmptyObjects(PublicationInfoItemSchemaV1, dump_only=True)) titles = fields.Method('get_titles') @pre_dump(pass_many=True) def filter_references(self, data, many): reference_records = self.get_resolved_references_by_control_number( data) if not many: reference_record_id = self.get_reference_record_id(data) reference_record = reference_records.get(reference_record_id) reference = self.get_reference_or_linked_reference_with_label( data, reference_record) return reference references = [] for reference in data: reference_record_id = self.get_reference_record_id(reference) reference_record = reference_records.get(reference_record_id) reference = self.get_reference_or_linked_reference_with_label( reference, reference_record) references.append(reference) return references def get_reference_record_id(self, data): return get_recid_from_ref(data.get('record')) def get_resolved_references_by_control_number(self, data): data = force_list(data) resolved_records = get_linked_records_in_field({'references': data}, 'references.record') return { record['control_number']: record for record in resolved_records } def get_reference_or_linked_reference_with_label(self, data, reference_record): if reference_record: reference_record.update( {'label': data.get('reference', {}).get('label')}) return reference_record return data.get('reference') def get_titles(self, data): title = data.pop('title', None) if title: data['titles'] = force_list(title) return data.get('titles', missing)
class CitationItemSchemaV1(Schema): authors = ListWithLimit(NestedWithoutEmptyObjects(AuthorSchemaV1, dump_only=True), limit=10) control_number = fields.Raw() publication_info = fields.List( NestedWithoutEmptyObjects(PublicationInfoItemSchemaV1, dump_only=True)) titles = fields.Raw() @post_dump def strip_empty(self, data): return strip_empty_values(data)
class CitationItemSchemaV1(Schema): authors = ListWithLimit(NestedWithoutEmptyObjects(AuthorSchemaV1, dump_only=True), limit=10) collaborations = fields.List(fields.Nested(CollaborationSchemaV1, dump_only=True), attribute="collaborations") collaborations_with_suffix = fields.List(fields.Nested( CollaborationWithSuffixSchemaV1, dump_only=True), attribute="collaborations") control_number = fields.Raw() publication_info = fields.List( NestedWithoutEmptyObjects(PublicationInfoItemSchemaV1, dump_only=True)) titles = fields.Raw() earliest_date = fields.Raw() @post_dump def strip_empty(self, data): return strip_empty_values(data)
class ReferenceItemSchemaV1(Schema): authors = ListWithLimit(NestedWithoutEmptyObjects(AuthorSchemaV1, dump_only=True, default=[]), limit=10) collaborations = fields.List(fields.Nested(CollaborationSchemaV1, dump_only=True), attribute="collaborations") collaborations_with_suffix = fields.List(fields.Nested( CollaborationWithSuffixSchemaV1, dump_only=True), attribute="collaborations") control_number = fields.Raw() label = fields.Raw() urls = fields.Raw() publication_info = fields.List( NestedWithoutEmptyObjects(PublicationInfoItemSchemaV1, dump_only=True)) titles = fields.Method('get_titles') misc = fields.Method('get_misc') arxiv_eprint = fields.Method('get_arxiv_eprints') dois = fields.Method('get_dois') @pre_dump(pass_many=True) def filter_references(self, data, many): reference_records = self.get_resolved_references_by_control_number( data) if not many: return self.get_resolved_reference(data, reference_records) references = [] for reference in data: resolved_reference = self.get_resolved_reference( reference, reference_records) references.append(resolved_reference) return references @pre_dump def force_each_collaboration_to_be_object(self, data): if not data.get('record'): collaborations = get_value(data, 'reference.collaborations') if collaborations: data['reference']['collaborations'] = [{ 'value': collaboration } for collaboration in collaborations] return data def get_resolved_reference(self, data, reference_records): reference_record_id = self.get_reference_record_id(data) reference_record = reference_records.get(reference_record_id) reference = self.get_reference_or_linked_reference_with_label( data, reference_record) return reference def get_reference_record_id(self, data): return get_recid_from_ref(data.get('record')) def get_resolved_references_by_control_number(self, data): data = force_list(data) resolved_records = get_linked_records_in_field({'references': data}, 'references.record') return { record['control_number']: record for record in resolved_records } def get_reference_or_linked_reference_with_label(self, data, reference_record): if reference_record: reference_record.update( {'label': data.get('reference', {}).get('label', missing)}) return reference_record return data.get('reference') def get_titles(self, data): title = data.pop('title', None) if title: data['titles'] = force_list(title) return data.get('titles', missing) def get_dois(self, data): dois = data.get('dois', None) control_number = data.get('control_number') if dois and not control_number: data['dois'] = force_list( {'value': get_value(data, 'dois[0]', default=missing)}) elif dois: data['dois'] = force_list( {'value': get_value(data, 'dois[0].value', default=missing)}) return data.get('dois', missing) def get_arxiv_eprints(self, data): arxiv_eprint = data.pop('arxiv_eprint', None) arxiv_eprints = data.get('arxiv_eprints') if arxiv_eprint: data['arxiv_eprint'] = force_list({'value': arxiv_eprint}) elif arxiv_eprints: data['arxiv_eprint'] = force_list({ 'value': get_value(data, 'arxiv_eprints[0].value', default=missing) }) data.pop('arxiv_eprints', None) return data.get('arxiv_eprint', missing) def get_misc(self, data): titles = data.get('titles') title = data.get('title') misc = data.get('misc') if not title and not titles and misc: return misc[0] return missing @post_dump def strip_empty(self, data): return strip_empty_values(data)
class RecordMetadataSchemaV1(Schema): _collections = fields.Raw() abstracts = fields.Raw() accelerator_experiments = fields.Nested(AcceleratorExperimentSchemaV1, dump_only=True, many=True) acquisition_source = fields.Raw() arxiv_eprints = fields.Raw() authors = ListWithLimit(fields.Nested( AuthorSchemaV1, dump_only=True), limit=10) book_series = fields.Raw() # citeable = fields.Raw() citation_count = fields.Raw() collaborations = fields.List(fields.Nested(CollaborationSchemaV1, dump_only=True), attribute="collaborations") collaborations_with_suffix = fields.List(fields.Nested(CollaborationWithSuffixSchemaV1, dump_only=True), attribute="collaborations") conference_info = fields.Nested( ConferenceInfoItemSchemaV1, dump_only=True, attribute='publication_info', many=True) control_number = fields.Raw() # copyright = fields.Raw() # core = fields.Raw() corporate_author = fields.Raw() # curated = fields.Raw() date = fields.Method('get_formatted_date') # deleted = fields.Raw() # deleted_records = fields.Raw() document_type = fields.Raw() # documents = fields.Raw() dois = fields.Nested(DOISchemaV1, dump_only=True, many=True) # editions = fields.Raw() # energy_ranges = fields.Raw() external_system_identifiers = fields.Nested( ExternalSystemIdentifierSchemaV1, dump_only=True, many=True) # figures = fields.Raw() # funding_info = fields.Raw() imprints = fields.Raw() inspire_categories = fields.Raw() isbns = fields.List(fields.Nested(IsbnSchemaV1, dump_only=True)) keywords = fields.Raw() languages = fields.Raw() # legacy_creation_date = fields.Raw() # license = fields.Raw() # new_record = fields.Raw() number_of_authors = fields.Method('get_number_of_authors') number_of_pages = fields.Raw() number_of_references = fields.Method('get_number_of_references') persistent_identifiers = fields.Raw() preprint_date = fields.Raw() # public_notes = fields.Raw() publication_info = fields.Nested( PublicationInfoItemSchemaV1, dump_only=True, many=True) # publication_type = fields.Raw() # record_affiliations = fields.Raw() # refereed = fields.Raw() # related_records = fields.Raw() report_numbers = fields.Raw() # self = fields.Raw() texkeys = fields.Raw() thesis_info = fields.Nested(ThesisInfoSchemaV1, dump_only=True) # title_translations = fields.Raw() titles = fields.Raw() # urls = fields.Raw() # withdrawn = fields.Raw() def get_formatted_date(self, data): earliest_date = data.get('earliest_date') if earliest_date is None: return missing return format_date(earliest_date) def get_number_of_authors(self, data): authors = data.get('authors') return self.get_len_or_missing(authors) def get_number_of_references(self, data): number_of_references = data.get('number_of_references') if number_of_references is not None: return number_of_references references = data.get('references') return self.get_len_or_missing(references) @staticmethod def get_len_or_missing(maybe_none_list): if maybe_none_list is None: return missing return len(maybe_none_list) @post_dump def strip_empty(self, data): return strip_empty_values(data)