From b5a0791a5daacc29c3097fd2865e6d272ef730a4 Mon Sep 17 00:00:00 2001 From: wuaho Date: Tue, 17 Aug 2021 18:06:51 +0800 Subject: [PATCH] 1 --- api/api_v1/ck_mana/__init__.py | 0 api/api_v1/ck_mana/event.py | 31 +++++++++++++++++++++++++ api/api_v1/endpoints/dashboard.py | 3 +-- api/api_v1/endpoints/data_auth.py | 16 +++++-------- api/api_v1/endpoints/report.py | 20 ++++++++++++++++ api/api_v1/endpoints/space.py | 38 ++++++++++++++++++++++++++----- crud/crud_space.py | 4 ++++ models/behavior_analysis.py | 2 +- schemas/report.py | 5 ++++ schemas/space.py | 10 ++++++++ 10 files changed, 110 insertions(+), 19 deletions(-) create mode 100644 api/api_v1/ck_mana/__init__.py create mode 100644 api/api_v1/ck_mana/event.py diff --git a/api/api_v1/ck_mana/__init__.py b/api/api_v1/ck_mana/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/api/api_v1/ck_mana/event.py b/api/api_v1/ck_mana/event.py new file mode 100644 index 0000000..c81f727 --- /dev/null +++ b/api/api_v1/ck_mana/event.py @@ -0,0 +1,31 @@ +from collections import defaultdict + +import pandas as pd +import numpy as np +from fastapi import APIRouter, Depends, Request +from motor.motor_asyncio import AsyncIOMotorDatabase + +import crud, schemas +from common import * + +from api import deps +from db import get_database +from db.ckdb import get_ck_db, CKDrive +from db.redisdb import get_redis_pool, RedisDrive + +from models.behavior_analysis import BehaviorAnalysis +from models.user_analysis import UserAnalysis +from models.x_analysis import XAnalysis + +router = APIRouter() + + +@router.post("/update_event_view") +async def update_event_view( + request: Request, + game: str, + ckdb: CKDrive = Depends(get_ck_db), + current_user: schemas.UserDB = Depends(deps.get_current_user) +) -> schemas.Msg: + """ 更新事件视图 """ + pass diff --git a/api/api_v1/endpoints/dashboard.py b/api/api_v1/endpoints/dashboard.py index aa00597..d9c6783 100644 --- a/api/api_v1/endpoints/dashboard.py +++ b/api/api_v1/endpoints/dashboard.py @@ -49,7 +49,7 @@ async def delete( current_user: schemas.UserDB = Depends(deps.get_current_user) ) -> schemas.Msg: """删除看板""" - del_dashboard = await crud.dashboard.delete(db, {'_id': {'$in': data_in.ids}, 'user_id': request.user.id}) + del_dashboard = await crud.dashboard.delete(db, {'_id': {'$in': data_in.ids}}) if del_dashboard.deleted_count == 0: return schemas.Msg(code=-1, msg='error', data='删除失败') @@ -104,7 +104,6 @@ async def copy( new_report_id = get_uid() new_report['_id'] = new_report_id new_report['project_id'] = dest_project_id - report['report_id'] = new_report_id await crud.report.insert_one(db, new_report) await crud.dashboard.update_one(db, diff --git a/api/api_v1/endpoints/data_auth.py b/api/api_v1/endpoints/data_auth.py index bc448e5..736d937 100644 --- a/api/api_v1/endpoints/data_auth.py +++ b/api/api_v1/endpoints/data_auth.py @@ -111,19 +111,15 @@ async def my_event(request: Request, data_auth_id = await crud.authority.get_data_auth_id(db, game, request.user.username) my_data_auth = [] - if data_auth_id: - # 所有数据权限 - if data_auth_id == '*': - my_data_auth = await ck.distinct(game, 'event', '#event_name') + my_data_auth = await ck.distinct(game, 'event', '#event_name') - else: - # 设置了数据权限 - my_data_auth = await crud.data_auth.get(db, ObjectId(data_auth_id)) - my_data_auth = my_data_auth['data'] + # + # else: + # # 设置了数据权限 + # my_data_auth = await crud.data_auth.get(db, ObjectId(data_auth_id)) + # my_data_auth = my_data_auth['data'] - if not my_data_auth: - return schemas.Msg(code=0, msg='ok', data=[]) event_show_name = await crud.event_mana.get_all_show_name(db, game) event_list.append({'id': 'event', 'title': '全部事件', 'category': []}) diff --git a/api/api_v1/endpoints/report.py b/api/api_v1/endpoints/report.py index 9994e4f..69e9916 100644 --- a/api/api_v1/endpoints/report.py +++ b/api/api_v1/endpoints/report.py @@ -7,6 +7,7 @@ import crud, schemas from db import get_database from api import deps +from utils import get_uid router = APIRouter() @@ -43,6 +44,25 @@ async def edit( return schemas.Msg(code=0, msg='ok', data='编辑成功') +@router.post("/copy") +async def copy( + request: Request, + data_in: schemas.ReportCopy, + game: str, + db: AsyncIOMotorDatabase = Depends(get_database), + current_user: schemas.UserDB = Depends(deps.get_current_user) +) -> schemas.Msg: + """复制报表到其他项目""" + + for report_id in data_in.report_ids: + new_report = await crud.report.get(db, report_id) + if not new_report: + continue + new_report_id = get_uid() + new_report['_id'] = new_report_id + new_report['project_id'] = data_in.dest_project_id + await crud.report.insert_one(db, new_report) + return schemas.Msg(code=0, msg='ok', data='编辑成功') @router.post("/read_report") async def read_report( diff --git a/api/api_v1/endpoints/space.py b/api/api_v1/endpoints/space.py index 540412b..b74aa72 100644 --- a/api/api_v1/endpoints/space.py +++ b/api/api_v1/endpoints/space.py @@ -39,17 +39,43 @@ async def delete( ) -> schemas.Msg: """删除空间""" # 删除空间 自己创建的 - del_space = await crud.space.delete(db, _id=data_in.id, user_id=current_user.id) + del_space = await crud.space.delete(db, {'_id': data_in.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) + await crud.folder.delete(db, {'pid': data_in.id}) # 删除文件夹下的 dashboard - await crud.dashboard.delete(db, **{'$in': {'_id': del_folder_ids}}) + if del_folder_ids: + await crud.dashboard.delete(db, {'_id': {'$in': del_folder_ids}}) # 删除空间下的 dashboard - await crud.dashboard.delete(db, pid=data_in.id) + await crud.dashboard.delete(db, {'pid': data_in.id}) if del_space.deleted_count == 0: return schemas.Msg(code=-1, msg='error', data='删除失败') - return schemas.Msg(code=0, msg='ok', dtta='删除成功') + return schemas.Msg(code=0, msg='ok', data='删除成功') + + +@router.post("/rename") +async def rename( + data_in: schemas.SpaceRename, + db: AsyncIOMotorDatabase = Depends(get_database), + current_user: schemas.UserDB = Depends(deps.get_current_user) +) -> schemas.Msg: + """重命名空间""" + res = await crud.space.rename(db, data_in) + + return schemas.Msg(code=0, msg='ok', data=1) + + +@router.post("/add_members") +async def add_members( + data_in: schemas.AddSpaceMembers, + db: AsyncIOMotorDatabase = Depends(get_database), + current_user: schemas.UserDB = Depends(deps.get_current_user) +) -> schemas.Msg: + """添加空间成员""" + # res = await crud.space.rename(db, data_in) + pass + + return schemas.Msg(code=0, msg='ok', data=1) diff --git a/crud/crud_space.py b/crud/crud_space.py index 0339027..72f5b6d 100644 --- a/crud/crud_space.py +++ b/crud/crud_space.py @@ -1,6 +1,7 @@ import pymongo from motor.motor_asyncio import AsyncIOMotorDatabase +import schemas from crud.base import CRUDBase from schemas import * @@ -20,6 +21,9 @@ class CRUDSpace(CRUDBase): async def read_space(self, db, user_id, project_id): return await self.read_have(db, user_id=user_id, project_id=project_id) + async def rename(self, db, data_in: schemas.SpaceRename): + return await self.update_one(db, {'_id': data_in.space_id}, {'$set': {'name': data_in.new_name}}) + 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)], diff --git a/models/behavior_analysis.py b/models/behavior_analysis.py index df47763..eb8e0cf 100644 --- a/models/behavior_analysis.py +++ b/models/behavior_analysis.py @@ -160,7 +160,7 @@ class BehaviorAnalysis: res_json = await self.rdb.get(f'{self.game}_event') columns = json.loads(res_json).keys() metadata = sa.MetaData(schema=self.game) - self.event_tbl = sa.Table('event', metadata, *[sa.Column(column) for column in columns]) + self.event_tbl = sa.Table('event_view', metadata, *[sa.Column(column) for column in columns]) def handler_filts(self, *ext_filters, g_f=True): user_filter = [] diff --git a/schemas/report.py b/schemas/report.py index 47893fc..ff35a81 100644 --- a/schemas/report.py +++ b/schemas/report.py @@ -30,6 +30,11 @@ class ReportEdit(BaseModel): desc: str +class ReportCopy(BaseModel): + report_ids: List[str] + dest_project_id: str + + class ReportDelete(DBBase): pass diff --git a/schemas/space.py b/schemas/space.py index f5f332f..de52c9e 100644 --- a/schemas/space.py +++ b/schemas/space.py @@ -35,6 +35,16 @@ class SpaceDelete(DBBase): pass +class SpaceRename(BaseModel): + space_id: str + new_name: str + + +class AddSpaceMembers(BaseModel): + space_id: str + user_ids: List[str] + + # -------------------------------------------------------------- # 数据库模型 class SpaceDB(DBBase):