def post(self, room_id): current_user = User.find_by_username(get_jwt_identity()) room = Room.query.filter_by(id=room_id).first() if room is None: return {"status": "error", 'message': 'Room does not exist.'}, 400 elif current_user in room.members: return { "status": "error", 'message': 'You are already in this room.' }, 400 else: # user login db.session.execute(joins.insert().values(user_id=current_user.id, room_id=room_id)) db.session.commit() socketio.emit('member', { 'data': { 'room_id': room.id, 'action': 'member_enter', 'data': user_schema.dump(current_user), 'message': 'Member ' + current_user.username + ' enter the room.' } }, room=room_id) return { "status": "success", "message": "You successfully enter room " + room.name, "data": room_schema.dump(room) }, 200
def add_user(): content = request.json gender = None if content is None else content.get('gender') year_of_birth = None if content is None else content.get('year_of_birth') resulting_user = app_controller.add_user(gender, year_of_birth) return jsonify(user_schema.dump(resulting_user).data)
def update_user_password(user_id: int, args: dict): user = User.query.get_or_404( user_id, description=f"User with id {user_id} not found") if not user.is_password_valid(args["current_password"]): abort(401, description="Invalid password") user.password = user.generate_hashed_password(args["new_password"]) db.session.commit() return jsonify({"success": True, "data": user_schema.dump(user)})
def create_user(): try: user = user_schema.loads(request.data) except ValidationError as err: return error_response(400, err.messages) if db.get_user_by_name(user.name): return error_response(400, "User already exists.") user = db.add_user(user) if user is None: return error_response(400) response = jsonify(user_schema.dump(user)) response.status_code = 201 response.headers["Location"] = url_for("api.get_user", id=user.id) return response
def delete(self, room_id): current_user = User.find_by_username(get_jwt_identity()) room = Room.query.filter_by(id=room_id).first() if room is None: return {"status": "error", 'message': 'Room does not exist.'}, 400 elif current_user not in room.members: return { "status": "error", 'message': 'You are not in this room.' }, 400 elif room.members.count() == 1: # delete room + delete song + delete joins db.session.execute( delete(joins).where(joins.c.user_id == current_user.id).where( joins.c.room_id == room_id)) room.current_song_id = None for song in room.playlist: s3.delete_object(Bucket=config.S3_BUCKET, Key=song.link[len(config.S3_LOCATION):]) db.session.delete(room) else: if current_user is room.owner: # make some one owner , then delete join new_owner = room.members[1] room.owner_id = new_owner.id db.session.execute( delete(joins).where( joins.c.user_id == current_user.id).where( joins.c.room_id == room_id)) message = "User " + new_owner.username + " becomes room owner." \ " User " + current_user.username + " left the room." else: db.session.execute( delete(joins).where( joins.c.user_id == current_user.id).where( joins.c.room_id == room_id)) message = "User " + current_user.username + " left the room." socketio.emit('member', { 'data': { 'room_id': room_id, 'action': 'member_leave', 'data': user_schema.dump(current_user), 'message': message } }, room=room_id) db.session.commit() return {"status": "success", "message": "You have left the room."}, 200
def user(user_id): claims = get_jwt_claims() if claims['role'] > 0 and get_jwt_identity() != user_id: abort(403, "You are not allowed to view this resource") user = User.query.get(user_id) if not user: abort(404, "User not found") if request.method == 'GET': res = user_schema.dump(user) return jsonify(res), 200 elif request.method == 'DELETE': db.session.delete(user) db.session.commit() return jsonify(), 204 else: abort(405, "The method is not allowed for the requested URL.")
def register(): data = request.get_json() hashed_password = generate_password_hash(data['password'], method='sha256') result = User.query.filter_by(email=data["email"]).first() if result: #check if email alredy in the database if result.email == data["email"]: return jsonify({"message":"User already exist"}), 403 new_user = User(username=data['username'], email=data['email'],password=hashed_password) db.session.add(new_user) db.session.commit() result = user_schema.dump(new_user) return jsonify({"message": "user created", "new_user": result}), 201
def update_user_data(user_id: int, args: dict): if User.query.filter(User.username == args["username"]).first(): abort( 409, description=f'User with username {args["username"]} already exists' ) if User.query.filter(User.email == args["email"]).first(): abort(409, description=f'User with email {args["email"]} already exists') user = User.query.get_or_404( user_id, description=f"User with id {user_id} not found") user.username = args["username"] user.email = args["email"] db.session.commit() return jsonify({"success": True, "data": user_schema.dump(user)})
def update_user(id): if id != get_jwt_identity(): return error_response(403) try: user = user_schema.loads(request.data) except ValidationError as err: return error_response(400, err.messages) if db.get_user(id) is None: return error_response(404) # "id" in request data is optional if user.id == 0: user.id = id # if "id" was provided in request data then it has to match the resource id if user.id != id: return error_response(400, "Request data id has to match resource id.") if not db.update_user(user): return error_response(400) response = jsonify(user_schema.dump(user)) return response
def register(): if not request.is_json: abort(400, "Missing JSON in request") username = request.json.get('username', None) if not username: abort(400, "Missing username parameter") password = request.json.get('password', None) if not password: abort(400, "Missing password parameter") if User.query.filter_by(username=username).first() != None: abort(409, "Username already registered") try: email = request.json.get('email', None) new_user = user_schema.load({'username': username, 'email': email}) except ValidationError as e: abort(400, e.messages) if User.query.filter_by(email=email).first() != None: abort(409, "Email already registered") new_user.set_password(password) db.session.add(new_user) db.session.commit() return user_schema.dump(new_user), 201
def get_user(id): user = db.get_user(id) if user is None: return error_response(404) return jsonify(user_schema.dump(user))
def get_users(): return jsonify(user_schema.dump(db.all_users(), many=True))
def get_user(user_id): user_info = app_controller.get_user_info(user_id) return abort(404) if user_info is None else jsonify( user_schema.dump(user_info).data)
def get_current_user(user_id: int): user = User.query.get_or_404( user_id, description=f"User with id {user_id} not found") return jsonify({"success": True, "data": user_schema.dump(user)})