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

rero / sonar / 17425918180

03 Sep 2025 07:11AM UTC coverage: 95.796% (-0.6%) from 96.378%
17425918180

push

github

PascalRepond
translations: extract messages

Co-Authored-by: Pascal Repond <pascal.repond@rero.ch>

7816 of 8159 relevant lines covered (95.8%)

0.96 hits per line

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

97.92
/sonar/modules/documents/dojson/rerodoc/overdo.py
1
# Swiss Open Access Repository
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
"""Overdo specialized class for RERODOC DOJSON transformation."""
17

18
from flask import current_app
1✔
19

20
from sonar.modules.documents.dojson.overdo import Overdo as BaseOverdo
1✔
21
from sonar.modules.organisations.api import OrganisationRecord
1✔
22

23

24
class Overdo(BaseOverdo):
1✔
25
    """Overdo specialized class for RERODOC DOJSON transformation."""
26

27
    registererd_organisations = []
1✔
28

29
    @staticmethod
1✔
30
    def create_organisation(organisation_key):
1✔
31
        """Create organisation if not existing and return it.
32

33
        :param str organisation_key: Key (PID) of the organisation.
34
        """
35
        if not organisation_key:
1✔
36
            raise Exception("No key provided")
1✔
37

38
        # Get organisation record from database
39
        organisation = OrganisationRecord.get_record_by_pid(organisation_key)
1✔
40

41
        if not organisation:
1✔
42
            # Create organisation record
43
            organisation = OrganisationRecord.create(
1✔
44
                {
45
                    "code": organisation_key,
46
                    "name": organisation_key,
47
                    "isShared": False,
48
                    "isDedicated": False,
49
                },
50
                dbcommit=True,
51
            )
52
            organisation.reindex()
1✔
53

54
    def do(self, blob, ignore_missing=True, exception_handlers=None):
1✔
55
        """Do transformation."""
56
        result = super().do(blob, ignore_missing=ignore_missing, exception_handlers=exception_handlers)
1✔
57

58
        # Verify data integrity
59
        self.verify(result)
1✔
60

61
        # Add default license if not set.
62
        if not result.get("usageAndAccessPolicy"):
1✔
63
            default_license = "License undefined"
1✔
64
            if (
1✔
65
                result.get("organisation")
66
                and result["organisation"][0]["$ref"] == "https://sonar.ch/api/organisations/hepbejune"
67
            ):
68
                default_license = "CC BY-NC-SA"
×
69

70
            result["usageAndAccessPolicy"] = {"license": default_license}
1✔
71

72
        return result
1✔
73

74
    def get_contributor_role(self, role_700=None):
1✔
75
        """Return contributor role.
76

77
        :param role_700: String, role found in field 700$e
78
        :returns: String containing the mapped role or None
79
        """
80
        if role_700 in ["Dir.", "Codir."]:
1✔
81
            return "dgs"
1✔
82

83
        if role_700 == "Libr./Impr.":
1✔
84
            return "prt"
1✔
85

86
        if role_700 == "joint author":
1✔
87
            return "cre"
1✔
88

89
        if not role_700:
1✔
90
            doc_type = self.blob_record.get("980__", {}).get("a")
1✔
91

92
            if not doc_type:
1✔
93
                return None
1✔
94

95
            if doc_type in ["PREPRINT", "POSTPRINT", "DISSERTATION", "REPORT"]:
1✔
96
                return "cre"
1✔
97

98
            if doc_type in [
1✔
99
                "BOOK",
100
                "THESIS",
101
                "MAP",
102
                "JOURNAL",
103
                "PARTITION",
104
                "AUDIO",
105
                "IMAGE",
106
            ]:
107
                return "ctb"
1✔
108

109
        return None
1✔
110

111
    def verify(self, result):
1✔
112
        """Verify record data integrity after processing.
113

114
        :param result: Record data
115
        """
116

117
        def is_pa_mandatory():
1✔
118
            """Check if record types make provision activity mandatory."""
119
            document_type = result.get("documentType")
1✔
120

121
            if not document_type:
1✔
122
                return True
1✔
123

124
            return document_type not in [
1✔
125
                "coar:c_beb9",
126
                "coar:c_6501",
127
                "coar:c_998f",
128
                "coar:c_dcae04bc",
129
                "coar:c_3e5a",
130
                "coar:c_5794",
131
                "coar:c_6670",
132
            ]
133

134
        self.result_ok = True
1✔
135

136
        # Check if provision activity is set, but it's optional depending
137
        # on record types
138
        if "provisionActivity" not in result and is_pa_mandatory():
1✔
139
            self.result_ok = False
1✔
140
            current_app.logger.warning(f"No provision activity found in record {result}")
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