• 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

75.76
/rero_mef/agents/viaf/serializers.py
1
# RERO MEF
2
# Copyright (C) 2021 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, url_for
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 AgentMefSearch
1✔
30

31

32
def add_links(pid, record):
1✔
33
    """Add MEF link to VIAF."""
34
    links = {}
1✔
35
    viaf_pid = record.get("pid")
1✔
36
    mef_pid_search = (
1✔
37
        AgentMefSearch().filter("term", viaf_pid=viaf_pid).source(["pid"]).scan()
38
    )
39
    with contextlib.suppress(Exception):
1✔
40
        for idx, search in enumerate(mef_pid_search):
1✔
41
            url = "{scheme}://{host}/api/agents/mef/" + str(search.pid)
×
42
            if idx:
×
43
                links[f"mef {idx}"] = url
×
44
            else:
45
                links["mef"] = url
×
46
    viaf_url = current_app.config.get("RERO_MEF_VIAF_BASE_URL")
1✔
47
    links["viaf.org"] = f"{viaf_url}/viaf/{viaf_pid!s}"
1✔
48
    link_factory = default_links_factory_with_additional(links)
1✔
49
    return link_factory(pid)
1✔
50

51

52
# Nice to have direct working links in test server!
53
def local_link(agent, name, record):
1✔
54
    """Change links to actual links."""
55
    if name in record and (ref := record[name].get("$ref")):
×
56
        my_pid = ref.split("/")[-1]
×
57
        url = url_for(
×
58
            f"invenio_records_rest.{agent}_item", pid_value=my_pid, _external=True
59
        )
60
        record[name].update({"$ref": url})
×
61

62

63
class ReroMefSerializer(JSONSerializer):
1✔
64
    """Mixin serializing records as JSON."""
65

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

69
        :param pid: Persistent identifier instance.
70
        :param record: Record instance.
71
        :param links_factory: Factory function for record links.
72
        """
73
        return super().serialize(
1✔
74
            pid=pid, record=record, links_factory=add_links, **kwargs
75
        )
76

77

78
json_ = ReroMefSerializer(RecordSchemaJSONV1)
1✔
79
"""JSON v1 serializer."""
1✔
80

81
json_agent_viaf_response = record_responsify(json_, "application/rero+json")
1✔
82
json_agent_viaf_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