• 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

82.14
/rero_mef/agents/serializers.py
1
# RERO MEF
2
# Copyright (C) 2020 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
import contextlib
1✔
19

20
from flask import current_app
1✔
21
from invenio_records_rest.links import default_links_factory_with_additional
1✔
22
from invenio_records_rest.schemas import RecordSchemaJSONV1
1✔
23
from invenio_records_rest.serializers.json import JSONSerializer
1✔
24
from invenio_records_rest.serializers.response import (
1✔
25
    record_responsify,
26
    search_responsify,
27
)
28

29
from .mef.api import AgentMefRecord
1✔
30
from .viaf.api import AgentViafSearch
1✔
31

32

33
def add_links(pid, record):
1✔
34
    """Add MEF link to agents."""
35
    links = {}
1✔
36
    for idx, mef_pid in enumerate(
1✔
37
        AgentMefRecord.get_mef(record.pid, record.name, pid_only=True)
38
    ):
39
        number = f"-{idx}" if idx else ""
×
40
        links[f"mef{number}"] = "{scheme}://{host}/api/agents/mef/" + str(mef_pid)
×
41

42
    with contextlib.suppress(Exception):
1✔
43
        query = (
1✔
44
            AgentViafSearch()
45
            .filter({"term": {record.viaf_pid_name: pid.pid_value}})
46
            .params(preserve_order=True)
47
            .sort({"_updated": {"order": "desc"}})
48
            .source("pid")
49
        )
50
        viaf_pid = next(query.scan()).pid
1✔
51
        links["viaf"] = "{scheme}://{host}/api/agents/viaf/" + str(viaf_pid)
×
52
        viaf_url = current_app.config.get("RERO_MEF_VIAF_BASE_URL")
×
53
        links["viaf.org"] = f"{viaf_url}/viaf/{viaf_pid!s}"
×
54
    link_factory = default_links_factory_with_additional(links)
1✔
55
    return link_factory(pid)
1✔
56

57

58
class ReroMefSerializer(JSONSerializer):
1✔
59
    """Mixin serializing records as JSON."""
60

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

64
        :param pid: Persistent identifier instance.
65
        :param record: Record instance.
66
        :param links_factory: Factory function for record links.
67
        """
68
        return super().serialize(
1✔
69
            pid=pid, record=record, links_factory=add_links, **kwargs
70
        )
71

72

73
json_ = ReroMefSerializer(RecordSchemaJSONV1)
1✔
74
"""JSON v1 serializer."""
1✔
75

76
json_agent_response = record_responsify(json_, "application/rero+json")
1✔
77
json_agent_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