Skip to content

Commit a1cf565

Browse files
committed
remove aiopg
1 parent b79fe29 commit a1cf565

File tree

4 files changed

+21
-149
lines changed

4 files changed

+21
-149
lines changed

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

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -182,25 +182,5 @@ async def get_aggregated_properties_for_user(
182182
assert isinstance(rows, list) # nosec
183183

184184
return GroupExtraPropertiesRepo._aggregate(
185-
rows, user_id, product_name, GroupExtraProperties.from_row_proxy
185+
rows, user_id, product_name, GroupExtraProperties.from_row
186186
)
187-
188-
@staticmethod
189-
async def get_aggregated_properties_for_user_v2(
190-
engine: AsyncEngine,
191-
connection: AsyncConnection | None = None,
192-
*,
193-
user_id: int,
194-
product_name: str,
195-
) -> GroupExtraProperties:
196-
async with pass_or_acquire_connection(engine, connection) as conn:
197-
list_stmt = _list_table_entries_ordered_by_group_type_stmt(
198-
user_id=user_id, product_name=product_name
199-
)
200-
result = await conn.stream(
201-
sa.select(list_stmt).order_by(list_stmt.c.type_order)
202-
)
203-
rows = [row async for row in result]
204-
return GroupExtraPropertiesRepo._aggregate(
205-
rows, user_id, product_name, GroupExtraProperties.from_row
206-
)
Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,30 @@
1+
from collections.abc import Mapping
12
from dataclasses import fields, is_dataclass
2-
from typing import TypeVar
3+
from typing import Any, TypeVar
34

4-
from aiopg.sa.result import RowProxy
55
from sqlalchemy.engine.row import Row
66

77
ModelType = TypeVar("ModelType")
88

99

1010
class FromRowMixin:
11-
"""Mixin to allow instance construction from aiopg.sa.result.RowProxy"""
11+
"""Mixin to allow instance construction from database row objects"""
1212

1313
@classmethod
14-
def from_row_proxy(cls: type[ModelType], row: RowProxy) -> ModelType:
15-
assert is_dataclass(cls) # nosec
16-
field_names = [f.name for f in fields(cls)]
17-
return cls(**{k: v for k, v in row.items() if k in field_names}) # type: ignore[return-value]
14+
def from_row(cls: type[ModelType], row: Any) -> ModelType:
15+
"""Creates an instance from a database row.
1816
19-
@classmethod
20-
def from_row(cls: type[ModelType], row: Row) -> ModelType:
17+
Supports both Row objects and mapping-like objects.
18+
"""
2119
assert is_dataclass(cls) # nosec
20+
21+
if isinstance(row, Row):
22+
mapping = row._asdict()
23+
elif isinstance(row, Mapping):
24+
mapping = row
25+
else:
26+
msg = f"Row must be a Row or Mapping type, got {type(row)}"
27+
raise TypeError(msg)
28+
2229
field_names = [f.name for f in fields(cls)]
23-
return cls(**{k: v for k, v in row._asdict().items() if k in field_names}) # type: ignore[return-value]
30+
return cls(**{k: v for k, v in mapping.items() if k in field_names}) # type: ignore[return-value]

packages/postgres-database/tests/test_utils_groups_extra_properties.py

Lines changed: 1 addition & 113 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ async def _creator(
6969
assert result
7070
row = await result.first()
7171
assert row
72-
properties = GroupExtraProperties.from_row_proxy(row)
72+
properties = GroupExtraProperties.from_row(row)
7373
created_properties.append((properties.group_id, properties.product_name))
7474
return properties
7575

@@ -365,115 +365,3 @@ async def test_get_aggregated_properties_for_user_returns_property_values_as_tru
365365
assert aggregated_group_properties.internet_access is False
366366
assert aggregated_group_properties.override_services_specifications is False
367367
assert aggregated_group_properties.use_on_demand_clusters is True
368-
369-
370-
async def test_get_aggregated_properties_for_user_returns_property_values_as_truthy_if_one_of_them_is_v2(
371-
asyncpg_engine: AsyncEngine,
372-
connection: aiopg.sa.connection.SAConnection,
373-
connection_factory: aiopg.sa.connection.SAConnection | AsyncConnection,
374-
product_name: str,
375-
registered_user: RowProxy,
376-
create_fake_product: Callable[[str], Awaitable[Row]],
377-
create_fake_group: Callable[..., Awaitable[RowProxy]],
378-
create_fake_group_extra_properties: Callable[..., Awaitable[GroupExtraProperties]],
379-
everyone_group_id: int,
380-
):
381-
await create_fake_product(product_name)
382-
await create_fake_product(f"{product_name}_additional_just_for_fun")
383-
384-
# create a specific extra properties for group that disallow everything
385-
everyone_group_extra_properties = await create_fake_group_extra_properties(
386-
everyone_group_id,
387-
product_name,
388-
internet_access=False,
389-
override_services_specifications=False,
390-
use_on_demand_clusters=False,
391-
)
392-
# this should return the everyone group properties
393-
aggregated_group_properties = (
394-
await GroupExtraPropertiesRepo.get_aggregated_properties_for_user_v2(
395-
asyncpg_engine, user_id=registered_user.id, product_name=product_name
396-
)
397-
)
398-
assert aggregated_group_properties == everyone_group_extra_properties
399-
400-
# now we create some standard groups and add the user to them and make everything false for now
401-
standard_groups = [await create_fake_group(connection) for _ in range(5)]
402-
for group in standard_groups:
403-
await create_fake_group_extra_properties(
404-
group.gid,
405-
product_name,
406-
internet_access=False,
407-
override_services_specifications=False,
408-
use_on_demand_clusters=False,
409-
)
410-
await _add_user_to_group(
411-
connection_factory, user_id=registered_user.id, group_id=group.gid
412-
)
413-
414-
# now we still should not have any of these value Truthy
415-
aggregated_group_properties = (
416-
await GroupExtraPropertiesRepo.get_aggregated_properties_for_user_v2(
417-
asyncpg_engine, user_id=registered_user.id, product_name=product_name
418-
)
419-
)
420-
assert aggregated_group_properties.internet_access is False
421-
assert aggregated_group_properties.override_services_specifications is False
422-
assert aggregated_group_properties.use_on_demand_clusters is False
423-
424-
# let's change one of these standard groups
425-
random_standard_group = random.choice(standard_groups) # noqa: S311
426-
result = await connection.execute(
427-
groups_extra_properties.update()
428-
.where(groups_extra_properties.c.group_id == random_standard_group.gid)
429-
.values(internet_access=True)
430-
)
431-
assert result.rowcount == 1
432-
433-
# now we should have internet access
434-
aggregated_group_properties = (
435-
await GroupExtraPropertiesRepo.get_aggregated_properties_for_user_v2(
436-
asyncpg_engine, user_id=registered_user.id, product_name=product_name
437-
)
438-
)
439-
assert aggregated_group_properties.internet_access is True
440-
assert aggregated_group_properties.override_services_specifications is False
441-
assert aggregated_group_properties.use_on_demand_clusters is False
442-
443-
# let's change another one of these standard groups
444-
random_standard_group = random.choice(standard_groups) # noqa: S311
445-
result = await connection.execute(
446-
groups_extra_properties.update()
447-
.where(groups_extra_properties.c.group_id == random_standard_group.gid)
448-
.values(override_services_specifications=True)
449-
)
450-
assert result.rowcount == 1
451-
452-
# now we should have internet access and service override
453-
aggregated_group_properties = (
454-
await GroupExtraPropertiesRepo.get_aggregated_properties_for_user_v2(
455-
asyncpg_engine, user_id=registered_user.id, product_name=product_name
456-
)
457-
)
458-
assert aggregated_group_properties.internet_access is True
459-
assert aggregated_group_properties.override_services_specifications is True
460-
assert aggregated_group_properties.use_on_demand_clusters is False
461-
462-
# and we can deny it again by setting a primary extra property
463-
# now create some personal extra properties
464-
personal_group_extra_properties = await create_fake_group_extra_properties(
465-
registered_user.primary_gid,
466-
product_name,
467-
internet_access=False,
468-
use_on_demand_clusters=True,
469-
)
470-
assert personal_group_extra_properties
471-
472-
aggregated_group_properties = (
473-
await GroupExtraPropertiesRepo.get_aggregated_properties_for_user_v2(
474-
asyncpg_engine, user_id=registered_user.id, product_name=product_name
475-
)
476-
)
477-
assert aggregated_group_properties.internet_access is False
478-
assert aggregated_group_properties.override_services_specifications is False
479-
assert aggregated_group_properties.use_on_demand_clusters is True

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

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,6 @@ async def search_public_user(
9696
search_pattern: str,
9797
limit: int,
9898
) -> list:
99-
10099
_pattern = f"%{search_pattern}%"
101100

102101
query = (
@@ -267,8 +266,8 @@ async def list_user_permissions(
267266
with contextlib.suppress(GroupExtraPropertiesNotFoundError):
268267
async with pass_or_acquire_connection(engine, connection) as conn:
269268
user_group_extra_properties = (
270-
await GroupExtraPropertiesRepo.get_aggregated_properties_for_user_v2(
271-
engine, conn, user_id=user_id, product_name=product_name
269+
await GroupExtraPropertiesRepo.get_aggregated_properties_for_user(
270+
conn, user_id=user_id, product_name=product_name
272271
)
273272
)
274273
override_services_specifications.allowed = (
@@ -321,7 +320,6 @@ async def search_users_and_get_profile(
321320
*,
322321
email_like: str,
323322
) -> list[Row]:
324-
325323
users_alias = sa.alias(users, name="users_alias")
326324

327325
invited_by = (
@@ -549,7 +547,6 @@ async def update_user_profile(
549547

550548
if updated_values := update.to_db():
551549
try:
552-
553550
async with transaction_context(engine=get_asyncpg_engine(app)) as conn:
554551
await conn.execute(
555552
users.update()

0 commit comments

Comments
 (0)