• 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.83
/sonar/modules/stats/serializers/__init__.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
"""Serializers."""
17

18
import csv
1✔
19

20
from flask import current_app
1✔
21
from invenio_records_rest.serializers.csv import CSVSerializer as BaseCSVSerializer
1✔
22
from invenio_records_rest.serializers.csv import Line
1✔
23
from invenio_records_rest.serializers.response import (
1✔
24
    record_responsify,
25
    search_responsify,
26
)
27

28
from sonar.modules.serializers import JSONSerializer
1✔
29
from sonar.theme.views import format_date
1✔
30

31
from ..schemas import RecordSchema
1✔
32

33

34
class CSVSerializer(BaseCSVSerializer):
1✔
35
    """Mixin serializing records as JSON."""
36

37
    def _format_csv(self, records):
1✔
38
        """Return the list of records as a CSV string.
39

40
        :param list recors: Records list
41
        """
42
        assert len(records) == 1
1✔
43
        record = records[0]
1✔
44
        headers = ["organisation", "type", "documents", "full_text", "no_full_text"]
1✔
45

46
        # Write header
47
        line = Line()
1✔
48
        writer = csv.DictWriter(line, fieldnames=headers)
1✔
49
        writer.writeheader()
1✔
50
        yield line.read()
1✔
51

52
        # Dump values
53
        for value in record["metadata"]["values"]:
1✔
54
            value["documents"] = len(value["pids"])
1✔
55
            value["no_full_text"] = value["documents"] - value["full_text"]
1✔
56
            value.pop("pids", None)
1✔
57
            writer.writerow(value)
1✔
58
            yield line.read()
1✔
59

60
    def process_dict(self, dictionary):
1✔
61
        """Transform record dict with nested keys to a flat dict.
62

63
        Needed to override the parent method to do nothing.
64
        :param dictionary: An input dictionary
65
        :returns: an untouched dictionary
66
        :rtype: dict
67
        """
68
        return dictionary
1✔
69

70

71
# Serializers
72
# ===========
73
#: JSON serializer definition.
74
json_v1 = JSONSerializer(RecordSchema)
1✔
75
#: CSV serializer definition.
76
csv_v1 = CSVSerializer()
1✔
77

78
# Records-REST serializers
79
# ========================
80
#: JSON record serializer for individual records.
81
json_v1_response = record_responsify(json_v1, "application/json")
1✔
82
#: JSON record serializer for search results.
83
json_v1_search = search_responsify(json_v1, "application/json")
1✔
84

85

86
def csv_record_responsify(serializer, mimetype):
1✔
87
    """Create a Records-REST response serializer.
88

89
    This function is the same as the `invenio-records-rest`, but it adds an
90
    header to change the download file name.
91

92
    :param serializer: Serializer instance.
93
    :param mimetype: MIME type of response.
94
    :returns: Function that generates a record HTTP response.
95
    """
96

97
    def view(pid, record, code=200, headers=None, links_factory=None):
1✔
98
        response = current_app.response_class(
1✔
99
            serializer.serialize(pid, record, links_factory=links_factory),
100
            mimetype=mimetype,
101
        )
102
        response.status_code = code
1✔
103
        response.cache_control.no_cache = True
1✔
104
        response.set_etag(str(record.revision_id))
1✔
105
        response.last_modified = record.updated
1✔
106
        if headers is not None:
1✔
107
            response.headers.extend(headers)
×
108

109
        # set the output filename
110
        date = format_date(record.created, "%Y%m%d%H%M")
1✔
111
        filename = f"stats-{date}.csv"
1✔
112
        if not response.headers.get("Content-Disposition"):
1✔
113
            response.headers["Content-Disposition"] = f'attachment; filename="{filename}"'
1✔
114

115
        return response
1✔
116

117
    return view
1✔
118

119

120
#: CSV record serializer for individual records.
121
csv_v1_response = csv_record_responsify(csv_v1, "text/csv")
1✔
122

123
__all__ = (
1✔
124
    "json_v1",
125
    "json_v1_response",
126
    "json_v1_search",
127
)
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