Skip to content

Commit a389e28

Browse files
committed
Small updates
1 parent f03db4e commit a389e28

26 files changed

Lines changed: 173 additions & 192 deletions

File tree

config/toml_config_manager.py

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -38,19 +38,26 @@ def validate_logging_level(*, level: str) -> LoggingLevel:
3838
raise ValueError(f"Invalid log level: '{level}'.") from err
3939

4040

41-
def configure_logging(*, level: LoggingLevel = DEFAULT_LOG_LEVEL) -> None:
42-
logging.getLogger().handlers.clear()
43-
41+
FMT: Final[str] = (
42+
"[%(asctime)s.%(msecs)03d] "
43+
"[%(threadName)s] "
44+
"%(funcName)20s "
45+
"%(module)s:%(lineno)d "
46+
"%(levelname)-8s - "
47+
"%(message)s"
48+
)
49+
DATEFMT: Final[str] = "%Y-%m-%d %H:%M:%S"
50+
51+
52+
def configure_logging(
53+
*,
54+
level: LoggingLevel = DEFAULT_LOG_LEVEL,
55+
) -> None:
4456
logging.basicConfig(
45-
level=getattr(logging, level),
46-
datefmt="%Y-%m-%d %H:%M:%S",
47-
format=(
48-
"[%(asctime)s.%(msecs)03d] "
49-
"%(funcName)20s "
50-
"%(module)s:%(lineno)d "
51-
"%(levelname)-8s - "
52-
"%(message)s"
53-
),
57+
level=level,
58+
datefmt=DATEFMT,
59+
format=FMT,
60+
force=True,
5461
)
5562

5663

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,32 @@
11
from abc import abstractmethod
2-
from typing import Protocol
2+
from typing import Protocol, TypedDict
3+
from uuid import UUID
34

4-
from app.application.common.query_models.user import UserQueryModel
5-
from app.application.common.query_params.user import UserListParams
5+
from app.application.common.query_params.offset_pagination import OffsetPaginationParams
6+
from app.application.common.query_params.sorting import SortingParams
7+
from app.domain.enums.user_role import UserRole
8+
9+
10+
class UserQueryModel(TypedDict):
11+
id_: UUID
12+
username: str
13+
role: UserRole
14+
is_active: bool
15+
16+
17+
class ListUsersQM(TypedDict):
18+
users: list[UserQueryModel]
19+
total: int
620

721

822
class UserQueryGateway(Protocol):
923
@abstractmethod
1024
async def read_all(
1125
self,
12-
user_read_all_params: UserListParams,
13-
) -> list[UserQueryModel] | None:
14-
""":raises ReaderError:"""
26+
pagination: OffsetPaginationParams,
27+
sorting: SortingParams,
28+
) -> ListUsersQM:
29+
"""
30+
:raises SortingError:
31+
:raises ReaderError:
32+
"""

src/app/application/common/query_models/__init__.py

Whitespace-only changes.

src/app/application/common/query_models/user.py

Lines changed: 0 additions & 11 deletions
This file was deleted.

src/app/application/common/query_params/pagination.py renamed to src/app/application/common/query_params/offset_pagination.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55

66
@dataclass(frozen=True, slots=True, kw_only=True)
7-
class Pagination:
7+
class OffsetPaginationParams:
88
"""
99
raises PaginationError
1010
"""
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
1+
from dataclasses import dataclass
12
from enum import StrEnum
23

34

45
class SortingOrder(StrEnum):
56
ASC = "ASC"
67
DESC = "DESC"
8+
9+
10+
@dataclass(frozen=True, slots=True, kw_only=True)
11+
class SortingParams:
12+
field: str
13+
order: SortingOrder

src/app/application/common/query_params/user.py

Lines changed: 0 additions & 16 deletions
This file was deleted.
Lines changed: 18 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,12 @@
11
import logging
22
from dataclasses import dataclass
3-
from typing import TypedDict
43

