From 3b75cb39993c7551905257208f34f8f7ed855582 Mon Sep 17 00:00:00 2001 From: wuaho Date: Thu, 27 May 2021 18:49:45 +0800 Subject: [PATCH] 1 --- init_db.py | 71 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 71 insertions(+) create mode 100644 init_db.py diff --git a/init_db.py b/init_db.py new file mode 100644 index 0000000..dcd66bf --- /dev/null +++ b/init_db.py @@ -0,0 +1,71 @@ +import crud +import schemas +from core.config import settings + +# 创建一个超级用户、、 +from db import connect_to_mongo, get_database +import asyncio + +connect_to_mongo() +db = get_database() + + +async def create_superuser(): + user = await crud.user.get_by_user(db=db, name=settings.SUPERUSER_NAME) + if not user: + user_in = schemas.UserCreate( + name=settings.SUPERUSER_NAME, + email=settings.SUPERUSER_EMAIL, + password=settings.SUPERUSER_PASSWORD, + nickname=settings.SUPERUSER_NICKNAME, + is_superuser=True, + ) + await crud.user.create(db, user_in) + await crud.user.create_index(db) + + +async def project_index(): + await crud.project.create_index(db) + + +async def folder_index(): + await crud.folder.create_index(db) + + +async def space_index(): + await crud.space.create_index(db) + + +async def dashboard_index(): + await crud.dashboard.create_index(db) + + +async def report_index(): + await crud.report.create_index(db) + + +async def data_attr_index(): + await crud.data_attr.create_index(db) + + +async def authority_init(): + await crud.authority.create_index(db) + await crud.authority.create(db, 'p', '*', '*', '/docs', '*') + await crud.authority.create(db, 'p', '*', '*', '/openapi.json', '*') + await crud.authority.create(db, 'p', '*', '*', '/api/v1/user/login', '*') + await crud.authority.create(db, 'p', '*', '*', '/docs', '*') + await crud.authority.create(db, 'p', '*', '*', '/api/v1/project/', '*') + + +async def main(): + await create_superuser() + await project_index() + await folder_index() + await space_index() + await dashboard_index() + await report_index() + await authority_init() + await data_attr_index() + +loop = asyncio.get_event_loop() +loop.run_until_complete(main())