Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add schemaorg serializer #1533

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions invenio_rdm_records/contrib/journal/processors.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,3 +154,35 @@ def post_dump(self, data, original=None, **kwargs):
data["ISSN"] = issn

return data


class JournalSchemaorgDumper(DumperMixin):
"""Dumper for Schemaorg serialization of 'Journal' custom field."""

def post_dump(self, data, original=None, **kwargs):
"""Adds serialized journal data to the input data."""
_original = original or {}
custom_fields = _original.get("custom_fields", {})
journal_data = custom_fields.get("journal:journal", {})

if not journal_data:
return data

title = journal_data.get("title")
volume = journal_data.get("volume")
issue = journal_data.get("issue")
pages = journal_data.get("pages")
issn = journal_data.get("issn")

if title:
data["container_title"] = title
if pages:
data["page"] = pages
if volume:
data["volume"] = volume
if issue:
data["issue"] = issue
if issn:
data["ISSN"] = issn

return data
2 changes: 2 additions & 0 deletions invenio_rdm_records/resources/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
DublinCoreXMLSerializer,
GeoJSONSerializer,
MARCXMLSerializer,
SchemaorgJSONLDSerializer,
StringCitationSerializer,
UIJSONSerializer,
)
Expand All @@ -78,6 +79,7 @@ def _bibliography_headers(obj_or_list, code, many=False):

record_serializers = {
"application/json": ResponseHandler(JSONSerializer(), headers=etag_headers),
"application/ld+json": ResponseHandler(SchemaorgJSONLDSerializer()),
"application/marcxml+xml": ResponseHandler(
MARCXMLSerializer(), headers=etag_headers
),
Expand Down
2 changes: 2 additions & 0 deletions invenio_rdm_records/resources/serializers/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
IIIFSequenceV2JSONSerializer,
)
from .marcxml import MARCXMLSerializer
from .schemaorg import SchemaorgJSONLDSerializer
from .ui import UIJSONSerializer

__all__ = (
Expand All @@ -37,6 +38,7 @@
"IIIFManifestV2JSONSerializer",
"IIIFSequenceV2JSONSerializer",
"MARCXMLSerializer",
"SchemaorgJSONLDSerializer",
"StringCitationSerializer",
"UIJSONSerializer",
"DCATSerializer",
Expand Down
27 changes: 27 additions & 0 deletions invenio_rdm_records/resources/serializers/schemaorg/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# -*- coding: utf-8 -*-
#
# Copyright (C) 2023 CERN.
#
# Invenio-RDM-Records is free software; you can redistribute it and/or modify
# it under the terms of the MIT License; see LICENSE file for more details.

"""Schemaorg Serializers for Invenio RDM Records."""
from flask_resources import BaseListSchema, MarshmallowSerializer
from flask_resources.serializers import JSONSerializer

from ....contrib.journal.processors import JournalSchemaorgDumper
from .schema import SchemaorgSchema


class SchemaorgJSONLDSerializer(MarshmallowSerializer):
"""Marshmallow based Schemaorg serializer for records."""

def __init__(self, **options):
"""Constructor."""
super().__init__(
format_serializer_cls=JSONSerializer,
object_schema_cls=SchemaorgSchema,
list_schema_cls=BaseListSchema,
schema_kwargs={"dumpers": [JournalSchemaorgDumper()]}, # Order matters
**options
)
Loading