You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
92 lines
3.2 KiB
92 lines
3.2 KiB
3 months ago
|
from datetime import datetime, date
|
||
|
|
||
|
|
||
|
def db_page(entity_filter, order, data, number):
|
||
|
"""分页帮助方法.
|
||
|
|
||
|
对db.session出来的实体进行分页
|
||
|
|
||
|
Args:
|
||
|
entity_filter: an entity that need pagination.
|
||
|
order: Sort by.
|
||
|
data: get pageNumber.
|
||
|
number: Number of lists per page
|
||
|
|
||
|
Returns:
|
||
|
可供json转换的dict
|
||
|
|
||
|
Raises:
|
||
|
TYPEError: 类型转换出错
|
||
|
"""
|
||
|
entityPaginate = entity_filter.order_by(order.desc()).paginate(
|
||
|
int(data.get('pageNumber')), number,
|
||
|
error_out=False)
|
||
|
resultList = []
|
||
|
for (i, result) in enumerate(entityPaginate.items):
|
||
|
result_modified = dict(zip(result.keys(), result))
|
||
|
for (j, child) in enumerate(result):
|
||
|
if type(child) == date or type(child) == datetime:
|
||
|
result_modified.__setitem__(result.keys()[j], str(child))
|
||
|
resultList.append(result_modified)
|
||
|
return resultList
|
||
|
|
||
|
|
||
|
def db_page_entity(entity, data, number):
|
||
|
"""分页帮助方法.
|
||
|
|
||
|
对实体查询出来的实体集合进行分页
|
||
|
|
||
|
Args:
|
||
|
entity: an entity that need pagination.
|
||
|
data: get pageNumber.
|
||
|
number: Number of lists per page
|
||
|
|
||
|
Returns:
|
||
|
可供json转换的dict
|
||
|
|
||
|
Raises:
|
||
|
TYPEError: 类型转换出错
|
||
|
"""
|
||
|
data_list = entity.paginate(int(data.get('pageNumber')), number, error_out=False)
|
||
|
result_list = [dict(zip([attr for attr in dir(result) if '_' not in attr and
|
||
|
attr is not 'query' and
|
||
|
attr is not 'metadata'],
|
||
|
[str(getattr(result, attr)) if
|
||
|
type(getattr(result, attr)) == date or
|
||
|
type(getattr(result, attr)) == datetime else
|
||
|
getattr(result, attr) for attr in dir(result) if '_' not in attr and
|
||
|
attr is not 'query' and
|
||
|
attr is not 'metadata']))
|
||
|
for result in data_list.items]
|
||
|
return result_list
|
||
|
|
||
|
|
||
|
def entityDictToStringDict(resultList):
|
||
|
"""
|
||
|
没啥用的函数,以前用来转换json数据为string模式的,更新了之后可以支持所有类型的数据 以前是因为json.dumps()不支持decimal类型的数据
|
||
|
用List2Json中的方法转换为float就解决了
|
||
|
"""
|
||
|
jsonList = []
|
||
|
for dic in resultList:
|
||
|
dicItem = {}
|
||
|
for key, value in dic.items():
|
||
|
if value is None:
|
||
|
value = ''
|
||
|
dicItem[key] = str(value)
|
||
|
jsonList.append(dicItem)
|
||
|
return jsonList
|
||
|
|
||
|
|
||
|
def singleEntityToJson(singleEntity):
|
||
|
"""
|
||
|
单条数据转json,不需要分页的
|
||
|
"""
|
||
|
dictList = [dict(zip([attr for attr in dir(singleEntity) if '_' not in attr and
|
||
|
attr is not 'query' and
|
||
|
attr is not 'metadata'],
|
||
|
[getattr(singleEntity, attr) for attr in dir(singleEntity)
|
||
|
if '_' not in attr and
|
||
|
attr is not 'query' and
|
||
|
attr is not 'metadata']))]
|
||
|
return dictList[0]
|