Skip to content

Commit 16c1837

Browse files
committed
feat(deposits): hide add new project option
Some organisations (e.g. hepvs) manage projects through their own dedicated form with a richer structure, so creating a project inline from the deposit form no longer makes sense for them. * Closes #1087. Co-Authored-By: Bertrand Zuchuat <bertrand.zuchuat@rero.ch>
1 parent 3398699 commit 16c1837

3 files changed

Lines changed: 74 additions & 0 deletions

File tree

sonar/config.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,10 @@ def _(x):
185185
JSONSCHEMAS_HOST = "sonar.ch"
186186
JSONSCHEMAS_REPLACE_REFS = True
187187

188+
#: List of organisation codes for which the "Add a new project" option
189+
#: is removed from the deposit's projects field.
190+
DEPOSITS_DISABLE_NEW_PROJECT_ORGANISATIONS = ["hepvs"]
191+
188192
# Flask configuration
189193
# ===================
190194
# See details on

sonar/jsonschemas/deposits_json_schema.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33

44
"""Deposits JSON schema class."""
55

6+
from flask import current_app
7+
68
from sonar.modules.users.api import current_user_record
79

810
from .json_schema_base import JSONSchemaBase
@@ -22,6 +24,9 @@ def process(self):
2224
if current_user_record:
2325
organisation = current_user_record.replace_refs().get("organisation")
2426

27+
if organisation.get("code") in current_app.config.get("DEPOSITS_DISABLE_NEW_PROJECT_ORGANISATIONS", []):
28+
schema["properties"]["projects"]["items"]["oneOf"].pop(1)
29+
2530
if not current_user_record or (current_user_record.is_moderator and organisation.get("isDedicated", False)):
2631
return schema
2732

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
# SPDX-FileCopyrightText: Fondation RERO+
2+
# SPDX-License-Identifier: AGPL-3.0-or-later
3+
4+
"""Test deposits JSON schema."""
5+
6+
from sonar.jsonschemas.deposits_json_schema import DepositsJSONSchema
7+
8+
9+
class MockUserRecord:
10+
"""Mock user record exposing only what DepositsJSONSchema.process() needs."""
11+
12+
is_moderator = False
13+
14+
def __init__(self, organisation):
15+
"""Store the organisation to return."""
16+
self.organisation = organisation
17+
18+
def replace_refs(self):
19+
"""Return self, no reference to resolve in this mock."""
20+
return self
21+
22+
def get(self, key):
23+
"""Return the mocked organisation."""
24+
return self.organisation if key == "organisation" else None
25+
26+
27+
def test_process_keeps_new_project_option_by_default(app, monkeypatch):
28+
"""Test the "Add a new project" option is kept when no organisation is restricted."""
29+
app.config["DEPOSITS_DISABLE_NEW_PROJECT_ORGANISATIONS"] = []
30+
monkeypatch.setattr(
31+
"sonar.jsonschemas.deposits_json_schema.current_user_record",
32+
MockUserRecord({"code": "hepvs"}),
33+
)
34+
35+
schema = DepositsJSONSchema("deposits").process()
36+
37+
assert len(schema["properties"]["projects"]["items"]["oneOf"]) == 2
38+
39+
40+
def test_process_removes_new_project_option_for_restricted_organisation(app, monkeypatch):
41+
"""Test the "Add a new project" option is removed for a restricted organisation."""
42+
app.config["DEPOSITS_DISABLE_NEW_PROJECT_ORGANISATIONS"] = ["hepvs"]
43+
monkeypatch.setattr(
44+
"sonar.jsonschemas.deposits_json_schema.current_user_record",
45+
MockUserRecord({"code": "hepvs"}),
46+
)
47+
48+
schema = DepositsJSONSchema("deposits").process()
49+
50+
one_of = schema["properties"]["projects"]["items"]["oneOf"]
51+
assert len(one_of) == 1
52+
assert one_of[0]["title"] == "Existing project"
53+
54+
55+
def test_process_keeps_new_project_option_for_other_organisation(app, monkeypatch):
56+
"""Test the "Add a new project" option is kept for a non-restricted organisation."""
57+
app.config["DEPOSITS_DISABLE_NEW_PROJECT_ORGANISATIONS"] = ["hepvs"]
58+
monkeypatch.setattr(
59+
"sonar.jsonschemas.deposits_json_schema.current_user_record",
60+
MockUserRecord({"code": "other"}),
61+
)
62+
63+
schema = DepositsJSONSchema("deposits").process()
64+
65+
assert len(schema["properties"]["projects"]["items"]["oneOf"]) == 2

0 commit comments

Comments
 (0)