• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

rero / rero-mef / 16621609190

30 Jul 2025 11:43AM UTC coverage: 84.491% (+0.008%) from 84.483%
16621609190

push

github

rerowep
chore: update dependencies

Co-Authored-by: Peter Weber <peter.weber@rero.ch>

4560 of 5397 relevant lines covered (84.49%)

0.84 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

96.88
/rero_mef/places/mef/serializers.py
1
# RERO MEF
2
# Copyright (C) 2024 RERO
3
#
4
# This program is free software: you can redistribute it and/or modify
5
# it under the terms of the GNU Affero General Public License as published by
6
# the Free Software Foundation, version 3 of the License.
7
#
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
# GNU Affero General Public License for more details.
12
#
13
# You should have received a copy of the GNU Affero General Public License
14
# along with this program. If not, see <http://www.gnu.org/licenses/>.
15

16
"""Record serialization."""
17

18
from flask import request, url_for
1✔
19
from invenio_records_rest.links import default_links_factory_with_additional
1✔
20
from invenio_records_rest.schemas import RecordSchemaJSONV1
1✔
21
from invenio_records_rest.serializers.json import JSONSerializer
1✔
22
from invenio_records_rest.serializers.response import (
1✔
23
    record_responsify,
24
    search_responsify,
25
)
26

27
from ...utils import get_entity_classes
1✔
28

29

30
def add_links(pid, record):
1✔
31
    """Add VIAF links to MEF."""
32
    links = {}
1✔
33
    # viaf_pid = record.get('viaf_pid')
34
    # if viaf_pid:
35
    #     links['viaf'] = '{scheme}://{host}/api/places/viaf/' \
36
    #             + str(viaf_pid)
37
    #     viaf_url = current_app.confg.get('RERO_MEF_VIAF_BASE_URL')
38
    #     links['viaf.org'] = '{viaf_url}/viaf/' + str(viaf_pid)
39

40
    link_factory = default_links_factory_with_additional(links)
1✔
41
    return link_factory(pid)
1✔
42

43

44
# Nice to have direct working links in test server!
45
def local_link(place, name, record):
1✔
46
    """Change links to actual links."""
47
    if name in record and (ref := record[name].get("$ref")):
1✔
48
        my_pid = ref.split("/")[-1]
1✔
49
        url = url_for(
1✔
50
            f"invenio_records_rest.{place}_item", pid_value=my_pid, _external=True
51
        )
52
        record[name].update({"$ref": url})
1✔
53

54

55
class ReroMefSerializer(JSONSerializer):
1✔
56
    """Mixin serializing records as JSON."""
57

58
    def serialize(self, pid, record, links_factory=None, **kwargs):
1✔
59
        """Serialize a single record and persistent identifier.
60

61
        :param pid: Persistent identifier instance.
62
        :param record: Record instance.
63
        :param links_factory: Factory function for record links.
64
        """
65
        rec = record
1✔
66
        if request:
1✔
67
            rec = rec.add_information(
1✔
68
                resolve=request.args.get(
69
                    "resolve", default=False, type=lambda v: v.lower() in ["true", "1"]
70
                ),
71
                sources=request.args.get(
72
                    "sources", default=False, type=lambda v: v.lower() in ["true", "1"]
73
                ),
74
            )
75
            # because the replace_refs loose the record original model. We need
76
            # to reset it to have correct 'created'/'updated' output data
77
            rec.model = record.model
1✔
78
            if not rec.get("type"):
1✔
79
                rec["type"] = "bf:Place"
×
80

81
        place_classes = get_entity_classes()
1✔
82
        for place, place_classe in place_classes.items():
1✔
83
            if place in ["pidref", "plgnd"]:
1✔
84
                local_link(place, place_classe.name, rec)
1✔
85

86
        return super().serialize(pid=pid, record=rec, links_factory=add_links, **kwargs)
1✔
87

88

89
_json = ReroMefSerializer(RecordSchemaJSONV1)
1✔
90
"""JSON v1 serializer."""
1✔
91

92
json_place_mef_response = record_responsify(_json, "application/rero+json")
1✔
93
json_place_mef_search = search_responsify(_json, "application/rero+json")
1✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc