def verify(): token = request.headers.get('authorization') if not token: raise MyException('Must include token in request header!') user = User.verify_token(token) # type: User if user: return jsonify({'verify': 'Success', 'user': user.username}) else: raise MyException('Token is invalid!')
def put(user_id): if not request.json or 'username' not in request.json or 'password' not in request.json: raise MyException( 'request payload must be JSON format and all field off entity `user` should be included!' ) user = User.query.get(user_id) if user: user.username = request.json.get('username') user.password = request.json.get('password') return jsonify(user.get_json()) else: raise MyException('user id is invalid!')
def login(): if not request.json or 'username' not in request.json: raise MyException('request body must be in JSON format!') user = User.query.get(request.json.get('username')) # type: User if not user: raise MyException('Username is invalid!') if user.password == request.json.get('password'): return jsonify({ 'login': '******', 'token': user.generate_token().decode('utf-8') }) else: raise MyException('Password is incorrect!')
def post(): if not request.json or not ('username' in request.json and 'password' in request.json): raise MyException( 'request payload must be JSON format and ALL field off entity `user` should be included!' ) user = User.query.get(request.json.get('username')) if user: raise MyException('username already exist!') else: user = User(username=request.json.get('username'), password=request.json.get('password')) db.session.add(user) db.session.commit() return jsonify(user.get_json())
def delete(user_id): user = User.query.get(user_id) if user: db.session.delete(user) return jsonify( {'message': 'Successfully delete user {id}'.format(id=user_id)}) else: raise MyException('user id is invalid!')
def patch(user_id): if not request.json: raise MyException('request payload must be JSON format!') user = User.query.get(user_id) if user: # check username or password is contained or not username = request.json.get('username') password = request.json.get('password') if not username and not password: raise MyException('At least include one field off entity `user`!') if username: user.username = username if password: user.password = password return jsonify(user.get_json()) else: print('User id is invalid!') raise MyException('user id is invalid!', 400)
def get_one(user_id): user = User.query.filter_by(id=user_id).first() if user: return jsonify(user.get_json()) else: raise MyException('user id is invalid!')
def handle_my_exception(error: MyException): response = jsonify(error.to_dict()) response.status_code = error.status_code return response