Skip to content

Commit

Permalink
Merge 4ce86fd into 051b75b
Browse files Browse the repository at this point in the history
  • Loading branch information
Sébastien Délèze authored Aug 20, 2021
2 parents 051b75b + 4ce86fd commit fe6692a
Show file tree
Hide file tree
Showing 19 changed files with 2,612 additions and 49 deletions.
3 changes: 2 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,8 @@
'monitoring = sonar.monitoring.views:blueprint',
'translations = sonar.translations.rest:blueprint',
'suggestions = sonar.suggestions.rest:blueprint',
'validation = sonar.modules.validation.views:blueprint'
'validation = sonar.modules.validation.views:blueprint',
'swisscovery = sonar.modules.swisscovery.rest:blueprint'
],
'invenio_assets.webpack': [
'sonar_theme = sonar.theme.webpack:theme'
Expand Down
3 changes: 3 additions & 0 deletions sonar/config_sonar.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,3 +97,6 @@
"""ARK scheme."""
# SONAR_APP_ARK_SHOULDER = 'ffk3'
"""ARK Shoulder, can be multiple for a given organisation."""

SONAR_APP_SWISSCOVERY_SEARCH_URL = 'https://swisscovery.slsp.ch/view/sru/41SLSP_NETWORK'
SONAR_APP_SWISSCOVERY_SEARCH_VERSION = '1.1'
26 changes: 26 additions & 0 deletions sonar/modules/deposits/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,32 @@ def create_document(self):
if identifiers:
metadata['identifiedBy'] = identifiers

# Content note
if self['metadata'].get('contentNote'):
metadata['contentNote'] = self['metadata']['contentNote']

# Extent
if self['metadata'].get('extent'):
metadata['extent'] = self['metadata']['extent']

# Additional materials
if self['metadata'].get('additionalMaterials'):
metadata['additionalMaterials'] = self['metadata'][
'additionalMaterials']

# Formats
if self['metadata'].get('formats'):
metadata['formats'] = self['metadata']['formats']

# Other material characteristics
if self['metadata'].get('otherMaterialCharacteristics'):
metadata['otherMaterialCharacteristics'] = self['metadata'][
'otherMaterialCharacteristics']

# Edition statement
if self['metadata'].get('editionStatement'):
metadata['editionStatement'] = self['metadata']['editionStatement']

# Contributors
contributors = []
for contributor in self.get('contributors', []):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -915,6 +915,83 @@
]
}
}
},
"contentNote": {
"title": "Content notes",
"type": "array",
"minItems": 1,
"items": {
"title": "Content note",
"type": "string",
"minLength": 1
}
},
"extent": {
"title": "Extent",
"description": "Extent of the resource, i.e. the number of pages or volumes.",
"type": "string",
"minLength": 1
},
"additionalMaterials": {
"title": "Additional materials",
"description": "Accompanying material of the resource, i.e. maps.",
"type": "string",
"minLength": 1
},
"formats": {
"title": "Formats",
"description": "Format of the resource, i.e. dimensions in cm.",
"type": "array",
"minItems": 1,
"items": {
"title": "Format",
"type": "string",
"minLength": 1
}
},
"otherMaterialCharacteristics": {
"title": "Other Material Characteristics",
"description": "Other Material Characteristics, i.e. illustrations, black and white, or coloured.",
"type": "string",
"minLength": 1
},
"editionStatement": {
"title": "Edition",
"type": "object",
"additionalProperties": false,
"properties": {
"editionDesignation": {
"title": "Designation",
"type": "object",
"properties": {
"value": {
"title": "Value",
"type": "string",
"minLength": 1
}
},
"required": [
"value"
]
},
"responsibility": {
"title": "Responsibility",
"type": "object",
"properties": {
"value": {
"title": "Value",
"type": "string",
"minLength": 1
}
},
"required": [
"value"
]
}
},
"required": [
"editionDesignation"
]
}
}
},
Expand Down
36 changes: 36 additions & 0 deletions sonar/modules/deposits/mappings/v7/deposits/deposit-v1.0.0.json
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,42 @@
"format": "yyyy-MM-dd||yyyy"
}
}
},
"contentNote": {
"type": "text"
},
"extent": {
"type": "text"
},
"additionalMaterials": {
"type": "text"
},
"formats": {
"type": "text"
},
"otherMaterialCharacteristics": {
"type": "text"
},
"editionStatement": {
"type": "object",
"properties": {
"editionDesignation": {
"type": "object",
"properties": {
"value": {
"type": "text"
}
}
},
"responsibility": {
"type": "object",
"properties": {
"value": {
"type": "text"
}
}
}
}
}
}
},
Expand Down
131 changes: 131 additions & 0 deletions sonar/modules/deposits/serializers/schemas/document.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
# -*- coding: utf-8 -*-
#
# Swiss Open Access Repository
# Copyright (C) 2021 RERO
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, version 3 of the License.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.

