def add_group(self, group_name, desc): if self.is_group_exists(group_name): logger.info(f"组名{group_name}已存在") return http_response(StatusCode.failed, '组名已存在!') group = self.group_dao.create_group(group_name, desc) logger.info(f"创建组{group_name}成功") return http_response(status_code.success, "创建成功", data={"groupId": group.id})
def user_register(self, username, password, password2, email): if password != password2: return http_response(status_code.bad_request, "两次密码输入不一致") user = self.user_dao.get_user(username=username) if user: logger.info(f"用户名{username}已存在") return http_response(status_code.failed, "用户名已存在") self.user_dao.create_user(username, password, email) logger.info(f"用户{username}注册成功") return http_response(status_code.success, "注册成功")
def user_login(self, request, username, password): user = self.user_dao.get_user(username=username, password=password) if user: self.update_user_login_status(user.id) logger.info(f"更新用户{user.username}的登录状态成功") self.save_session(request, username) logger.info(f"用户{username}登录成功") return http_response(status_code.success, '登录成功', data={"userId": user.id}) else: logger.info(f"用户{username}登录密码或用户名错误") return http_response(status_code.failed, '用户名或密码错误')
def update_user_info(self, user_id, username, password, password2, email): if password != password2: return http_response(status_code.bad_request, "两次密码输入不一致") user = self.user_dao.get_user(id=user_id) if user.username != username: if self.user_dao.get_user(id=user_id): logger.info(f"用户名{username}已存在") return http_response(status_code.failed, "用户名已存在") user.username = username user.password = to_md5(password) user.email = email user.save() logger.info(f'用户id={user_id}更改个人信息成功') return http_response(status_code.success, '修改成功')
def load_groups(self, username): group_objs = self.group_dao.query_groups_by_username(username) result = list() for group in group_objs: result.append(self.group2json(group)) logger.info(f"用户{username}查询组列表成功") return http_response(status_code.success, "获取成功", data=result)
def wrapper(*args, **kwargs): assert len(args) > 0, "请传request参数" request = args[0] user = "" if method and request.method != method: logger.error(f"用户{user}访问{request.path}时,请求方式错误!") return http_response(StatusCode.wrong_method, '请求方式错误!') try: res = func(*args, **kwargs) logger.info(f"用户{user}访问{request.path}成功") return res except BadRequest as msg: logger.error(f"用户{user}访问{request.path}时,请求参数不正确:{str(msg)}") return http_response(StatusCode.bad_request, str(msg)) except Exception as err: logger.error(f'用户{user}访问{request.path}时,发生异常:{str(err)}' f'\n请求参数为{json.loads(request.body)},\n{traceback.format_exc()}') return http_response(StatusCode.failed, f'发生异常:{str(err)}')
def delete_groups(self, group_id_list: list): self.group_dao.delete_groups(group_id_list) logger.info(f'删除组id in{str(group_id_list)}成功') return http_response(status_code.success, "删除成功")
def delete_group(self, group_id): self.delete_group(group_id) logger.info(f'删除组id={group_id}成功') return http_response(status_code.success, '删除成功')
def update_group(self, group_id, group_name, desc): self.group_dao.update_group(group_id, group_name, desc) logger.info(f"更改id={group_id}组名为{group_name},描述为{desc}") return http_response(status_code.success, "修改成功")