auth service completed and tested
This commit is contained in:
@@ -11,7 +11,7 @@ from ApiLibrary import system_arrow
|
||||
|
||||
class PyObjectId(ObjectId):
|
||||
"""Custom type for handling MongoDB ObjectId in Pydantic models."""
|
||||
|
||||
|
||||
@classmethod
|
||||
def __get_pydantic_core_schema__(
|
||||
cls,
|
||||
@@ -21,17 +21,21 @@ class PyObjectId(ObjectId):
|
||||
"""Define the core schema for PyObjectId."""
|
||||
return core_schema.json_or_python_schema(
|
||||
json_schema=core_schema.str_schema(),
|
||||
python_schema=core_schema.union_schema([
|
||||
core_schema.is_instance_schema(ObjectId),
|
||||
core_schema.chain_schema([
|
||||
core_schema.str_schema(),
|
||||
core_schema.no_info_plain_validator_function(cls.validate),
|
||||
]),
|
||||
]),
|
||||
python_schema=core_schema.union_schema(
|
||||
[
|
||||
core_schema.is_instance_schema(ObjectId),
|
||||
core_schema.chain_schema(
|
||||
[
|
||||
core_schema.str_schema(),
|
||||
core_schema.no_info_plain_validator_function(cls.validate),
|
||||
]
|
||||
),
|
||||
]
|
||||
),
|
||||
serialization=core_schema.plain_serializer_function_ser_schema(
|
||||
lambda x: str(x),
|
||||
return_schema=core_schema.str_schema(),
|
||||
when_used='json',
|
||||
when_used="json",
|
||||
),
|
||||
)
|
||||
|
||||
@@ -54,14 +58,14 @@ class PyObjectId(ObjectId):
|
||||
|
||||
class MongoBaseModel(BaseModel):
|
||||
"""Base model for all MongoDB documents."""
|
||||
|
||||
|
||||
model_config = ConfigDict(
|
||||
arbitrary_types_allowed=True,
|
||||
json_encoders={ObjectId: str},
|
||||
populate_by_name=True,
|
||||
from_attributes=True,
|
||||
validate_assignment=True,
|
||||
extra='allow'
|
||||
extra="allow",
|
||||
)
|
||||
|
||||
# Optional _id field that will be ignored in create operations
|
||||
@@ -69,11 +73,11 @@ class MongoBaseModel(BaseModel):
|
||||
|
||||
def get_extra(self, field_name: str, default: Any = None) -> Any:
|
||||
"""Safely get extra field value.
|
||||
|
||||
|
||||
Args:
|
||||
field_name: Name of the extra field to retrieve
|
||||
default: Default value to return if field doesn't exist
|
||||
|
||||
|
||||
Returns:
|
||||
Value of the extra field if it exists, otherwise the default value
|
||||
"""
|
||||
@@ -81,7 +85,7 @@ class MongoBaseModel(BaseModel):
|
||||
|
||||
def as_dict(self) -> Dict[str, Any]:
|
||||
"""Convert model to dictionary including all fields and extra fields.
|
||||
|
||||
|
||||
Returns:
|
||||
Dict containing all model fields and extra fields with proper type conversion
|
||||
"""
|
||||
@@ -94,18 +98,18 @@ class MongoDocument(MongoBaseModel):
|
||||
created_at: float = Field(default_factory=lambda: system_arrow.now().timestamp())
|
||||
updated_at: float = Field(default_factory=lambda: system_arrow.now().timestamp())
|
||||
|
||||
@model_validator(mode='before')
|
||||
@model_validator(mode="before")
|
||||
@classmethod
|
||||
def prevent_protected_fields(cls, data: Any) -> Any:
|
||||
"""Prevent user from setting protected fields like _id and timestamps."""
|
||||
if isinstance(data, dict):
|
||||
# Remove protected fields from input
|
||||
data.pop('_id', None)
|
||||
data.pop('created_at', None)
|
||||
data.pop('updated_at', None)
|
||||
|
||||
data.pop("_id", None)
|
||||
data.pop("created_at", None)
|
||||
data.pop("updated_at", None)
|
||||
|
||||
# Set timestamps
|
||||
data['created_at'] = system_arrow.now().timestamp()
|
||||
data['updated_at'] = system_arrow.now().timestamp()
|
||||
|
||||
data["created_at"] = system_arrow.now().timestamp()
|
||||
data["updated_at"] = system_arrow.now().timestamp()
|
||||
|
||||
return data
|
||||
|
||||
Reference in New Issue
Block a user