Skip to content

Commit 1874890

Browse files
committed
drafts
1 parent fa54398 commit 1874890

File tree

3 files changed

+91
-9
lines changed

3 files changed

+91
-9
lines changed

packages/models-library/src/models_library/api_schemas_webserver/users.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -222,8 +222,8 @@ class UserGet(OutputSchema):
222222
email: EmailStr | None = None
223223

224224
@classmethod
225-
def from_model(cls, args):
226-
...
225+
def from_model(cls, data):
226+
return cls.model_validate(data, from_attributes=True)
227227

228228

229229
class UsersForAdminSearchQueryParams(RequestParameters):

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

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,82 @@ def _parse_as_user(user_id: Any) -> UserID:
5252
raise UserNotFoundError(uid=user_id, user_id=user_id) from err
5353

5454

55+
def _public_user_cols(caller_id: UserID):
56+
return (
57+
# Fits PublicUser model
58+
users.c.id.label("user_id"),
59+
users.c.name.label("user_name"),
60+
# privacy settings
61+
sa.case(
62+
(
63+
users.c.privacy_hide_email.is_(True) & (users.c.id != caller_id),
64+
None,
65+
),
66+
else_=users.c.email,
67+
).label("email"),
68+
sa.case(
69+
(
70+
users.c.privacy_hide_fullname.is_(True) & (users.c.id != caller_id),
71+
None,
72+
),
73+
else_=users.c.first_name,
74+
).label("first_name"),
75+
sa.case(
76+
(
77+
users.c.privacy_hide_fullname.is_(True) & (users.c.id != caller_id),
78+
None,
79+
),
80+
else_=users.c.last_name,
81+
).label("last_name"),
82+
users.c.primary_gid.label("group_id"),
83+
)
84+
85+
86+
async def get_public_user(
87+
engine: AsyncEngine,
88+
connection: AsyncConnection | None = None,
89+
*,
90+
caller_id: UserID,
91+
user_id: UserID,
92+
):
93+
query = sa.select(*_public_user_cols(caller_id=caller_id)).where(
94+
users.c.id == user_id
95+
)
96+
97+
async with pass_or_acquire_connection(engine, connection) as conn:
98+
result = await conn.execute(query)
99+
user = result.first()
100+
if not user:
101+
raise UserNotFoundError(uid=user_id)
102+
return user
103+
104+
105+
async def search_public_user(
106+
engine: AsyncEngine,
107+
connection: AsyncConnection | None = None,
108+
*,
109+
caller_id: UserID,
110+
search_pattern: str,
111+
limit: int,
112+
) -> list:
113+
114+
pattern_ = f"%{search_pattern}%"
115+
is_public_email = users.c.privacy_hide_email.is_(False) | (users.c.id != caller_id)
116+
117+
query = (
118+
sa.select(*_public_user_cols(caller_id=caller_id))
119+
.where(
120+
users.c.name.ilike(pattern_)
121+
| (is_public_email & users.c.users.c.email.ilike(pattern_))
122+
)
123+
.limit(limit)
124+
)
125+
126+
async with pass_or_acquire_connection(engine, connection) as conn:
127+
result = await conn.stream(query)
128+
return [got async for got in result]
129+
130+
55131
async def get_user_or_raise(
56132
engine: AsyncEngine,
57133
connection: AsyncConnection | None = None,

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

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -87,17 +87,23 @@ async def pre_register_user(
8787
#
8888

8989

90-
async def get_public_user(
91-
app: web.Application, *, caller_id: UserID, user_id: UserID
92-
) -> dict[str, Any]:
93-
...
90+
async def get_public_user(app: web.Application, *, caller_id: UserID, user_id: UserID):
91+
return await _users_repository.get_public_user(
92+
get_asyncpg_engine(app),
93+
caller_id=caller_id,
94+
user_id=user_id,
95+
)
9496

9597

9698
async def search_public_users(
9799
app: web.Application, *, caller_id: UserID, match_: str, limit: int
98-
) -> list[dict[str, Any]]:
99-
100-
...
100+
) -> list:
101+
return await _users_repository.search_public_user(
102+
get_asyncpg_engine(app),
103+
caller_id=caller_id,
104+
search_pattern=match_,
105+
limit=limit,
106+
)
101107

102108

103109
async def get_user(app: web.Application, user_id: UserID) -> dict[str, Any]:

0 commit comments

Comments
 (0)