Skip to content

Commit

Permalink
config: added service schema for zenodo records
Browse files Browse the repository at this point in the history
* added export formats in config
* override bibtex serializer to include swhid
  • Loading branch information
alejandromumo committed Nov 20, 2024
1 parent 305b211 commit 547b7c0
Show file tree
Hide file tree
Showing 6 changed files with 99 additions and 0 deletions.
13 changes: 13 additions & 0 deletions invenio.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -1064,3 +1064,16 @@ COMMUNITIES_SHOW_BROWSE_MENU_ENTRY = True

JOBS_ADMINISTRATION_ENABLED = True
"""Enable Jobs administration view."""

from invenio_app_rdm.config import APP_RDM_RECORD_EXPORTERS as default_exporters

APP_RDM_RECORD_EXPORTERS = {
**default_exporters,
"bibtex": {
"name": _("BibTeX"),
"serializer": ("zenodo_rdm.serializers:ZenodoBibtexSerializer"),
"params": {},
"content-type": "application/x-bibtex",
"filename": "{id}.bib",
}
}
6 changes: 6 additions & 0 deletions site/zenodo_rdm/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
# under the terms of the MIT License; see LICENSE file for more details.
"""Custom code config."""


from .params import ZenodoArgsSchema, ZenodoSearchOptions
from .redirector import (
communities_detail_view_function,
Expand All @@ -27,6 +28,7 @@
redirect_records_search_slash,
search_view_function,
)
from .schema import ZenodoRecordSchema

# I18N_TRANSLATIONS_PATHS = [os.path.abspath("./site/zenodo_rdm/translations")]

Expand Down Expand Up @@ -370,3 +372,7 @@ def lock_edit_record_published_files(service, identity, record=None, draft=None)
"//cdnjs.cloudflare.com/ajax/libs/mathjax/3.2.2/es5/tex-mml-chtml.js"
"?config=TeX-AMS-MML_HTMLorMML"
)


RDM_RECORD_SCHEMA = ZenodoRecordSchema
"""Base record schema."""
2 changes: 2 additions & 0 deletions site/zenodo_rdm/legacy/serializers/schemas/zenodojson.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,8 @@ class ZenodoSchema(common.LegacySchema):
files = fields.Method("dump_files", dump_only=True)
metadata = fields.Nested(MetadataSchema)

swh = fields.Dict(dump_only=True)

owners = fields.Method("dump_owners")

def dump_owners(self, obj):
Expand Down
26 changes: 26 additions & 0 deletions site/zenodo_rdm/schema.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# -*- coding: utf-8 -*-
#
# Copyright (C) 2024 CERN.
#
# Zenodo-RDM is free software; you can redistribute it and/or modify
# it under the terms of the MIT License; see LICENSE file for more details.
"""Zenodo-RDM service schema."""

from invenio_rdm_records.services.schemas import RDMRecordSchema
from marshmallow import Schema, fields


class SWHSchema(Schema):
"""Software Heritage schema."""

swhid = fields.Str()


class ZenodoRecordSchema(RDMRecordSchema):
"""Zenodo service schema.
This schema subclasses the base schema and extends it with Zenodo-specific
fields.
"""

swh = fields.Nested(SWHSchema, dump_only=True)
11 changes: 11 additions & 0 deletions site/zenodo_rdm/serializers/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# -*- coding: utf-8 -*-
#
# Copyright (C) 2024 CERN.
#
# ZenodoRDM is free software; you can redistribute it and/or modify
# it under the terms of the MIT License; see LICENSE file for more details.
"""Zenodo serializers."""

from .bibtex import ZenodoBibtexSerializer

__all__ = ("ZenodoBibtexSerializer",)
41 changes: 41 additions & 0 deletions site/zenodo_rdm/serializers/bibtex.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# -*- coding: utf-8 -*-
#
# Copyright (C) 2024 CERN.
#
# ZenodoRDM is free software; you can redistribute it and/or modify
# it under the terms of the MIT License; see LICENSE file for more details.
"""Zenodo bibtex serializer."""

from flask_resources import BaseListSchema, MarshmallowSerializer
from flask_resources.serializers import SimpleSerializer
from invenio_rdm_records.resources.serializers import BibtexSerializer
from invenio_rdm_records.resources.serializers.bibtex.schema import BibTexSchema
from marshmallow import fields, missing


class ZenodoBibtexSchema(BibTexSchema):
"""Zenodo bibtex schema."""

swhid = fields.Method("get_swhid")

def get_swhid(self, obj):
"""Get swhid."""
return obj.get("swh", {}).get("swhid") or missing


class ZenodoBibtexSerializer(MarshmallowSerializer):
"""Zenodo bibtex serializer."""

def __init__(self, **options):
"""Initialize serializer."""
super().__init__(
format_serializer_cls=SimpleSerializer,
object_schema_cls=ZenodoBibtexSchema,
list_schema_cls=BaseListSchema,
encoder=self.bibtex_tostring,
)

@classmethod
def bibtex_tostring(cls, record):
"""Stringify a BibTex record."""
return record

0 comments on commit 547b7c0

Please sign in to comment.