-
Notifications
You must be signed in to change notification settings - Fork 219
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 ab2a2b6
Showing
17 changed files
with
304 additions
and
15 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
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
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,14 @@ | ||
from typing import Any, Type, Union, TYPE_CHECKING | ||
|
||
from pydantic import BaseModel | ||
|
||
if TYPE_CHECKING: | ||
from beanie.odm.documents import Document | ||
|
||
|
||
def parse_obj( | ||
model: Union[Type[BaseModel], Type["Document"]], data: Any | ||
) -> BaseModel: | ||
if hasattr(model, "_parse_obj_saving_state"): | ||
return model._parse_obj_saving_state(data) # type: ignore | ||
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,44 @@ | ||
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 | ||
|
||
|
||
def save_state_after(f: Callable): | ||
@wraps(f) | ||
async def wrapper(self: "DocType", *args, **kwargs): | ||
result = await f(self, *args, **kwargs) | ||
if self.use_state_management(): | ||
self._save_state() | ||
return result | ||
|
||
return wrapper |
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 |
---|---|---|
@@ -1,6 +1,6 @@ | ||
[tool.poetry] | ||
name = "beanie" | ||
version = "1.3.0" | ||
version = "1.4.0" | ||
description = "Asynchronous Python ODM for MongoDB" | ||
authors = ["Roman <[email protected]>"] | ||
license = "Apache-2.0" | ||
|
@@ -14,7 +14,7 @@ readme = "README.md" | |
|
||
[tool.poetry.dependencies] | ||
python = ">=3.6.1,<4.0" | ||
pydantic = "^1.5" | ||
pydantic = "^1.7" | ||
motor = "^2.5" | ||
click = "^7.1.2" | ||
toml = "^0.10.2" | ||
|
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
Oops, something went wrong.