From 61babc3442635562db4c3662d9b125535122e59b Mon Sep 17 00:00:00 2001 From: youhuo Date: Thu, 18 Aug 2022 16:12:26 +0800 Subject: [PATCH] feat: fix bugs of opensource --- .gitignore | 4 ++-- app.properties | 2 +- app.py | 8 ++++++-- app/__init__.py | 17 ++++++++++------- app/setting.py | 5 ++++- models/tone_model.py | 2 +- services/auth_service.py | 2 ++ services/case_service.py | 5 ++++- services/outline_service.py | 27 +++++++++++++-------------- views/common_view.py | 9 ++++++--- views/outline_view.py | 3 +-- views/requirement_view.py | 2 +- 12 files changed, 51 insertions(+), 35 deletions(-) diff --git a/.gitignore b/.gitignore index 8fdc0d6..808746a 100644 --- a/.gitignore +++ b/.gitignore @@ -56,6 +56,6 @@ venv.bak/ *.db # application config -antx.properties acm-data -nacos-data/ \ No newline at end of file +nacos-data/ +results \ No newline at end of file diff --git a/app.properties b/app.properties index 0498fff..c921a27 100644 --- a/app.properties +++ b/app.properties @@ -3,7 +3,7 @@ host = 0.0.0.0 port = 8005 debug = True access_log = True -workers = 1 +workers = 3 sanic_key = test-library auto_reload = False jwt_secret_key = test-lib-jwt-secret-key diff --git a/app.py b/app.py index 4031c8e..3fd6a9b 100644 --- a/app.py +++ b/app.py @@ -1,6 +1,10 @@ -from app import app +from sanic import Sanic + +from app import init_app if __name__ == '__main__': - app.static('/static', 'dist') + app = Sanic('testlib') + app.static('/static','dist') + init_app(app) app.run(app.config['HOST'], app.config['PORT'], debug=app.config['DEBUG'], access_log=app.config['ACCESS_LOG'], workers=app.config['WORKERS'], auto_reload=app.config['AUTO_RELOAD']) diff --git a/app/__init__.py b/app/__init__.py index ebc8017..dbc9c41 100644 --- a/app/__init__.py +++ b/app/__init__.py @@ -1,20 +1,20 @@ -from sanic import Sanic +import os.path + from app.conf import conf -from app.log import log from app.db import db +from app.log import log from app.oss import alg_oss from app.redis import redis +from app.setting import STATIC_ROOT, STATICFILES_DIRS, OUTLINE_PATH -def init_app(): - app = Sanic('testlib') +def init_app(app): conf.init_app(app) log.init_app(app) db.init_app(app) redis.init_app(app) - alg_oss.init_app(app) + create_outline_files() init_blueprints(app) - return app def init_blueprints(app): @@ -22,4 +22,7 @@ def init_blueprints(app): app.blueprint(Views().bp) -app = init_app() +def create_outline_files(): + outline_path = OUTLINE_PATH + if not os.path.exists(OUTLINE_PATH): + os.makedirs(outline_path) diff --git a/app/setting.py b/app/setting.py index d3870f3..582f2ab 100644 --- a/app/setting.py +++ b/app/setting.py @@ -2,12 +2,15 @@ import os BASE_DIR = os.path.abspath(os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))), "../")) +PROJECT_DIR = os.path.join(BASE_DIR, 'testlib') STATIC_URL = '/dist/dist/' STATICFILES_DIRS = ( os.path.join(BASE_DIR, 'dist/'), ) -STATIC_ROOT = os.path.join(BASE_DIR, '/dist/').replace('\\', '/') +STATIC_ROOT = os.path.join(BASE_DIR, 'testlib/dist/').replace('\\', '/') MEDIA_URL = '/dist/media/' MEDIA_ROOT = os.path.join(BASE_DIR, 'dist/media/').replace('\\', '/') OFFLINE_DATA_DIR = 'offline_data' + +OUTLINE_PATH = os.path.join(PROJECT_DIR, 'results/outlines').replace('\\', '/') diff --git a/models/tone_model.py b/models/tone_model.py index c32f697..0d1b24f 100644 --- a/models/tone_model.py +++ b/models/tone_model.py @@ -36,7 +36,7 @@ async def query_tone_suite(page_num, page_size, condition): select_orm = select(ToneCase.suite_id, ToneCase.suite_name, ToneCase.test_type, func.count(ToneCase.suite_name)) if len(condition) > 0: select_orm = select_orm.where(*condition) - select_orm = select_orm.group_by(ToneCase.suite_name) + select_orm = select_orm.group_by(ToneCase.suite_id, ToneCase.suite_name, ToneCase.test_type) select_orm = select_orm.limit(page_size).offset((page_num - 1) * page_size) async with db.conn() as session: results = await session.execute(select_orm) diff --git a/services/auth_service.py b/services/auth_service.py index fae714c..8918a63 100644 --- a/services/auth_service.py +++ b/services/auth_service.py @@ -104,6 +104,8 @@ async def apply_senior_role(name, data): async def get_reviewers(name): users = await User.query_obj_all(User.nick_name != name, User.role.in_([User_Role.SENIOR, User_Role.COMMON])) + if not users: + return [name] return [user.nick_name for user in users] diff --git a/services/case_service.py b/services/case_service.py index d730469..2863a41 100644 --- a/services/case_service.py +++ b/services/case_service.py @@ -56,7 +56,7 @@ async def create_case(data, owner): data.update(dict({'creator': owner, 'parent': parent})) if 'custom_fields' in data: data.update(dict({'custom_fields': data['custom_fields']})) - if 'type' in data: + if 'test_type' in data: data.update(dict({'type': Case_Type(data['type']).value})) if 'labels' in data: data.update({'labels': data['labels']}) @@ -256,6 +256,9 @@ async def get_follow_module_tree(node_id): if result['parent'] not in module_tree: module_tree[result['parent']] = list() module_tree[result['parent']].append(result) + if len(module_tree) == 0: + root['children'] = [] + return root, True __build_module_tree(module_tree[int(node_id)], module_tree) root['children'] = module_tree[int(node_id)] return root, True diff --git a/services/outline_service.py b/services/outline_service.py index 2f43672..b62738f 100644 --- a/services/outline_service.py +++ b/services/outline_service.py @@ -1,12 +1,11 @@ -import re +import os.path from datetime import datetime from uuid import uuid4 -from app import alg_oss +from app import OUTLINE_PATH from common.enums import User_Role -from common.http import read_file_content from models.outline_model import Outline -from services.const import ERROR_NO_REQUIREMENT_PERMISSION, ERROR_UN_EXISTED_OUTLINE, ERROR_FAILED_OUTLINE_DELETE +from services.const import ERROR_NO_REQUIREMENT_PERMISSION, ERROR_UN_EXISTED_OUTLINE OUTLINE_KEY = 'outline_dict' @@ -24,7 +23,9 @@ async def query_outlines(prefix): async def store(file_name, file_body, file_type, owner, title, remark): tid = f'{uuid4()}{file_type}' - alg_oss.put_object_from_bytes(tid, file_body) + file_path = os.path.join(OUTLINE_PATH, tid) + with open(file_path, "wb") as f: + f.write(file_body) result = await Outline.save( Outline(name=file_name, title=title, owner=owner, tid=tid, remark=remark)) return result.to_dict() @@ -38,7 +39,9 @@ async def update(file_id, file, data, user): return ERROR_NO_REQUIREMENT_PERMISSION, False if file: - alg_oss.put_object_from_bytes(outline.tid, file.body) + file_path = os.path.join(OUTLINE_PATH, outline.tid) + with open(file_path, "wb") as f: + f.write(file.body) outline.name = file.name if data.get('remark'): outline.remark = data.get('remark') @@ -64,12 +67,9 @@ async def extract(file_id): outline = await Outline.query_obj_one(Outline.id == file_id) if not outline: return ERROR_UN_EXISTED_OUTLINE, False - oss_link = alg_oss.get_sign_url(outline.tid) - oss_link = re.sub(r'&Expires=.*&', '&', oss_link) - oss_link = oss_link.replace('https', 'http') - file_body = await read_file_content(oss_link) - if not file_body: - return '下载失败', False + file_path = os.path.join(OUTLINE_PATH, outline.tid) + with open(file_path, "rb") as f: + file_body = f.read() return (outline.name, file_body), True @@ -79,7 +79,6 @@ async def remove(file_id, user): return ERROR_UN_EXISTED_OUTLINE, False if outline.owner != user['user_name'] and user['role'] == User_Role.JUNIOR.value: return ERROR_NO_REQUIREMENT_PERMISSION, False - if not alg_oss.remove_object(outline.tid): - return ERROR_FAILED_OUTLINE_DELETE, False + os.remove(os.path.join(OUTLINE_PATH, outline.tid)) await outline.remove() return 'success', True diff --git a/views/common_view.py b/views/common_view.py index b3053c3..53ffa99 100644 --- a/views/common_view.py +++ b/views/common_view.py @@ -18,14 +18,17 @@ async def upload_images(request, user_infos): @bp.exception(NotFound) async def ignore_404s(request, exception): - return response.text("404. Oops, That page couldn't found.") + oss = 'http://0.0.0.0:8005/static' + url = oss + '/index.html' + _, res = await http_request(url, 'get') + res = res.replace('', '') + return html(res.replace('{OPENANOLIS_TEST_LIB_TOOLS_SERVER}', oss)) @bp.route('/') async def handle_request(request): - oss = 'http://0.0.0.0:8005/static/' + oss = 'http://0.0.0.0:8005/static' url = oss + '/index.html' _, res = await http_request(url, 'get') res = res.replace('', '') - print(res) return html(res.replace('{OPENANOLIS_TEST_LIB_TOOLS_SERVER}', oss)) diff --git a/views/outline_view.py b/views/outline_view.py index 2d4bcd4..9e7b054 100644 --- a/views/outline_view.py +++ b/views/outline_view.py @@ -77,8 +77,7 @@ async def rename(request, file_id, user_infos): @bp.get('/download/') -@login_auth -async def download(_, file_id, user_infos): +async def download(_, file_id): result, ok = await extract(file_id) if not ok: return rsp(code=500, msg=result) diff --git a/views/requirement_view.py b/views/requirement_view.py index 2624c72..dfccf85 100644 --- a/views/requirement_view.py +++ b/views/requirement_view.py @@ -41,7 +41,7 @@ async def create(request, user_infos): response, ok = check_args(check_keys, request.json) if not ok: return response - result, ok = await create_requirement(request.json, user_infos) + result, ok = await create_requirement(request.json, user_infos['user_name']) if not ok: return rsp(code=500, msg=result) return rsp(data=result) -- Gitee