Files
Crm-Backend/repositories/mixins.py

26 lines
724 B
Python

from typing import Generic, TypeVar
from sqlalchemy.ext.asyncio import AsyncSession
EntityType = TypeVar("EntityType")
class RepBaseMixin(Generic[EntityType]):
session: AsyncSession
class RepDeleteMixin(RepBaseMixin[EntityType]):
async def delete(self, obj: EntityType, is_soft: bool) -> None:
if not is_soft:
await self.session.delete(obj)
await self.session.commit()
return
if not hasattr(obj, "is_deleted"):
raise AttributeError(
f"{obj.__class__.__name__} does not support soft delete (missing `is_deleted` field)"
)
obj.is_deleted = True
self.session.add(obj)
await self.session.commit()