def testGetUsersScanCount(self): from model.user import UserService import logging logger = logging.getLogger('peewee') logger.addHandler(logging.StreamHandler()) logger.setLevel(logging.DEBUG) UserService.get_users_scan_count()
def list_users(): """ 显示用户列表 一个用户包含 用户名,扫描次数,最新扫描时间,扫描记录 动作包含是否加入管理员等等 v2.5 可条件查询 路由请求格式 fullname=XX&department=&role= 参数值为''表示所有 fullname=XX 参数名也表示所有,例如这里表示只查询fullname :return: """ def change_user_dic(user): """ 将user转换成DIC :param user: :return: """ # from model.user_task import UserTask, UserTaskService # scan_count = UserTaskService.count(where=(UserTask.user_id == user.id)) recent_operation_time = user.recent_operation_time.strftime( "%Y-%m-%d %H:%M") if user.recent_operation_time else "" return { "id": user.id, "user_name": user.user_name, "full_name": user.full_name, "mobile_phone": user.mobile_phone, "email": user.email, "depart_ment": user.dept_name, "role": user.role, "scan_count": user.scan_count, "recent_operation_time": recent_operation_time, "user_info": user.user_info } def generate_search_query(full_name, depart_ment, role, id): result = list() if full_name is not None and full_name != "": result.append(User.full_name == full_name) if depart_ment is not None and depart_ment != "": result.append(User.dept_name == depart_ment) if role is not None and role != "": result.append(User.role == role) if id is not None and id != "": result.append(User.id == id) return result try: full_name = request.args.get("full_name") depart_ment = request.args.get('depart_ment') role = request.args.get("role") id = request.args.get("id") # 构造条件查询元组 query = generate_search_query(full_name, depart_ment, role, id) if len(query) > 0: users = [ change_user_dic(user) for user in UserService.get_users_scan_count( where=tuple(query)) ] else: users = [ change_user_dic(user) for user in UserService.get_users_scan_count() ] return jsonify(status=200, message="查询成功", data=users) except Exception as e: logger.exception("list_user raise error") return jsonify(status=500, message="未知异常", data={"extra_info": "发生未知异常,请联系管理员查看异常日志"})