• 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

93.75
/sonar/modules/documents/extensions.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
"""DocumentRecord Extensions."""
17

18
import contextlib
1✔
19

20
from flask import current_app
1✔
21
from invenio_db import db
1✔
22
from invenio_pidstore.errors import PIDDoesNotExistError
1✔
23
from invenio_pidstore.models import PersistentIdentifier
1✔
24
from invenio_records.extensions import RecordExtension
1✔
25

26
from sonar.modules.documents.urn import Urn
1✔
27

28

29
class ArkDocumentExtension(RecordExtension):
1✔
30
    """Register/unregister Ark identifiers."""
31

32
    def post_delete(self, record, force=False):
1✔
33
        """Called after a record is deleted.
34

35
        :param record: the invenio record instance to be processed.
36
        :param force: unused.
37
        """
38
        if ark_id := record.get_ark():
1✔
39
            with contextlib.suppress(PIDDoesNotExistError):
1✔
40
                PersistentIdentifier.get("ark", ark_id).delete()
1✔
41

42

43
class UrnDocumentExtension(RecordExtension):
1✔
44
    """Create URN identifiers."""
45

46
    def pre_create(self, record):
1✔
47
        """Called before a record is created.
48

49
        :param record: the invenio record instance to be processed.
50
        """
51
        # Generate URN codes for documents without URNs.
52
        if not record.get_rero_urn_code(record):
1✔
53
            Urn.create_urn(record)
1✔
54
        if record.model:
1✔
55
            with db.session.begin_nested():
1✔
56
                record.model.data = record
1✔
57
                db.session.add(record.model)
1✔
58

59
    def post_create(self, record):
1✔
60
        """Called after a record is created.
61

62
        :param record: the invenio record instance to be processed.
63
        """
64
        try:
1✔
65
            Urn.register_urn_code_from_document(record)
1✔
66
        except Exception as e:
×
67
            current_app.logger.error(f"Error during URN registration{e}")
×
68

69
    def post_delete(self, record, force=False):
1✔
70
        """Called after a record is deleted.
71

72
        :param record: the invenio record instance to be processed.
73
        :param force: unused.
74
        """
75
        from .api import DocumentRecord
1✔
76

77
        if urn_code := DocumentRecord.get_rero_urn_code(record):
1✔
78
            with contextlib.suppress(PIDDoesNotExistError):
1✔
79
                urn_pid = PersistentIdentifier.get("urn", urn_code)
1✔
80
                urn_pid.delete()
1✔
81
                current_app.logger.error(
1✔
82
                    f"Document (pid:{record['pid']}) has been deleted and contains an URN ({urn_code})"
83
                )
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