1
1
import datetime
2
2
import logging
3
3
import warnings
4
+ from collections .abc import Callable
4
5
from dataclasses import dataclass , fields
5
- from typing import Any , Callable
6
+ from typing import Any
6
7
7
8
import sqlalchemy as sa
8
- from aiopg .sa .connection import SAConnection
9
- from aiopg .sa .result import RowProxy
9
+ from common_library .async_tools import maybe_await
10
10
from sqlalchemy .ext .asyncio import AsyncConnection , AsyncEngine
11
11
12
+ from ._protocols import DBConnection
12
13
from .models .groups import GroupType , groups , user_to_groups
13
14
from .models .groups_extra_properties import groups_extra_properties
14
15
from .utils_models import FromRowMixin
22
23
)
23
24
24
25
25
- class GroupExtraPropertiesError (Exception ):
26
- ...
26
+ class GroupExtraPropertiesError (Exception ): ...
27
27
28
28
29
- class GroupExtraPropertiesNotFoundError (GroupExtraPropertiesError ):
30
- ...
29
+ class GroupExtraPropertiesNotFoundError (GroupExtraPropertiesError ): ...
31
30
32
31
33
32
@dataclass (frozen = True , slots = True , kw_only = True )
@@ -99,24 +98,6 @@ def _get_stmt(gid: int, product_name: str):
99
98
& (groups_extra_properties .c .product_name == product_name )
100
99
)
101
100
102
- @staticmethod
103
- async def get (
104
- connection : SAConnection , * , gid : int , product_name : str
105
- ) -> GroupExtraProperties :
106
- warnings .warn (
107
- _WARNING_FMSG .format ("get" , "get_v2" ),
108
- DeprecationWarning ,
109
- stacklevel = 1 ,
110
- )
111
-
112
- query = GroupExtraPropertiesRepo ._get_stmt (gid , product_name )
113
- result = await connection .execute (query )
114
- assert result # nosec
115
- if row := await result .first ():
116
- return GroupExtraProperties .from_row_proxy (row )
117
- msg = f"Properties for group { gid } not found"
118
- raise GroupExtraPropertiesNotFoundError (msg )
119
-
120
101
@staticmethod
121
102
async def get_v2 (
122
103
engine : AsyncEngine ,
@@ -174,7 +155,7 @@ def _aggregate(
174
155
175
156
@staticmethod
176
157
async def get_aggregated_properties_for_user (
177
- connection : SAConnection ,
158
+ connection : DBConnection ,
178
159
* ,
179
160
user_id : int ,
180
161
product_name : str ,
@@ -197,30 +178,9 @@ async def get_aggregated_properties_for_user(
197
178
)
198
179
assert result # nosec
199
180
200
- rows : list [ RowProxy ] | None = await result .fetchall ()
201
- assert rows is not None # nosec
181
+ rows = await maybe_await ( result .fetchall () )
182
+ assert isinstance ( rows , list ) # nosec
202
183
203
184
return GroupExtraPropertiesRepo ._aggregate (
204
- rows , user_id , product_name , GroupExtraProperties .from_row_proxy
185
+ rows , user_id , product_name , GroupExtraProperties .from_row
205
186
)
206
-
207
- @staticmethod
208
- async def get_aggregated_properties_for_user_v2 (
209
- engine : AsyncEngine ,
210
- connection : AsyncConnection | None = None ,
211
- * ,
212
- user_id : int ,
213
- product_name : str ,
214
- ) -> GroupExtraProperties :
215
- async with pass_or_acquire_connection (engine , connection ) as conn :
216
-
217
- list_stmt = _list_table_entries_ordered_by_group_type_stmt (
218
- user_id = user_id , product_name = product_name
219
- )
220
- result = await conn .stream (
221
- sa .select (list_stmt ).order_by (list_stmt .c .type_order )
222
- )
223
- rows = [row async for row in result ]
224
- return GroupExtraPropertiesRepo ._aggregate (
225
- rows , user_id , product_name , GroupExtraProperties .from_row
226
- )
0 commit comments