5-
from app.application.common.exceptions.query import SortingError
6-
from app.application.common.ports.user_query_gateway import UserQueryGateway
7-
from app.application.common.query_models.user import UserQueryModel
8-
from app.application.common.query_params.pagination import Pagination
9-
from app.application.common.query_params.sorting import SortingOrder
10-
from app.application.common.query_params.user import (
11-
UserListParams,
12-
UserListSorting,
4+
from app.application.common.ports.user_query_gateway import (
5+
ListUsersQM,
6+
UserQueryGateway,
137
)
8+
from app.application.common.query_params.offset_pagination import OffsetPaginationParams
9+
from app.application.common.query_params.sorting import SortingOrder, SortingParams
1410
from app.application.common.services.authorization.authorize import (
1511
authorize,
1612
)
@@ -26,16 +22,12 @@
2622

2723
@dataclass(frozen=True, slots=True, kw_only=True)
2824
class ListUsersRequest:
29-
limit: int
3025
offset: int
26+
limit: int
3127
sorting_field: str
3228
sorting_order: SortingOrder
3329

3430

35-
class ListUsersResponse(TypedDict):
36-
users: list[UserQueryModel]
37-
38-
3931
class ListUsersQueryService:
4032
"""
4133
- Open to admins.
@@ -50,14 +42,14 @@ def __init__(
5042
self._current_user_service = current_user_service
5143
self._user_query_gateway = user_query_gateway
5244

53-
async def execute(self, request_data: ListUsersRequest) -> ListUsersResponse:
45+
async def execute(self, request_data: ListUsersRequest) -> ListUsersQM:
5446
"""
5547
:raises AuthenticationError:
5648
:raises DataMapperError:
5749
:raises AuthorizationError:
58-
:raises ReaderError:
5950
:raises PaginationError:
6051
:raises SortingError:
52+
:raises ReaderError:
6153
"""
6254
log.info("List users: started.")
6355

@@ -72,28 +64,18 @@ async def execute(self, request_data: ListUsersRequest) -> ListUsersResponse:
7264
)
7365

7466
log.debug("Retrieving list of users.")
75-
user_list_params = UserListParams(
76-
pagination=Pagination(
77-
limit=request_data.limit,
78-
offset=request_data.offset,
79-
),
80-
sorting=UserListSorting(
81-
sorting_field=request_data.sorting_field,
82-
sorting_order=request_data.sorting_order,
83-
),
67+
pagination = OffsetPaginationParams(
68+
limit=request_data.limit,
69+
offset=request_data.offset,
8470
)
85-
86-
users: list[UserQueryModel] | None = await self._user_query_gateway.read_all(
87-
user_list_params,
71+
sorting = SortingParams(
72+
field=request_data.sorting_field,
73+
order=request_data.sorting_order,
74+
)
75+
response = await self._user_query_gateway.read_all(
76+
pagination=pagination,
77+
sorting=sorting,
8878
)
89-
if users is None:
90-
log.error(
91-
"Retrieving list of users failed: invalid sorting column '%s'.",
92-
request_data.sorting_field,
93-
)
94-
raise SortingError("Invalid sorting field.")
95-
96-
response = ListUsersResponse(users=users)
9779

9880
log.info("List users: done.")
9981
return response

src/app/domain/entities/base.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,3 +42,6 @@ def __hash__(self) -> int:
4242
reduces the risk of hash collisions between different entity types.
4343
"""
4444
return hash((type(self), self.id_))
45+
46+
def __repr__(self) -> str:
47+
return f"{type(self).__name__}(id_={self.id_!r})"

src/app/infrastructure/adapters/main_flusher_sqla.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
DB_FLUSH_FAILED,
1313
DB_QUERY_FAILED,
1414
)
15-
from app.infrastructure.adapters.types import MainAsyncSession
15+
from app.infrastructure.adapters.types_ import MainAsyncSession
1616
from app.infrastructure.exceptions.gateway import DataMapperError
1717

1818
log = logging.getLogger(__name__)

0 commit comments

Comments
 (0)