-
Notifications
You must be signed in to change notification settings - Fork 94
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use Config class to configure models
- Loading branch information
Showing
14 changed files
with
280 additions
and
62 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
from datetime import datetime | ||
|
||
from odmantic.bson import BSON_TYPES_ENCODERS, BaseBSONModel, ObjectId | ||
|
||
|
||
class M(BaseBSONModel): | ||
id: ObjectId | ||
date: datetime | ||
|
||
class Config: | ||
json_encoders = { | ||
**BSON_TYPES_ENCODERS, | ||
datetime: lambda dt: dt.year, | ||
} | ||
|
||
|
||
print(M(id=ObjectId(), date=datetime.utcnow()).json()) | ||
#> {"id": "5fa3378c8fde3766574d874d", "date": 2020} |
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,55 @@ | ||
from typing import Any, Dict, Optional, Type | ||
|
||
from pydantic.main import BaseConfig | ||
from pydantic.typing import AnyCallable | ||
|
||
from odmantic.bson import BSON_TYPES_ENCODERS | ||
from odmantic.utils import is_dunder | ||
|
||
|
||
class BaseODMConfig: | ||
"""Base class of the Config defined in the Models""" | ||
|
||
collection: Optional[str] = None | ||
|
||
# Inherited from pydantic | ||
title: Optional[str] = None | ||
json_encoders: Dict[Type[Any], AnyCallable] = {} | ||
|
||
|
||
ALLOWED_CONFIG_OPTIONS = {name for name in dir(BaseODMConfig) if not is_dunder(name)} | ||
|
||
|
||
class EnforcedPydanticConfig: | ||
"""Configuration options enforced to work with Models""" | ||
|
||
validate_all = True | ||
validate_assignment = True | ||
|
||
|
||
def validate_config( | ||
cls_config: Type[BaseODMConfig], cls_name: str | ||
) -> Type[BaseODMConfig]: | ||
"""Validate and build the model configuration""" | ||
for name in dir(cls_config): | ||
if not is_dunder(name) and name not in ALLOWED_CONFIG_OPTIONS: | ||
raise ValueError(f"'{cls_name}': 'Config.{name}' is not supported") | ||
|
||
if cls_config is BaseODMConfig: | ||
bases = (EnforcedPydanticConfig, BaseODMConfig, BaseConfig) | ||
else: | ||
bases = ( | ||
EnforcedPydanticConfig, | ||
cls_config, | ||
BaseODMConfig, | ||
BaseConfig, | ||
) # type:ignore | ||
|
||
# Merge json_encoders to preserve bson type encoders | ||
namespace = { | ||
"json_encoders": { | ||
**BSON_TYPES_ENCODERS, | ||
**getattr(cls_config, "json_encoders", {}), | ||
} | ||
} | ||
return type("Config", bases, namespace) |
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.