32 lines
987 B
Python
32 lines
987 B
Python
import pymongo
|
|
from motor.motor_asyncio import AsyncIOMotorDatabase
|
|
|
|
from crud.base import CRUDBase
|
|
from schemas import *
|
|
|
|
__all__ = 'report',
|
|
|
|
|
|
class CRUDReport(CRUDBase):
|
|
|
|
async def create(self, db: AsyncIOMotorDatabase, obj_in: ReportCreate, user_id: str):
|
|
db_obj = ReportDB(
|
|
**obj_in.dict(), user_id=user_id,
|
|
_id=uuid.uuid1().hex
|
|
)
|
|
await db[self.coll_name].insert_one(db_obj.dict(by_alias=True))
|
|
|
|
async def create_index(self, db: AsyncIOMotorDatabase):
|
|
await db[self.coll_name].create_index(
|
|
[('project_id', pymongo.DESCENDING), ('name', pymongo.DESCENDING), ('user_id', pymongo.DESCENDING)],
|
|
unique=True)
|
|
|
|
async def read_report(self, db, user_id, project_id, projection=None, **kwargs):
|
|
where = {'user_id': user_id, 'project_id': project_id}
|
|
where.update(**kwargs)
|
|
res = await self.find_many(db, where, projection)
|
|
return res
|
|
|
|
|
|
report = CRUDReport('report')
|