• 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

41.51
/sonar/modules/documents/cli/documents.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
"""Documents CLI commands."""
17

18
import json
1✔
19
from random import randint
1✔
20

21
import click
1✔
22
from flask.cli import with_appcontext
1✔
23
from invenio_db import db
1✔
24
from rero_invenio_files.pdf import PDFGenerator
1✔
25

26
from sonar.modules.documents.cli.rerodoc import rerodoc
1✔
27
from sonar.modules.documents.cli.urn import urn
1✔
28
from sonar.modules.documents.serializers.schemas.dc import DublinCoreSchema
1✔
29

30
from ..api import DocumentIndexer, DocumentRecord
1✔
31

32

33
@click.group()
1✔
34
def documents():
1✔
35
    """Commands for documents."""
36

37

38
documents.add_command(rerodoc)
1✔
39
documents.add_command(urn)
1✔
40

41

42
@click.group("documents")
1✔
43
def documents_fixtures():
1✔
44
    """Commands to import documents."""
45

46

47
def extract_metadata(record):
1✔
48
    """Extract metadata from record."""
49
    dc = DublinCoreSchema()
×
50
    data = {}
×
51
    if title := dc.get_title(record):
×
52
        data["title"] = title
×
53
    if authors := dc.get_contributors(record):
×
54
        data["authors"] = authors
×
55
    if summary := dc.get_descriptions(record):
×
56
        data["summary"] = summary[0]["value"]
×
57
    return data
×
58

59

60
@documents_fixtures.command("import")
1✔
61
@click.argument("file", type=click.File("r"))
1✔
62
@click.option("-m", "--maximum", type=int, default=None, help="Maximum number of documents to import.")
1✔
63
@with_appcontext
1✔
64
def import_documents(file, maximum=None):
1✔
65
    """Import documents from JSON file."""
66
    click.secho(f"Importing document from {file.name}")
×
67

68
    indexer = DocumentIndexer()
×
69
    n = 0
×
70
    data_documents = json.load(file)
×
71
    if maximum is not None:
×
72
        data_documents = data_documents[:maximum]
×
73
    with click.progressbar(data_documents, label="Loading documents...") as bar:
×
74
        for record in bar:
×
75
            try:
×
76
                # Register record to DB
77
                db_record = DocumentRecord.create(record)
×
78
                data = extract_metadata({"metadata": db_record})
×
79
                for i in range(1, randint(2, 5)):
×
80
                    pdf = PDFGenerator(data)
×
81
                    pdf.render()
×
82
                    db_record.add_file(pdf.output(), f"document_{i}.pdf", label="Document", type="file")
×
83

84
                db_record.commit()
×
85
                db.session.commit()
×
86

87
                indexer.index(db_record)
×
88
                n += 1
×
89
            except Exception as error:
×
90
                click.secho(
×
91
                    f"Document {record} could not be imported: {error}",
92
                    fg="red",
93
                )
94

95
    click.secho(f"{n} documents imported.", fg="green")
×
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