Skip to content

Commit 389e25c

Browse files
committed
♻️(backend) simplify further select options on link reach/role
We reduce the number of options even more by treating link reach and link role independently: link reach must be higher than its ancestors' equivalent link reach and link role must be higher than its ancestors' link role. This reduces the number of possibilities but we decided to start with the most restrictive and simple offer and extend it if we realize it faces too many criticism instead of risking to offer too many options that are too complex and must be reduced afterwards.
1 parent 364189b commit 389e25c

File tree

2 files changed

+15
-44
lines changed

2 files changed

+15
-44
lines changed

src/backend/core/choices.py

+12-39
Original file line numberDiff line numberDiff line change
@@ -65,49 +65,22 @@ class LinkReachChoices(PriorityTextChoices):
6565
def get_select_options(cls, link_reach, link_role):
6666
"""
6767
Determines the valid select options for link reach and link role depending on the
68-
list of ancestors' link reach/role definitions.
68+
ancestors' link reach/role given as arguments.
6969
Returns:
7070
Dictionary mapping possible reach levels to their corresponding possible roles.
7171
"""
72-
# If no ancestors, return all options
73-
if not link_reach:
74-
return {
75-
reach: LinkRoleChoices.values if reach != cls.RESTRICTED else None
76-
for reach in cls.values
77-
}
78-
79-
# Initialize the result for all reaches with possible roles
80-
result = {
81-
reach: set(LinkRoleChoices.values) if reach != cls.RESTRICTED else None
82-
for reach in cls.values
83-
}
84-
85-
# Handle special rules directly with early returns for efficiency
86-
87-
if link_role == LinkRoleChoices.EDITOR:
88-
# Rule 1: public/editor → override everything
89-
if link_reach == cls.PUBLIC:
90-
return {cls.PUBLIC: [LinkRoleChoices.EDITOR]}
91-
92-
# Rule 2: authenticated/editor
93-
if link_reach == cls.AUTHENTICATED:
94-
result[cls.AUTHENTICATED].discard(LinkRoleChoices.READER)
95-
result.pop(cls.RESTRICTED, None)
96-
97-
if link_role == LinkRoleChoices.READER:
98-
# Rule 3: public/reader
99-
if link_reach == cls.PUBLIC:
100-
result.pop(cls.AUTHENTICATED, None)
101-
result.pop(cls.RESTRICTED, None)
102-
103-
# Rule 4: authenticated/reader
104-
if link_reach == cls.AUTHENTICATED:
105-
result.pop(cls.RESTRICTED, None)
106-
107-
# Convert sets to ordered lists where applicable
10872
return {
109-
reach: sorted(roles, key=LinkRoleChoices.get_priority) if roles else roles
110-
for reach, roles in result.items()
73+
reach: [
74+
role
75+
for role in LinkRoleChoices.values
76+
if LinkRoleChoices.get_priority(role)
77+
>= LinkRoleChoices.get_priority(link_role)
78+
]
79+
if reach != cls.RESTRICTED
80+
else None
81+
for reach in cls.values
82+
if LinkReachChoices.get_priority(reach)
83+
>= LinkReachChoices.get_priority(link_reach)
11184
}
11285

11386

src/backend/core/tests/test_models_documents.py

+3-5
Original file line numberDiff line numberDiff line change
@@ -1190,7 +1190,7 @@ def test_models_documents_restore_complex_bis(django_assert_num_queries):
11901190
(
11911191
"authenticated",
11921192
"editor",
1193-
{"authenticated": ["editor"], "public": ["reader", "editor"]},
1193+
{"authenticated": ["editor"], "public": ["editor"]},
11941194
),
11951195
(
11961196
"restricted",
@@ -1206,8 +1206,8 @@ def test_models_documents_restore_complex_bis(django_assert_num_queries):
12061206
"editor",
12071207
{
12081208
"restricted": None,
1209-
"authenticated": ["reader", "editor"],
1210-
"public": ["reader", "editor"],
1209+
"authenticated": ["editor"],
1210+
"public": ["editor"],
12111211
},
12121212
),
12131213
# No ancestors (edge case)
@@ -1216,8 +1216,6 @@ def test_models_documents_restore_complex_bis(django_assert_num_queries):
12161216
None,
12171217
{
12181218
"public": ["reader", "editor"],
1219-
"authenticated": ["reader", "editor"],
1220-
"restricted": None,
12211219
},
12221220
),
12231221
(

0 commit comments

Comments
 (0)