def test_get_affiliations(): """Test getting controlled affiliations.""" affiliation = ''' Institute for Research in Biomedicine (IRB), Faculty of Biomedical Sciences, Università della Svizzera italiana, Switzerland - Graduate School for Cellular and Biomedical Sciences, University of Bern, c/o Theodor Kocher Institute, Freiestrasse 1, P.O. Box 938, CH-3000 Bern 9, Switzerland ''' affiliations = DocumentRecord.get_affiliations(affiliation) assert affiliations == [ 'Uni of Bern and Hospital', 'Uni of Italian Switzerland' ] affiliations = DocumentRecord.get_affiliations(None) assert not affiliations
def marc21_to_contribution_field_100(self, key, value): """Extract contribution from field 100.""" if not value.get('a'): return None contribution = self.get('contribution', []) data = { 'agent': { 'type': 'bf:Person', 'preferred_name': value.get('a') }, 'role': ['cre'] } # Affiliation if value.get('u'): data['affiliation'] = value.get('u') affiliations = DocumentRecord.get_affiliations(value.get('u')) if affiliations: data['controlledAffiliation'] = affiliations # Date of birth - date of death date_of_birth, date_of_death = marc21tojson.extract_date(value.get('d')) if date_of_birth: data['agent']['date_of_birth'] = date_of_birth if date_of_death: data['agent']['date_of_death'] = date_of_death contribution.append(data) self['contribution'] = contribution return None
def create_document(self): """Create document from deposit.""" metadata = {} # Organisation if current_user_record and current_user_record.get('organisation'): metadata['organisation'] = current_user_record['organisation'] # Document type metadata['documentType'] = self['metadata']['documentType'] # Language language = self['metadata'].get('language', 'eng') # Title metadata['title'] = [{ 'type': 'bf:Title', 'mainTitle': [{ 'language': language, 'value': self['metadata']['title'] }] }] # Subtitle if self['metadata'].get('subtitle'): metadata['title'][0]['subtitle'] = [{ 'language': language, 'value': self['metadata']['subtitle'] }] # Other title if self['metadata'].get('otherLanguageTitle', {}).get('title'): metadata['title'][0]['mainTitle'].append({ 'language': self['metadata']['otherLanguageTitle'].get( 'language', language), 'value': self['metadata']['otherLanguageTitle']['title'] }) # Languages metadata['language'] = [{'value': language, 'type': 'bf:Language'}] # Document date if self['metadata'].get('documentDate'): metadata['provisionActivity'] = [{ 'type': 'bf:Publication', 'startDate': self['metadata']['documentDate'] }] # Published in if self['metadata'].get('publication'): part_of = { 'numberingYear': self['metadata']['publication']['year'], 'document': { 'title': self['metadata']['publication']['publishedIn'] } } if self['metadata']['publication'].get('pages'): part_of['numberingPages'] = self['metadata']['publication'][ 'pages'] if self['metadata']['publication'].get('volume'): part_of['numberingVolume'] = self['metadata']['publication'][ 'volume'] if self['metadata']['publication'].get('number'): part_of['numberingIssue'] = self['metadata']['publication'][ 'number'] if self['metadata']['publication'].get('editors'): part_of['document']['contribution'] = self['metadata'][ 'publication']['editors'] if self['metadata']['publication'].get('publisher'): part_of['document']['publication'] = { 'statement': self['metadata']['publication']['publisher'] } metadata['partOf'] = [part_of] # Other electronic versions if self['metadata'].get('otherElectronicVersions'): metadata['otherEdition'] = [{ 'document': { 'electronicLocator': link['url'] }, 'publicNote': link['publicNote'] } for link in self['metadata']['otherElectronicVersions']] # Specific collections if self['metadata'].get('specificCollections'): metadata['specificCollections'] = self['metadata'][ 'specificCollections'] # Classification if self['metadata'].get('classification'): metadata['classification'] = [{ 'type': 'bf:ClassificationUdc', 'classificationPortion': self['metadata']['classification'] }] # Abstracts if self['metadata'].get('abstracts'): metadata['abstracts'] = [{ 'language': abstract.get('language', language), 'value': abstract['abstract'] } for abstract in self['metadata']['abstracts']] # Subjects if self['metadata'].get('subjects'): metadata['subjects'] = [{ 'label': { 'language': subject.get('language', language), 'value': subject['subjects'] } } for subject in self['metadata']['subjects']] # Contributors contributors = [] for contributor in self['contributors']: data = { 'agent': { 'type': 'bf:Person', 'preferred_name': contributor['name'] }, 'role': [contributor['role']], 'affiliation': contributor.get('affiliation') } # ORCID for contributor if contributor.get('orcid'): data['agent']['identifiedBy'] = { 'type': 'bf:Doi', 'source': 'ORCID', 'value': contributor['orcid'] } # Resolve controlled affiliations if data.get('affiliation'): affiliations = DocumentRecord.get_affiliations( data['affiliation']) if affiliations: data['controlledAffiliation'] = affiliations else: data.pop('affiliation', None) contributors.append(data) if contributors: metadata['contribution'] = contributors document = DocumentRecord.create(metadata, dbcommit=True, with_bucket=True) current_order = 2 for file in self.files: with file.file.storage().open() as pdf_file: content = pdf_file.read() if file.get('category', 'main') == 'main': order = 1 else: order = current_order current_order += 1 kwargs = { 'label': file.get('label', file['key']), 'order': order } if file.get('embargo', False) and file.get('embargoDate'): kwargs['embargo_date'] = file['embargoDate'] if file.get('exceptInOrganisation'): kwargs['restricted'] = 'organisation' document.add_file(content, file['key'], **kwargs) document.commit() document.reindex() self['document'] = { '$ref': DocumentRecord.get_ref_link('documents', document['pid']) } return document