Full Working static multi tenant
This commit is contained in:
@@ -6,12 +6,16 @@ from pydantic import BaseModel, Field, computed_field
|
||||
|
||||
|
||||
class CrudDocument(BaseModel):
|
||||
id: Optional[PydanticObjectId] = Field(alias="_id", default=None)
|
||||
id: Optional[PydanticObjectId] = Field(default=None)
|
||||
created_at: datetime = Field(default=datetime.now(UTC), nullable=False, title="Créé le")
|
||||
# created_by: str
|
||||
updated_at: datetime = Field(default_factory=datetime.utcnow, nullable=False, title="Modifié le")
|
||||
# updated_by: str
|
||||
|
||||
@property
|
||||
def _id(self):
|
||||
return self.id
|
||||
|
||||
@computed_field
|
||||
def label(self) -> str:
|
||||
return self.compute_label()
|
||||
@@ -45,12 +49,17 @@ class CrudDocument(BaseModel):
|
||||
|
||||
@classmethod
|
||||
async def get(cls, db, model_id):
|
||||
return cls.model_validate(await cls._get_collection(db).find_one({"_id": model_id}))
|
||||
value = await cls._get_collection(db).find_one({"_id": model_id})
|
||||
if not value:
|
||||
return None
|
||||
|
||||
value["id"] = value.pop("_id")
|
||||
return cls.model_validate(value)
|
||||
|
||||
@classmethod
|
||||
async def update(cls, db, model, update_schema):
|
||||
update_query = {
|
||||
"$set": {field: value for field, value in update_schema.model_dump(mode="json").items()}
|
||||
"$set": {field: value for field, value in update_schema.model_dump(mode="json").items() if field!= "id" }
|
||||
}
|
||||
|
||||
await cls._get_collection(db).update_one({"_id": model.id}, update_query)
|
||||
|
||||
Reference in New Issue
Block a user