feat: 新增 file/params
This commit is contained in:
15
nc_http/tools/helpers/file.py
Normal file
15
nc_http/tools/helpers/file.py
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
import os
|
||||||
|
import time
|
||||||
|
|
||||||
|
|
||||||
|
def save_file(f, clip=None, root=None):
|
||||||
|
clip = clip or ''
|
||||||
|
clip_path = os.path.join(root, clip, str(int(time.time())))
|
||||||
|
if not os.path.exists(clip_path):
|
||||||
|
os.makedirs(clip_path)
|
||||||
|
|
||||||
|
file_path = os.path.join(clip_path, f.filename)
|
||||||
|
with open(file_path, 'wb+') as tmp_f:
|
||||||
|
tmp_f.write(f.read())
|
||||||
|
|
||||||
|
return file_path
|
23
nc_http/tools/helpers/params.py
Normal file
23
nc_http/tools/helpers/params.py
Normal file
@@ -0,0 +1,23 @@
|
|||||||
|
def filter_fields(data, valid_fields):
|
||||||
|
"""
|
||||||
|
过滤字段
|
||||||
|
:param data:
|
||||||
|
:param valid_fields: 格式['field1', 'field2', 'field3', {'field4': ['sub_field1', 'sub_field2']}]
|
||||||
|
:return:
|
||||||
|
"""
|
||||||
|
if isinstance(data, list):
|
||||||
|
tmp = []
|
||||||
|
for item in data:
|
||||||
|
tmp.append(filter_fields(item, valid_fields))
|
||||||
|
format_data = tmp
|
||||||
|
else:
|
||||||
|
format_data = {}
|
||||||
|
for field in valid_fields:
|
||||||
|
if isinstance(field, dict):
|
||||||
|
for key, sub_valid_fields in field.items():
|
||||||
|
if isinstance(sub_valid_fields, list) and key in data:
|
||||||
|
format_data[key] = filter_fields(data[key], sub_valid_fields)
|
||||||
|
else:
|
||||||
|
if field in data:
|
||||||
|
format_data[field] = data[field]
|
||||||
|
return format_data
|
Reference in New Issue
Block a user