diff --git a/settings/dev.py b/settings/dev.py index a5c2f66..3f83f97 100644 --- a/settings/dev.py +++ b/settings/dev.py @@ -5,6 +5,9 @@ BASE_DIR = os.path.dirname(os.path.abspath(__file__)) SECRET_KEY = 'cf036e50524a11ecb4a618c04d9eb245' +JSONIFY_PRETTYPRINT_REGULAR = False +RESTFUL_JSON = {'separators': (',', ':'), 'indent': 0} + # mongodb 地址 MONGODB_SETTINGS = { 'host': 'mongodb://admin:111111@10.2.2.10:27017/ops_api?authSource=admin', diff --git a/src/common/utils.py b/src/common/utils.py new file mode 100644 index 0000000..d0d172b --- /dev/null +++ b/src/common/utils.py @@ -0,0 +1,18 @@ +from flask import jsonify + + +def make_response(status, code, msg, **kwargs): + """返回一个错误,响应内容是 `json` + :param status: http 状态 + :param code: 操作状态码,通常 `1000` 是成功 + :param msg: 消息内容 + :param kwargs: 其他需要写入响应的内容,注意需要 `json` 可以解析 + """ + content = { + "code": code, + "msg": msg, + } + content.update(**kwargs) + response = jsonify(content) + response.status_code = status + return response diff --git a/src/project/views.py b/src/project/views.py index 2b256d6..3103ff3 100644 --- a/src/project/views.py +++ b/src/project/views.py @@ -1,13 +1,14 @@ import datetime from flask import current_app as app -from flask_restful import reqparse, abort +from flask_restful import reqparse from asset.models import Host from project import fields from project.models import Project, Channel, Server, Version from common.views import ListMixin, CreateMixin, ListCreateViewSet, DetailViewSet from common.permission import token_header_required +from common.utils import make_response class ProjectViews(ListMixin, CreateMixin): @@ -93,7 +94,7 @@ class ServerSyncView(CreateMixin): data = args["data"] if count != len(data): - abort(400, msg="quantity mismatch", code=1001) + return make_response(400, 1001, "quantity mismatch") hosts = {} channels = {} @@ -140,6 +141,6 @@ class ServerSyncView(CreateMixin): continue if errors: - return {"msg": "有区服同步出错!", "code": 1020, "errors": errors} + return make_response(200, 1020, "有区服同步出错", errors=errors) - return {"msg": "ok", "code": 1000} + return make_response(200, 1000, "success")