def wrapper(*args, **kwargs): auth = Auth() result = auth.identify(request) if (result['status'] and result['data']): return func(*args, **kwargs) else: return jsonify(result)
def login(): username = request.form.get("username") password = request.form.get("password") if (not username or not password): return jsonify(common.falseReturn('', '用户名或密码不能为空')) else: auth = Auth() return auth.authenticate(username, password)
def info(): auth = Auth() result = auth.identify(request) if (result['status'] and result['data']): user = User.query.filter_by(id=result['data']).first() returnUser = { 'id': user.id, 'username': user.username, 'email': user.email, 'nickname': user.nickname, 'avatar': "/static/imgs/user/avatar/%s" % user.real_avatar } result = common.trueReturn(returnUser, "请求成功") return jsonify(result)
def post(self): iauth = Auth.identify(Auth, request) if iauth['status']: if iauth['data']['isAdmin']: name = request.json.get('groupname') about = request.json.get('about') if check_group(name): group = Groups(name=name, about=about) res = Groups.add(group) if group.id: result = common.trueReturn( { 'id': group.id, 'name': group.name }, "请求成功") else: result = common.falseReturn(res, '请求失败') else: result = common.falseReturn('该分组已存在', '请求失败') else: result = common.falseReturn('权限不足.', "请求失败") else: result = common.falseReturn(iauth['data'], "请求失败") return jsonify(result)
def post(self): iauth = Auth.identify(Auth, request) if iauth['status']: try: user = Users.query.filter_by( username=request.json.get('username')).first() user.password = Users.hash_password( Users, request.json.get('password')) user.name = request.json.get('name') user.email = request.json.get('email') user.about_me = request.json.get('about_me') a = user.update() resp = { "code": 0, "status": True, "msg": "用户资料更新成功", "data": "" } except Exception as e: resp = { "code": 10041, "status": False, "msg": "用户资料更新失败", "data": e } else: resp = { "code": 10042, "status": False, "msg": "用户资料更新失败", "data": iauth['data'] } return jsonify(resp)
def get(self): result = Auth.identify(Auth, request) if result['status']: user = Users.query.filter_by( username=result["data"]["username"]).first() print user.group.gusers.all() # Groups.delete(Groups, 1) else: result = common.falseReturn(result['data'], "请求失败") return jsonify(result)
def get(self, groupid): iauth = Auth.identify(Auth, request) if iauth['status']: if iauth['data']['isAdmin']: try: Groups.delete(Groups, groupid) result = common.trueReturn('删除分组成功.', "请求成功") except Exception as e: result = common.falseReturn(e, "请求失败") else: result = common.falseReturn('权限不足.', "请求失败") else: result = common.falseReturn(iauth['data'], "请求失败") return result
def upload_avatar(): ''' 获取图片 :return: ''' auth = Auth() result_auth = auth.identify(request) #验证token if (result_auth['status'] and result_auth['data']): file = request.files['file'] if file: filename = "%s.jpg" % (result_auth['data']) path = os.path.join(os.getcwd(), 'static', 'imgs', 'user', 'avatar', filename) user = User.query.filter_by(id=result_auth['data']).first() user.real_avatar = filename user.update() file.save(path) result = {'filename': filename} else: return jsonify(common.falseReturn('', '保存失败')) return jsonify(common.trueReturn(result, '保存成功')) else: return jsonify(result_auth)
def get(self): iauth = Auth.identify(Auth, request) if iauth['status']: try: g_list = [] for g in Groups.query.all(): g_list.append({ "id": g.id, "name": g.name, "about": g.about, }) result = common.trueReturn(g_list, u"请求成功") except Exception as e: result = common.falseReturn(e, "请求失败") else: result = common.falseReturn(iauth['data'], "请求失败") return result
def post(self): iauth = Auth.identify(Auth, request) if iauth['status']: try: group = Groups.query.filter_by( id=request.json.get('id')).first() if not check_group(request.json.get('groupname')): result = common.falseReturn('该分组名字已存在', "请求失败") else: group.name = request.json.get('groupname') group.about = request.json.get('about') a = group.update() result = common.trueReturn('分组更新成功', "请求成功") except Exception as e: result = common.falseReturn(e, "请求失败") else: result = common.falseReturn(iauth['data'], "请求失败") return jsonify(result)
def get(self, userid): iauth = Auth.identify(Auth, request) if iauth['status']: if userid == iauth['data']['id']: resp = { "code": 10051, "status": False, "msg": "用户删除失败", "data": "不能删除自己" } if iauth['data']['isAdmin']: try: Users.delete(Users, userid) resp = { "code": 0, "status": True, "msg": "用户删除成功", "data": "" } except Exception as e: resp = { "code": 10052, "status": False, "msg": "用户删除失败", "data": e } else: resp = { "code": 10053, "status": False, "msg": "用户删除失败", "data": "权限不足" } else: resp = { "code": 10053, "status": False, "msg": "用户删除失败", "data": iauth['data'] } return jsonify(resp)
def post(self): username = request.json.get('username') password = request.json.get('password') user = Users.query.filter_by(username=username).first() if user: if user.verify_password(password): user.last_seen = datetime.now() a = user.update() # print a # return jsonify(common.trueReturn('', "用户登录成功")) token = Auth.authenticate(Auth, user) resp = { "code": 0, "status": True, "msg": "登录成功", "data": { "token": token, "name": user.name or user.username, "isAdmin": user.isAdmin } } else: resp = { "code": 10001, "status": False, "msg": "用户名或密码错误", "data": "" } else: resp = { "code": 10001, "status": False, "msg": "用户名或密码错误", "data": "" } return jsonify(resp)
def get(self): iauth = Auth.identify(Auth, request) if iauth['status']: try: u_list = [] for u in Users.query.all(): u_list.append({ "id": u.id, "username": u.username, "name": u.name, "email": u.email, "isAdmin": u.isAdmin, "member_since": str(u.member_since), "about_me": u.about_me, }) resp = { "code": 0, "status": True, "msg": "用户列表获取成功", "data": u_list } except Exception as e: resp = { "code": 10031, "status": False, "msg": "用户列表获取失败", "data": e } else: resp = { "code": 10032, "status": False, "msg": "用户列表获取失败", "data": iauth['data'] } return jsonify(resp)
def logout(): auth = Auth() result = auth.kill_auth(request) return jsonify(result)