• 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.73
/sonar/resources/projects/service.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
"""Projects service."""
17

18
from invenio_records_resources.services import SearchOptions as BaseSearchOptions
1✔
19
from invenio_records_resources.services.records.facets import TermsFacet
1✔
20
from invenio_records_resources.services.records.params import (
1✔
21
    FacetsParam,
22
    PaginationParam,
23
    QueryStrParam,
24
    SortParam,
25
)
26
from invenio_records_resources.services.records.params.querystr import (
1✔
27
    SuggestQueryParser,
28
)
29
from invenio_records_resources.services.records.schema import ServiceSchemaWrapper
1✔
30
from invenio_records_rest.utils import obj_or_import_string
1✔
31

32
from sonar.modules.organisations.api import current_organisation
1✔
33
from sonar.modules.utils import has_custom_resource
1✔
34

35
from ..service import RecordService, RecordServiceConfig
1✔
36
from .api import Record, RecordComponent
1✔
37
from .permissions import RecordPermissionPolicy
1✔
38
from .results import RecordList
1✔
39

40

41
class PreFacetsParam(FacetsParam):
1✔
42
    """."""
43

44
    def filter(self, search):
1✔
45
        """Apply a pre filter on the search."""
46
        if not self._filters:
1✔
47
            return search
1✔
48

49
        filters = list(self._filters.values())
1✔
50

51
        post_filter = filters[0]
1✔
52
        for f in filters[1:]:
1✔
53
            post_filter |= f
×
54

55
        return search.filter(post_filter)
1✔
56

57

58
class SearchOptions(BaseSearchOptions):
1✔
59
    """Search options."""
60

61
    sort_default = "relevance"
1✔
62
    sort_default_no_query = "newest"
1✔
63
    sort_options = {
1✔
64
        "relevance": {
65
            "fields": ["_score"],
66
        },
67
        "name": {"fields": ["metadata.name.raw"]},
68
        "newest": {"fields": ["-metadata.startDate"]},
69
        "oldest": {"fields": ["metadata.startDate"]},
70
    }
71

72
    pagination_options = {"default_results_per_page": 10, "default_max_results": 10000}
1✔
73

74
    params_interpreters_cls = [
1✔
75
        QueryStrParam,
76
        PaginationParam,
77
        SortParam,
78
        PreFacetsParam,
79
    ]
80

81
    facets = {
1✔
82
        "user": TermsFacet(field="metadata.user.pid"),
83
        "organisation": TermsFacet(field="metadata.organisation.pid"),
84
        "status": TermsFacet(field="metadata.validation.status"),
85
    }
86

87
    suggest_parser_cls = SuggestQueryParser.factory(
1✔
88
        fields=[
89
            "metadata.name.suggest",
90
            "metadata.projectSponsor.suggest",
91
            "metadata.innerSearcher.suggest",
92
            "metadata.keywords.suggest",
93
        ]
94
    )
95

96

97
class ProjectsRecordServiceConfig(RecordServiceConfig):
1✔
98
    """Projects service configuration."""
99

100
    permission_policy_cls = RecordPermissionPolicy
1✔
101

102
    record_cls = Record
1✔
103

104
    result_list_cls = RecordList
1✔
105

106
    search = SearchOptions
1✔
107

108
    components = [*RecordServiceConfig.components, RecordComponent]
1✔
109

110

111
class ProjectsRecordService(RecordService):
1✔
112
    """Projects service."""
113

114
    default_config = ProjectsRecordServiceConfig
1✔
115

116
    @property
1✔
117
    def schema(self):
1✔
118
        """Returns the data schema instance."""
119
        schema_path = "sonar.resources.projects.schema:RecordSchema"
1✔
120

121
        if has_custom_resource("projects"):
1✔
122
            schema_path = f"sonar.dedicated.{current_organisation['code']}.projects.schema:RecordSchema"
1✔
123

124
        schema = obj_or_import_string(schema_path)
1✔
125

126
        return ServiceSchemaWrapper(self, schema=schema)
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