Flask是Python3开发平台的小型框架,是DJango框架的轻量版,所谓的轻量,并不是说Flask功能没有DJango强大,而是为了提供用户开发过程中更大的灵活空间,缩减了很多框架性的东西,而不会自动生成框架性的代码。为了开发的灵活性,Flask提供了很多扩展库,如Flask-sqlalchemy,就是对数据库操作的扩展库。
平台开发,咱们经常碰到的就是分页展示功能,几乎每个平台都会使用到,而Flask-sqlalchemy分页采用paginate方法,简单粗暴,直接上代码,以下是获取用户列表的分页代码:
@user_sys.route("/user/query", methods=['GET'])
def query_users():
# 判断当前登陆的用户有效性
if not isCurrentUserValid(current_user):
return getResultAnd2Josn(CODE_CURRENT_USER_INVALID,MSG_CURRENT_USER_INVALID, {})
# 接收并解析请求的json数据
res = request.get_data()
data = json.loads(res)
# 当前选中的组织机构ID(按需求来,该项目的需求是查看当前组织机构的用户列表)
organization_id = data.get('organization_id')
if organization_id <= 0:
return getResultAnd2Josn(CODE_FAILED, "Organization had no selected", {'total': 0, 'items': []})
# 请求的页码
page_num = data.get('page_num')
# 每页展示数量
data_count = data.get('data_count')
# 排序, 1-注册时间降序;2-注册时间升序;3-姓名降序;4-姓名升序
order_by = data.get('order_by')
if page_num >= 0 and data_count > 0:
user_order_by = User.createDate.desc()
if 2 == order_by:
user_order_by = User.createDate.asc()
elif 3 == order_by:
# 为了兼容中文,需要CONVERT
user_order_by = func.CONVERT(literal_column('name using gbk')).desc()
elif 4 == order_by:
user_order_by = func.CONVERT(literal_column('name using gbk')).asc()
# 搜索字符串
filter = data.get('filter')
if None == filter or len(filter) == 0:
my_current_user = getCurrentuser()
if my_current_user:
if not is_common_user(my_current_user):
organization = Organization.query.get(my_current_user.organizationId)
sub_organization_ids = organization.get_all_sub_organizations_id()
sub_organization_ids.append(organization_id)
# 重点:分页用户列表,通过BaseQuery类的paginate方法进行分页查询,返回Pagination实例,该该包含了数据列表、数据总量,当前页等信息
user_paginate = User.query.order_by(user_order_by).filter(User.organizationId.in_(sub_organization_ids)).paginate(page=page_num, per_page=data_count)
# 返回json给前端
return get_json_users_by_user_paginate(user_paginate)
else:
# my_organization_id = my_current_user.organizationId
user_paginate = User.query.order_by(user_order_by).filter(and_(User.organizationId == organization_id, User.id == my_current_user.id)).paginate(page=page_num, per_page=data_count)
return get_json_users_by_user_paginate(user_paginate)
else:
return getResultAnd2Josn(CODE_CURRENT_USER_INVALID, MSG_CURRENT_USER_INVALID, {})
else:
my_current_user = getCurrentuser()
if my_current_user:
if not is_common_user(my_current_user):
organization = Organization.query.get(my_current_user.organizationId)
sub_organization_ids = organization.get_all_sub_organizations_id()
sub_organization_ids.append(organization_id)
user_paginate = User.query.order_by(user_order_by).filter(and_(User.organizationId.in_(sub_organization_ids), or_(User.name.like('%'+filter+"%"), User.account.like('%' + filter + "%"))), and_(User.organizationId == organization_id)).paginate(page=page_num, per_page=data_count)
return get_json_users_by_user_paginate(user_paginate)
else:
user_paginate = User.query.order_by(user_order_by).filter(
and_(User.organizationId == organization_id, User.id == my_current_user.id,
or_(User.name.like('%' + filter + '%'), User.account.like('%' + filter + '%'))),
and_(User.organizationId == organization_id)).paginate(page=page_num, per_page=data_count)
return get_json_users_by_user_paginate(user_paginate)
else:
return getResultAnd2Josn(CODE_CURRENT_USER_INVALID, MSG_CURRENT_USER_INVALID, {})
return getResultAnd2Josn(CODE_FAILED, MSG_FAILED, {'total': 0, 'items': []})