diff --git a/api/api_v1/endpoints/authority.py b/api/api_v1/endpoints/authority.py index d0011d3..6405a7f 100644 --- a/api/api_v1/endpoints/authority.py +++ b/api/api_v1/endpoints/authority.py @@ -222,14 +222,14 @@ async def all_role(request: Request, } """获取域内所有角色""" - roles = await crud.authority.find_many(db, role_name={'$exists': 1}, game=game) + roles = await crud.authority.find_many(db, {'role_name': {'$exists': 1}, 'game': game}) dom_data = [{'role': item['v1'], 'title': item['role_name'], 'id': str(item['_id'])} for item in roles] for item in dom_data: q = await crud.authority.get_role_dom_authority(db, item['role'], game, api_data) item['authority'] = [{'title': k, 'child': v} for k, v in q.items()] # 获取系统角色 - roles = await crud.authority.find_many(db, role_name={'$exists': 1}, game='*') + roles = await crud.authority.find_many(db, {'role_name':{'$exists': 1}, 'game':'*'}) sys_data = [{'role': item['v1'], 'title': item['role_name'], 'id': str(item['_id'])} for item in roles] for item in sys_data: q = await crud.authority.get_role_dom_authority(db, item['role'], dom=game, api_data=api_data) @@ -241,8 +241,6 @@ async def all_role(request: Request, } return schemas.Msg(code=0, msg='ok', data=data) - - # @router.post("/set_role") # async def set_role(request: Request, # data_id: schemas.AccountSetRole, diff --git a/api/api_v1/endpoints/dashboard.py b/api/api_v1/endpoints/dashboard.py index 4633558..085c2b3 100644 --- a/api/api_v1/endpoints/dashboard.py +++ b/api/api_v1/endpoints/dashboard.py @@ -5,6 +5,7 @@ import crud, schemas from db import get_database from api import deps +from utils.func import get_uid router = APIRouter() @@ -26,12 +27,13 @@ async def create( @router.post("/delete") async def delete( + request: Request, data_in: schemas.DashboardDelete, db: AsyncIOMotorDatabase = Depends(get_database), current_user: schemas.UserDB = Depends(deps.get_current_user) ) -> schemas.Msg: """删除看板""" - del_dashboard = await crud.dashboard.delete(db, _id=data_in.id, user_id=current_user.id) + del_dashboard = await crud.dashboard.delete(db, {'_id': {'$in': data_in.ids}, 'user_id': request.user.id}) if del_dashboard.deleted_count == 0: return schemas.Msg(code=-1, msg='error', data='删除失败') @@ -47,11 +49,45 @@ async def move( """ 移动看板 """ - res = await crud.dashboard.update_one(db, {'_id': data_in.source_id}, - {'$set': dict(cat=data_in.cat, pid=data_in.dest_id)}) - if res.deleted_count == 0: - return schemas.Msg(code=-1, msg='error', data='删除失败') - return schemas.Msg(code=0, msg='ok', data='删除成功') + for source_id in data_in.source_ids: + res = await crud.dashboard.update_one(db, {'_id': source_id}, + {'$set': dict(cat=data_in.cat, pid=data_in.dest_pid)}) + return schemas.Msg(code=0, msg='ok', data='移动成功') + + +@router.post("/copy") +async def copy( + data_in: schemas.DashboardCopy, + db: AsyncIOMotorDatabase = Depends(get_database), + current_user: schemas.UserDB = Depends(deps.get_current_user) +) -> schemas.Msg: + """ + 复制到其他项目 + """ + # 检查是否存在默认空间 不存在就创建 + dest_project_id = data_in.dest_project_id + dest_default_space = await crud.space.find_one(db, {'project_id': dest_project_id, 'name': '默认空间'}, + {'_id': True}) + dest_space_id = dest_default_space.get('_id') + # 创建默认空间 + if not dest_space_id: + default_space = await crud.space.create(db, + schemas.SpaceCreate(name='默认空间', project_id=dest_project_id), + user=current_user) + dest_space_id = default_space.inserted_id + + dashboards = await crud.dashboard.find_many(db, {'_id': {'$in': data_in.source_ids}}, {'_id': False}) + for item in dashboards: + item['project_id'] = dest_project_id + item['pid'] = dest_space_id + item['cat'] = 'space' + item['_id'] = get_uid() + await crud.dashboard.update_one(db, + {'project_id': item['project_id'], 'name': item['name'], + 'user_id': item['user_id']}, {'$set': item}, + upsert=True) + + return schemas.Msg(code=0, msg='ok', data='复制成功') @router.post("/add_report") diff --git a/api/api_v1/endpoints/data_auth.py b/api/api_v1/endpoints/data_auth.py index ec0777b..f50ea31 100644 --- a/api/api_v1/endpoints/data_auth.py +++ b/api/api_v1/endpoints/data_auth.py @@ -146,7 +146,7 @@ async def user_property(request: Request, data = json.loads(data) propertys = [] - data_attr = await crud.data_attr.find_many(db, game=game, cat='user') + data_attr = await crud.data_attr.find_many(db, {'game':game, 'cat':'user'}) data_attr = {item['name']: item for item in data_attr} for k, v in data.items(): @@ -181,7 +181,7 @@ async def load_prop_quotas(request: Request, all_filed = await rdb.get(f'{game}_event') all_filed = json.loads(all_filed) - data_attr = await crud.data_attr.find_many(db, game=game, cat='event') + data_attr = await crud.data_attr.find_many(db, {'game':game, 'cat':'event'}) data_attr = {item['name']: item for item in data_attr} event_props = [] for item in event_prop_list: @@ -274,7 +274,7 @@ async def load_filter_props(request: Request, all_filed = await rdb.get(f'{game}_event') all_filed = json.loads(all_filed) - data_attr = await crud.data_attr.find_many(db, game=game, cat='event') + data_attr = await crud.data_attr.find_many(db, {'game':game, 'cat':'event'}) data_attr = {item['name']: item for item in data_attr} event_props = [] for item in event_prop_list: @@ -288,7 +288,7 @@ async def load_filter_props(request: Request, } event_props.append(event_prop) - data_attr = await crud.data_attr.find_many(db, game=game, cat='user') + data_attr = await crud.data_attr.find_many(db, {'game':game, 'cat':'user'}) data_attr = {item['name']: item for item in data_attr} user_props = [] for item in user_prop_list: diff --git a/api/api_v1/endpoints/data_mana.py b/api/api_v1/endpoints/data_mana.py index 0280222..6f13057 100644 --- a/api/api_v1/endpoints/data_mana.py +++ b/api/api_v1/endpoints/data_mana.py @@ -31,7 +31,7 @@ async def read_data_attr( data = json.loads(data) res = [] - data_attr = await crud.data_attr.find_many(db, game=game, cat=cat) + data_attr = await crud.data_attr.find_many(db, {'game':game, 'cat':cat}) data_attr = {item['name']: item for item in data_attr} for k, v in data.items(): diff --git a/api/api_v1/endpoints/event_mana.py b/api/api_v1/endpoints/event_mana.py index ad18128..08987a3 100644 --- a/api/api_v1/endpoints/event_mana.py +++ b/api/api_v1/endpoints/event_mana.py @@ -29,7 +29,7 @@ async def event_list( """事件列表""" event_list = await ckdb.distinct(game, 'event', '#event_name') event_count = await ckdb.yesterday_event_count(game) - event_meta = await crud.event_mana.find_many(db, game=game) or {} + event_meta = await crud.event_mana.find_many(db, {'game':game}) or {} if event_meta: event_meta = pd.DataFrame(event_meta).set_index('event_name').fillna('').T.to_dict() diff --git a/api/api_v1/endpoints/project.py b/api/api_v1/endpoints/project.py index 44af6b3..a645b45 100644 --- a/api/api_v1/endpoints/project.py +++ b/api/api_v1/endpoints/project.py @@ -55,7 +55,7 @@ async def create( await crud.authority.create(db, 'g', settings.SUPERUSER_NAME, role_name, '*', '*', role_name='系统项目管理员', game='*') # 添加数据权限 await crud.authority.set_data_auth(db, schemas.DataAuthSet(username=request.user.username, data_auth_id='*'), - game=data_in.game,v1=role_name) + game=data_in.game, v1=role_name) return schemas.Msg(code=0, msg='创建成功') @@ -161,7 +161,7 @@ async def members(request: Request, current_user: schemas.UserDB = Depends(deps.get_current_user) ): """查看项目成员""" - roles = await crud.authority.find_many(db, ptype='g', v2=game) + roles = await crud.authority.find_many(db, {'ptype': 'g', 'v2': game}) data = dict() data_auth_ids = set() for item in roles: @@ -214,7 +214,7 @@ async def read_kanban( kanban = await crud.folder.read_folder(db, project_id=data_in.id, user_id=current_user.id, cat='kanban') for item in kanban: - dashboards = await crud.dashboard.find_many(db, pid=item['_id']) + dashboards = await crud.dashboard.find_many(db, {'pid': item['_id']}) res['kanban'].append({ 'name': item['name'], 'children': [], @@ -232,7 +232,7 @@ async def read_kanban( 'members._id': current_user.id # '$or': [{'rw_members': current_user.id}, {'r_members': current_user.id}] } - spaces = await crud.space.find_many(db, **where) + spaces = await crud.space.find_many(db, where) # 空间 文件夹 看板 for item in spaces: res['spaces'].append({ @@ -242,7 +242,7 @@ async def read_kanban( }) res['spaces'][-1]['authority'] = 'rw' if current_user.id in item['members'] else 'r' - for f in await crud.folder.find_many(db, pid=item['_id']): + for f in await crud.folder.find_many(db, {'pid': item['_id']}): res['spaces'][-1]['children'].append({ 'name': f['name'], '_id': f['_id'], @@ -250,14 +250,14 @@ async def read_kanban( 'isFolder': True }) - for d in await crud.dashboard.find_many(db, pid=f['_id']): + for d in await crud.dashboard.find_many(db, {'pid': f['_id']}): res['spaces'][-1]['children'][-1]['children'].append({ 'name': d['name'], '_id': d['_id'] }) # 空间 看板 - for d in await crud.dashboard.find_many(db, pid=item['_id']): + for d in await crud.dashboard.find_many(db, {'pid': item['_id']}): res['spaces'][-1]['children'].append({ 'name': d['name'], '_id': d['_id'], diff --git a/api/api_v1/endpoints/space.py b/api/api_v1/endpoints/space.py index 351fcbe..540412b 100644 --- a/api/api_v1/endpoints/space.py +++ b/api/api_v1/endpoints/space.py @@ -41,7 +41,7 @@ async def delete( # 删除空间 自己创建的 del_space = await crud.space.delete(db, _id=data_in.id, user_id=current_user.id) # 删除文件夹 - del_folder = await crud.folder.find_many(db, pid=data_in.id) + del_folder = await crud.folder.find_many(db, {'pid':data_in.id}) del_folder_ids = [f['_id'] for f in del_folder] await crud.folder.delete(db, pid=data_in.id) # 删除文件夹下的 dashboard diff --git a/api/api_v1/endpoints/test.py b/api/api_v1/endpoints/test.py index ee0d415..b73300f 100644 --- a/api/api_v1/endpoints/test.py +++ b/api/api_v1/endpoints/test.py @@ -1,9 +1,12 @@ from typing import Any from fastapi import APIRouter, Depends, Request +from motor.motor_asyncio import AsyncIOMotorDatabase +import crud import schemas from api import deps +from db import get_database from db.ckdb import CKDrive, get_ck_db from db.redisdb import RedisDrive, get_redis_pool from models.behavior_analysis import BehaviorAnalysis @@ -17,13 +20,11 @@ async def test( game: str, ckdb: CKDrive = Depends(get_ck_db), rdb: RedisDrive = Depends(get_redis_pool), + db: AsyncIOMotorDatabase = Depends(get_database), current_user: schemas.UserDB = Depends(deps.get_current_user), - analysis: BehaviorAnalysis = Depends(BehaviorAnalysis)) -> schemas.Msg: - await analysis.init() - query = analysis.scatter_model_sql() - data = { - 'game': game, - 'analysis': analysis.game, - 'query': query - } - return schemas.Msg(code=0, msg='ok', data=data) + analysis: BehaviorAnalysis = Depends(BehaviorAnalysis)): + + aaa=await crud.space.create(db, + schemas.SpaceCreate(name='默认空间', project_id='1b238814be9811ebb7a0d5bd867f57d6'), + user=current_user) + return 'aa' diff --git a/crud/base.py b/crud/base.py index 00fdfa4..e5ea933 100644 --- a/crud/base.py +++ b/crud/base.py @@ -10,8 +10,11 @@ class CRUDBase: async def get(self, db, id: Union[ObjectId, str]) -> dict: return (await db[self.coll_name].find_one({'_id': id})) or dict() - async def find_one(self, db, projection=None, *args, **kwargs): - return (await db[self.coll_name].find_one(projection, *args, **kwargs)) or dict() + async def insert_one(self, db, document): + return await db[self.coll_name].insert_one(document) + + async def find_one(self, db, filter=None, *args, **kwargs): + return (await db[self.coll_name].find_one(filter, *args, **kwargs)) or dict() async def read_have(self, db, v: str, **kwargs): where = {'members': v} @@ -19,8 +22,8 @@ class CRUDBase: cursor = db[self.coll_name].find(where) return await cursor.to_list(length=9999) - async def find_many(self, db, projection=None, **kwargs): - cursor = db[self.coll_name].find(kwargs, projection) + async def find_many(self, db, *args, **kwargs): + cursor = db[self.coll_name].find(*args, **kwargs) return await cursor.to_list(length=9999) def find(self, db, *args, **kwargs): @@ -32,8 +35,8 @@ class CRUDBase: async for doc in cursor: yield doc - async def delete(self, db, **kwargs): - return await db[self.coll_name].delete_many(kwargs) + async def delete(self, db, filter, collation=None, hint=None, session=None): + return await db[self.coll_name].delete_many(filter, collation, hint, session) async def update_one(self, db, filter, update, upsert=False): return await db[self.coll_name].update_one(filter, update, upsert) diff --git a/crud/crud_authority.py b/crud/crud_authority.py index e828b27..636f44a 100644 --- a/crud/crud_authority.py +++ b/crud/crud_authority.py @@ -39,9 +39,9 @@ class CRUDAuthority(CRUDBase): pass async def get_role_dom_authority(self, db, role, dom, api_data): - selected_api = {item['v2'] for item in await self.find_many(db, v0=role, v1=dom)} + selected_api = {item['v2'] for item in await self.find_many(db, {'v0':role, 'v1':dom})} - anonymous_api = {item['v2'] for item in await self.find_many(db, v0='*')} + anonymous_api = {item['v2'] for item in await self.find_many(db, {'v0':'*'})} api_data = deepcopy(api_data) diff --git a/crud/crud_data_auth.py b/crud/crud_data_auth.py index c574e20..e9203a6 100644 --- a/crud/crud_data_auth.py +++ b/crud/crud_data_auth.py @@ -17,7 +17,7 @@ class CRUDDataAuth(CRUDBase): await self.update_one(db, data, {'$set': data}, upsert=True) async def get_game_data_auth(self, db, game): - return await self.find_many(db, game=game) + return await self.find_many(db, {'game':game}) async def edit_data_auth(self, db, data_in: DataAuthEdit): return await self.update_one(db, {'_id': ObjectId(data_in.data_auth_id)}, diff --git a/crud/crud_report.py b/crud/crud_report.py index 4e0d2cd..af76aa6 100644 --- a/crud/crud_report.py +++ b/crud/crud_report.py @@ -22,7 +22,9 @@ class CRUDReport(CRUDBase): unique=True) async def read_report(self, db, user_id, project_id, projection=None, **kwargs): - res = await self.find_many(db, user_id=user_id, project_id=project_id,projection=projection, **kwargs) + where = {'user_id': user_id, 'project_id': project_id} + where.update(**kwargs) + res = await self.find_many(db, where, projection) return res diff --git a/crud/crud_space.py b/crud/crud_space.py index 2537bc2..0339027 100644 --- a/crud/crud_space.py +++ b/crud/crud_space.py @@ -15,7 +15,7 @@ class CRUDSpace(CRUDBase): **obj_in.dict(by_alias=True), user_id=user.id, _id=uuid.uuid1().hex ) - await db[self.coll_name].insert_one(db_obj.dict(by_alias=True)) + return await db[self.coll_name].insert_one(db_obj.dict(by_alias=True)) async def read_space(self, db, user_id, project_id): return await self.read_have(db, user_id=user_id, project_id=project_id) diff --git a/schemas/dashboard.py b/schemas/dashboard.py index 714cd96..6f6431d 100644 --- a/schemas/dashboard.py +++ b/schemas/dashboard.py @@ -24,8 +24,8 @@ class ReadDashboard(BaseModel): id: str -class DashboardDelete(DBBase): - pass +class DashboardDelete(BaseModel): + ids: List[str] class Category(str, Enum): @@ -34,11 +34,16 @@ class Category(str, Enum): class DashboardMove(BaseModel): - source_id: str - dest_id: str + source_ids: List[str] + dest_pid: str cat: Category +class DashboardCopy(BaseModel): + source_ids: List[str] + dest_project_id: str + + class Report(BaseModel): name: str report_id: str diff --git a/utils/func.py b/utils/func.py new file mode 100644 index 0000000..359e8bb --- /dev/null +++ b/utils/func.py @@ -0,0 +1,5 @@ +import time + + +def get_uid(): + return hex(int(time.time() * 1000 * 10 ** 6))[2:]