Skip to content

Commit 19b0842

Browse files
committed
reduced duplication
1 parent 733ecdd commit 19b0842

File tree

3 files changed

+31
-46
lines changed

3 files changed

+31
-46
lines changed

packages/postgres-database/src/simcore_postgres_database/utils_users.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,3 +229,30 @@ def is_private(hide_attribute: Column, caller_id: int):
229229

230230
def is_public(hide_attribute: Column, caller_id: int):
231231
return hide_attribute.is_(False) | (users.c.id == caller_id)
232+
233+
234+
def visible_user_profile_cols(caller_id: int):
235+
"""Returns user profile columns with visibility constraints applied based on privacy settings."""
236+
return (
237+
sa.case(
238+
(
239+
is_private(users.c.privacy_hide_email, caller_id),
240+
None,
241+
),
242+
else_=users.c.email,
243+
).label("email"),
244+
sa.case(
245+
(
246+
is_private(users.c.privacy_hide_fullname, caller_id),
247+
None,
248+
),
249+
else_=users.c.first_name,
250+
).label("first_name"),
251+
sa.case(
252+
(
253+
is_private(users.c.privacy_hide_fullname, caller_id),
254+
None,
255+
),
256+
else_=users.c.last_name,
257+
).label("last_name"),
258+
)

services/web/server/src/simcore_service_webserver/groups/_groups_repository.py

Lines changed: 2 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
pass_or_acquire_connection,
2626
transaction_context,
2727
)
28-
from simcore_postgres_database.utils_users import is_private, is_public
28+
from simcore_postgres_database.utils_users import is_public, visible_user_profile_cols
2929
from sqlalchemy import and_
3030
from sqlalchemy.dialects.postgresql import insert
3131
from sqlalchemy.engine.row import Row
@@ -443,28 +443,7 @@ def _group_user_cols(caller_id: UserID):
443443
return (
444444
users.c.id,
445445
users.c.name,
446-
# privacy settings
447-
sa.case(
448-
(
449-
is_private(users.c.privacy_hide_email, caller_id),
450-
None,
451-
),
452-
else_=users.c.email,
453-
).label("email"),
454-
sa.case(
455-
(
456-
is_private(users.c.privacy_hide_fullname, caller_id),
457-
None,
458-
),
459-
else_=users.c.first_name,
460-
).label("first_name"),
461-
sa.case(
462-
(
463-
is_private(users.c.privacy_hide_fullname, caller_id),
464-
None,
465-
),
466-
else_=users.c.last_name,
467-
).label("last_name"),
446+
*visible_user_profile_cols(caller_id),
468447
users.c.primary_gid,
469448
)
470449

services/web/server/src/simcore_service_webserver/users/_users_repository.py

Lines changed: 2 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@
3030
from simcore_postgres_database.utils_users import (
3131
UsersRepo,
3232
generate_alternative_username,
33-
is_private,
3433
is_public,
34+
visible_user_profile_cols,
3535
)
3636
from sqlalchemy import delete
3737
from sqlalchemy.engine.row import Row
@@ -59,28 +59,7 @@ def _public_user_cols(caller_id: int):
5959
# Fits PublicUser model
6060
users.c.id.label("user_id"),
6161
users.c.name.label("user_name"),
62-
# privacy settings
63-
sa.case(
64-
(
65-
is_private(users.c.privacy_hide_email, caller_id),
66-
None,
67-
),
68-
else_=users.c.email,
69-
).label("email"),
70-
sa.case(
71-
(
72-
is_private(users.c.privacy_hide_fullname, caller_id),
73-
None,
74-
),
75-
else_=users.c.first_name,
76-
).label("first_name"),
77-
sa.case(
78-
(
79-
is_private(users.c.privacy_hide_fullname, caller_id),
80-
None,
81-
),
82-
else_=users.c.last_name,
83-
).label("last_name"),
62+
*visible_user_profile_cols(caller_id),
8463
users.c.primary_gid.label("group_id"),
8564
)
8665

0 commit comments

Comments
 (0)