-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2c7d311
commit 73a5810
Showing
4 changed files
with
33 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
from __future__ import annotations | ||
|
||
from . import base, mixins, session | ||
from .current_user_context_manager import CurrentUserContextManager | ||
from .db_context_manager import DBContextManager | ||
from .db_context_switcher import DBContextSwitcher |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
from __future__ import annotations | ||
|
||
from contextlib import contextmanager | ||
from typing import TYPE_CHECKING, Generator | ||
|
||
from kwik.database.context_vars import current_user_ctx_var | ||
|
||
if TYPE_CHECKING: | ||
import kwik.models | ||
|
||
|
||
@contextmanager | ||
def CurrentUserContextManager(user: kwik.models.User) -> Generator[None, None, None]: | ||
""" | ||
Context manager for the current user. | ||
Automatically manages the current user for the CRUD operations. | ||
""" | ||
|
||
token = current_user_ctx_var.set(user) | ||
try: | ||
yield | ||
finally: | ||
current_user_ctx_var.reset(token) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
from __future__ import annotations | ||
|
||
import enum | ||
|
||
|
||
def merge_str_enum(name: str, *args): | ||
return enum.StrEnum(name, [(e, e) for arg in args for e in arg]) |