159 lines
4.6 KiB
Python
159 lines
4.6 KiB
Python
# import json
|
|
# import time
|
|
# import zlib
|
|
# from datetime import datetime
|
|
# from time import mktime
|
|
#
|
|
# from flask import request, g
|
|
# from nc_http.tools.helpers import strip_value
|
|
#
|
|
#
|
|
# 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
|
|
#
|
|
#
|
|
# def get_client_ip(request):
|
|
# """
|
|
# 获取客户端 ip
|
|
# :param request:
|
|
# :return:
|
|
# """
|
|
# x_forwarded_for = request.headers.get('X-Forwarded-For')
|
|
# if x_forwarded_for:
|
|
# ips = x_forwarded_for.split(',')
|
|
# return ips[0].strip()
|
|
# return request.headers.get('X-Real-Ip', request.remote_addr)
|
|
#
|
|
#
|
|
# def get_time(data, is_datetime=False):
|
|
# if data:
|
|
# if isinstance(data, int):
|
|
# return data
|
|
#
|
|
# if isinstance(data, datetime):
|
|
# return int(time.mktime(data.timetuple()))
|
|
#
|
|
# _data = None
|
|
# if data.isdigit():
|
|
# _data = int(data)
|
|
# if is_datetime:
|
|
# _data = datetime.fromtimestamp(_data)
|
|
# else:
|
|
# if data.count('-') == 2:
|
|
# date_format = '%Y-%m-%d'
|
|
# elif data.count('-') == 1:
|
|
# date_format = '%Y-%m'
|
|
# elif data.count('/') == 2:
|
|
# date_format = '%Y/%m/%d'
|
|
#
|
|
# else:
|
|
# date_format = ''
|
|
#
|
|
# if data.count(':') == 2:
|
|
# time_format = '%H:%M:%S'
|
|
# elif data.count(':') == 1:
|
|
# time_format = '%H:%M'
|
|
# else:
|
|
# time_format = ''
|
|
#
|
|
# if date_format or time_format:
|
|
# if ' ' in data:
|
|
# datetime_format = '{} {}'.format(date_format, time_format)
|
|
# else:
|
|
# datetime_format = '{}{}'.format(date_format, time_format)
|
|
#
|
|
# _data = datetime.strptime(data, datetime_format)
|
|
# if is_datetime is False:
|
|
# _data = int(mktime(_data.timetuple()))
|
|
# else:
|
|
# if is_datetime:
|
|
# return None
|
|
# return 0
|
|
# else:
|
|
# if is_datetime:
|
|
# return None
|
|
# return 0
|
|
#
|
|
# return _data
|
|
#
|
|
#
|
|
# def camelize(uncamelized_str):
|
|
# 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):
|
|
# 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 uncamelize_dict(d):
|
|
# return {uncamelize(k): v for k, v in d.items()}
|
|
#
|
|
#
|
|
# def camelize_dict(d):
|
|
# return {camelize(k): v for k, v in d.items()}
|
|
#
|
|
#
|
|
# def get_request_json(is_uncamelize=True):
|
|
# """
|
|
# 获取 json 传递参数
|
|
# :return:
|
|
# """
|
|
# if request.method.lower() == 'get':
|
|
# data = request.args.to_dict()
|
|
# else:
|
|
# if request.content_encoding and 'gzip' in request.content_encoding:
|
|
# json_data = zlib.decompress(request.get_data())
|
|
# 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)
|
|
#
|
|
# return g.request_data
|
|
#
|
|
#
|
|
# def get_params():
|
|
# data = request.args.to_dict()
|
|
# if request.method.lower() != 'get':
|
|
# if request.content_encoding and 'gzip' in request.content_encoding:
|
|
# json_data = zlib.decompress(request.get_data())
|
|
# data.update(json.loads(json_data))
|
|
# else:
|
|
# data.update(request.get_json(force=True, silent=True) or {})
|
|
# return data
|