-
-
Notifications
You must be signed in to change notification settings - Fork 193
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
9dce295
commit 8615709
Showing
8 changed files
with
115 additions
and
8 deletions.
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
Empty file.
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
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
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,9 @@ | ||
from typing import Any, Type | ||
|
||
from pydantic import BaseModel | ||
|
||
|
||
def parse_obj(model: Type[BaseModel], data: Any) -> BaseModel: | ||
if hasattr(model, "_parse_obj_saving_state"): | ||
return model._parse_obj_saving_state(data) | ||
return model.parse_obj(data) |
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,33 @@ | ||
import inspect | ||
from functools import wraps | ||
from typing import Callable, TYPE_CHECKING | ||
|
||
from beanie.exceptions import StateManagementIsTurnedOff, StateNotSaved | ||
|
||
if TYPE_CHECKING: | ||
from beanie.odm.documents import DocType | ||
|
||
|
||
def check_if_state_saved(self: "DocType"): | ||
if not self.use_state_management(): | ||
raise StateManagementIsTurnedOff( | ||
"State management is turned off for this document" | ||
) | ||
if self._saved_state is None: | ||
raise StateNotSaved("No state was saved") | ||
|
||
|
||
def saved_state_needed(f: Callable): | ||
@wraps(f) | ||
def sync_wrapper(self: "DocType", *args, **kwargs): | ||
check_if_state_saved(self) | ||
return f(self, *args, **kwargs) | ||
|
||
@wraps(f) | ||
async def async_wrapper(self: "DocType", *args, **kwargs): | ||
check_if_state_saved(self) | ||
return await f(self, *args, **kwargs) | ||
|
||
if inspect.iscoroutinefunction(f): | ||
return async_wrapper | ||
return sync_wrapper |