-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #52 from grillazz/46-switch-to-python-311
46 switch to python 311
- Loading branch information
Showing
13 changed files
with
673 additions
and
554 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
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 |
---|---|---|
@@ -1,30 +1,40 @@ | ||
from bson import ObjectId | ||
from bson import ObjectId as _ObjectId | ||
from bson.errors import InvalidId | ||
from pydantic import BaseModel, Field | ||
from pydantic import BaseModel, Field, ConfigDict, AfterValidator | ||
from typing_extensions import Annotated | ||
|
||
|
||
class ObjectIdField(str): | ||
@classmethod | ||
def __get_validators__(cls): | ||
yield cls.validate | ||
# class ObjectIdField(str): | ||
# @classmethod | ||
# def __get_validators__(cls): | ||
# yield cls.validate | ||
# | ||
# @classmethod | ||
# def validate(cls, value): | ||
# try: | ||
# return ObjectId(str(value)) | ||
# except InvalidId as e: | ||
# raise ValueError("Not a valid ObjectId") from e | ||
|
||
@classmethod | ||
def validate(cls, value): | ||
try: | ||
return ObjectId(str(value)) | ||
except InvalidId: | ||
raise ValueError("Not a valid ObjectId") | ||
|
||
def check_object_id(value: str) -> str: | ||
if not _ObjectId.is_valid(value): | ||
raise ValueError('Invalid ObjectId') | ||
return value | ||
|
||
|
||
ObjectIdField = Annotated[str, AfterValidator(check_object_id)] | ||
|
||
|
||
config = ConfigDict(arbitrary_types_allowed=True, populate_by_name=True) | ||
|
||
|
||
class Document(BaseModel): | ||
# model_config = config | ||
|
||
name: str = Field(...) | ||
desc: str = Field(...) | ||
|
||
class Config: | ||
allow_population_by_field_name = True | ||
arbitrary_types_allowed = True | ||
json_encoders = {ObjectId: str} | ||
|
||
|
||
class DocumentResponse(Document): | ||
id: ObjectIdField = Field(...) | ||
id: ObjectIdField |
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.