feat(func): 新增 get_request_json 对驼峰命名参数转换的功能
This commit is contained in:
21
.pre-commit-config.yaml
Normal file
21
.pre-commit-config.yaml
Normal file
@@ -0,0 +1,21 @@
|
||||
repos:
|
||||
|
||||
- repo: https://github.com/pre-commit/mirrors-pylint
|
||||
rev: v2.7.4
|
||||
hooks:
|
||||
- id: pylint
|
||||
exclude: ^nc_http/trash
|
||||
args:
|
||||
- --disable=C0103,C0114,C0115,C0116,E0401,R0201,R0903,R1722,W0105,W0108,W0401,W0613,W0621
|
||||
# - --enable=unused-import
|
||||
|
||||
- repo: https://github.com/pre-commit/pre-commit-hooks
|
||||
rev: v3.3.0
|
||||
hooks:
|
||||
- id: check-json
|
||||
- id: check-merge-conflict
|
||||
- id: check-yaml
|
||||
- id: no-commit-to-branch
|
||||
args: [--branch, staging]
|
||||
- id: check-added-large-files
|
||||
args: [ --maxkb=512 ]
|
@@ -23,9 +23,40 @@ def strip_value(data):
|
||||
return data
|
||||
|
||||
|
||||
def get_request_json():
|
||||
def camelize(uncamelized_str):
|
||||
"""
|
||||
小写下划线转驼峰
|
||||
:param uncamelized_str:
|
||||
:return:
|
||||
"""
|
||||
if not uncamelized_str:
|
||||
return uncamelized_str
|
||||
result = ''.join(i.capitalize() for i in uncamelized_str.split('_'))
|
||||
result = ''.join((result[0].lower(), result[1:]))
|
||||
return result
|
||||
|
||||
|
||||
def uncamelize(camelized_str):
|
||||
"""
|
||||
驼峰转小写下划线
|
||||
:param camelized_str:
|
||||
:return:
|
||||
"""
|
||||
if not camelized_str:
|
||||
return camelized_str
|
||||
lst = []
|
||||
for index, char in enumerate(camelized_str):
|
||||
if char.isupper() and index != 0:
|
||||
lst.append("_")
|
||||
lst.append(char)
|
||||
|
||||
return ''.join(lst).lower()
|
||||
|
||||
|
||||
def get_request_json(is_uncamelize=False):
|
||||
"""
|
||||
获取 json 传递参数
|
||||
:param is_uncamelize: 是否进行驼峰->小写下划线转换
|
||||
:return:
|
||||
"""
|
||||
if 'request_data' not in g:
|
||||
@@ -37,6 +68,8 @@ def get_request_json():
|
||||
data = json.loads(json_data)
|
||||
else:
|
||||
data = request.get_json(force=True, silent=True) or {}
|
||||
if is_uncamelize:
|
||||
data = {uncamelize(k): v for k, v in data.items()}
|
||||
|
||||
g.request_data = strip_value(data)
|
||||
|
||||
|
@@ -8,3 +8,11 @@ captcha>=0.3
|
||||
six>=1.15.0
|
||||
requests>=2.25.0
|
||||
locust>=1.5.3
|
||||
|
||||
docxtpl>=0.11.5
|
||||
python-docx>=0.8.11
|
||||
|
||||
python-dateutil
|
||||
sqlalchemy
|
||||
|
||||
pre-commit
|
Reference in New Issue
Block a user