"""Document serializer."""

from marshmallow import Schema, fields, post_dump, pre_dump


class RemoveEmptyValuesMixin():
"""Mixin for removing empty values from schema."""

@post_dump
def remove_empty_values(self, data, **kwargs):
"""Remove empty values before dumping data."""
return {key: value for key, value in data.items() if value}


class DocumentMetadataSchema(Schema, RemoveEmptyValuesMixin):
"""Serialize deposit metadata."""

title = fields.Method('get_title')
subtitle = fields.Method('get_subtitle')
identifiedBy = fields.List(fields.Dict())
language = fields.Method('get_language')
abstracts = fields.Method('get_abstracts')
documentType = fields.Str()
contentNote = fields.List(fields.Str())
extent = fields.Str()
dissertation = fields.Dict()
additionalMaterials = fields.Str()
formats = fields.List(fields.Str())
otherMaterialCharacteristics = fields.Str()
editionStatement = fields.Dict()
documentDate = fields.Method('get_date')
publicationPlace = fields.Method('get_publication_place')
publisher = fields.Method('get_publisher')

def get_title(self, obj):
"""Get title."""
if not obj.get('title'):
return None

return obj['title'][0]['mainTitle'][0]['value']

def get_subtitle(self, obj):
"""Get subttitle."""
if not obj.get('title') or not obj['title'][0].get('subtitle'):
return None

return obj['title'][0]['subtitle'][0]['value']

def get_language(self, obj):
"""Get language."""
if not obj.get('language'):
return None

return obj['language'][0]['value']

def get_abstracts(self, obj):
"""Get abstracts."""
if not obj.get('abstracts'):
return None

return [{
'language': item['language'],
'abstract': item['value']
} for item in obj['abstracts']]

def get_date(self, obj):
"""Get date."""
for provision_activity in obj.get('provisionActivity', []):
if provision_activity.get('startDate'):
return provision_activity['startDate']

return None

def get_publication_place(self, obj):
"""Get publication place."""
for provision_activity in obj.get('provisionActivity', []):
for statement in provision_activity.get('statement', []):
if statement['type'] == 'bf:Place':
return statement['label']['value']

return None

def get_publisher(self, obj):
"""Get publisher."""
for provision_activity in obj.get('provisionActivity', []):
for statement in provision_activity.get('statement', []):
if statement['type'] == 'bf:Agent':
return statement['label']['value']

return None


class DocumentSchema(Schema, RemoveEmptyValuesMixin):
"""Serialize deposit from document schema."""

metadata = fields.Nested(DocumentMetadataSchema)
contributors = fields.Method('get_contributors')

@pre_dump
def init_data(self, item, **kwargs):
"""Initialize data before processing."""
contribution = item.pop('contribution', None)
item = {'metadata': item, 'contribution': contribution}
return item

def get_contributors(self, obj):
"""Get contributors."""
if not obj.get('contribution'):
return None

return [{
'name': item['agent']['preferred_name'],
'role': item['role'][0]
} for item in obj['contribution']]
33 changes: 33 additions & 0 deletions sonar/modules/documents/dojson/overdo.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@

"""Base overdo class for DOJSON transformation."""

import re

from dojson import Overdo as BaseOverdo


Expand All @@ -36,6 +38,37 @@ def not_repetitive(value, subfield, default=None):

return data

@staticmethod
def extract_date(date=None):
"""Try to extract date of birth and date of death from field.
:param date: String, date to parse
:returns: Tuple containing date of birth and date of death
"""
if not date:
return (None, None)

# Match a full date
match = re.search(r'^([0-9]{4}-[0-9]{2}-[0-9]{2})$', date)
if match:
return (match.group(1), None)

match = re.search(r'^([0-9]{2}-[0-9]{2}-[0-9]{4})$', date)
if match:
return (match.group(1), None)

# Match these value: "1980-2010"
match = re.search(r'^([0-9]{4})-([0-9]{4})$', date)
if match:
return (match.group(1), match.group(2))

# Match these value: "1980-" or "1980"
match = re.search(r'^([0-9]{4})-?', date)
if match:
return (match.group(1), None)

raise Exception('Date "{date}" is not recognized'.format(date=date))

def do(self, blob, ignore_missing=True, exception_handlers=None):
"""Store blob values and do transformation."""
self.blob_record = blob
Expand Down
Loading

0 comments on commit fe6692a

Please sign in to comment.