Skip to content

Commit

Permalink
oai: fix error on document type processing
Browse files Browse the repository at this point in the history
* Closes rero#789.

Co-Authored-by: Bertrand Zuchuat <bertrand.zuchuat@rero.ch>
  • Loading branch information
Garfield-fr committed Mar 2, 2022
1 parent dd07585 commit faf5ede
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 5 deletions.
10 changes: 10 additions & 0 deletions sonar/modules/documents/serializers/dc.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,22 @@

"""Dublin Core serializer."""

from invenio_records_rest.serializers.base import PreprocessorMixin
from invenio_records_rest.serializers.dc import DublinCoreSerializer


class SonarDublinCoreSerializer(DublinCoreSerializer):
"""Marshmallow based DublinCore serializer for records."""

@staticmethod
def preprocess_search_hit(pid, record_hit, links_factory=None, **kwargs):
"""Prepare a record hit from Elasticsearch for serialization."""
# TODO: SET VERSION BEFORE PROCESS RECORD. FIND BETTER SOLUTION.
if '_version' not in record_hit:
record_hit['_version'] = 1;
return PreprocessorMixin.preprocess_search_hit(
pid, record_hit, links_factory=None, **kwargs)

def dump(self, obj, context=None):
"""Serialize object with schema.
Expand Down
11 changes: 6 additions & 5 deletions sonar/modules/documents/serializers/schemas/dc.py
Original file line number Diff line number Diff line change
Expand Up @@ -249,11 +249,12 @@ def get_titles(self, obj):

def get_types(self, obj):
"""Get types."""
if obj['metadata'].get('documentType'):
return [
'http://purl.org/coar/resource_type/{type}'.format(
type=obj['metadata']['documentType'].split(':')[1])
]
if obj['metadata'].get('documentType', ''):
types = obj['metadata'].get('documentType', '').split(':')
if len(types) == 1:
return [f'{types[0]}']
if len(types) == 2:
return [f'http://purl.org/coar/resource_type/{types[1]}']

return []

Expand Down
4 changes: 4 additions & 0 deletions tests/ui/documents/test_dc_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -458,6 +458,10 @@ def test_types(minimal_document):
result = dc_v1.transform_record(minimal_document['pid'], minimal_document)
assert result['types'] == []

minimal_document['documentType'] = 'advanced_studies_thesis'
result = dc_v1.transform_record(minimal_document['pid'], minimal_document)
assert result['types'] == ['advanced_studies_thesis']

minimal_document['documentType'] = 'coar:c_2f33'
result = dc_v1.transform_record(minimal_document['pid'], minimal_document)
assert result['types'] == ['http://purl.org/coar/resource_type/c_2f33']

0 comments on commit faf5ede

Please sign in to comment.