def get_new_board(difficulty): try: payload = verify_decode_jwt(get_token_auth_header()) # Check if the user (auth_id) already exists user = None user_info = payload["http://www.jordanhuus.com/user_info"] if User.query.filter(User.auth_id == payload["sub"]).count() > 0: user = User.query.filter(User.auth_id == payload["sub"]) \ .first() else: first_name = user_info["name"].split(" ")[0] last_name = first_name if len(user_info["name"].split(" ")) \ == 1 else user_info["name"].split(" ")[1] user = User(first_name, last_name, user_info["id"]) user.add() # Store the newly created board board = SudokuBoard(difficulty, user) board.add() except KeyError as ke: abort(400, f"Request body is missing {ke} dictionary key.") except Exception: abort(500) return jsonify(board.format()), 200
def save_board(): try: # User info payload = verify_decode_jwt(get_token_auth_header()) data = json.loads(request.data) # Update the board board = SudokuBoard.query.get(data["board_id"]) board.board_json = json.dumps(data["board_json"]) board.update() # Return all boards boards = SudokuBoard.query.filter(User.auth_id == payload["sub"]) boards_data = [board.format() for board in boards] except KeyError as ke: abort(400, "Request to save board is missing " + str(ke) + ".") except Exception: abort(500) return jsonify({ "success": True, "saved_board_id": board.id, "user_boards": boards_data }), 200
def book_reservation(jwt): data = request.get_json() token = request.headers.environ['HTTP_AUTHORIZATION'].split()[1] user = verify_decode_jwt(token)['sub'] customer = db.session.query(Customer).filter(Customer.name == user).first() format = "%Y-%m-%d %H:%M:%S" start_date = datetime.strptime(data['start'], format) end_date = datetime.strptime(data['end'], format) reserve = Reservation(customer_id=customer.id, bike_id=data['bike_id'], start=start_date, end=end_date) db.session.add(reserve) db.session.commit() return jsonify({"success": True}), 200
def callback_handling(): # Handles response from token endpoint try: auth0.authorize_access_token() resp = auth0.get('userinfo') userinfo = resp.json() access_token = auth0.token['access_token'] accessinfo = verify_decode_jwt(access_token) # Store the user information in flask session. session['accessinfo'] = accessinfo session['isLogged'] = True return redirect('/exercices') except: print(sys.exc_info()) abort(404)
def apartments(jwt): ## get apartments with name, address, picture, number of bikes can rent and their IDs # ''' # data = [{ # "name": "Manhattan Park", # "address": "30 River Rd, New York, NY 10044", # "picture": "https://cdn-img-feed.streeteasy.com/nyc/image/36/366939736.jpg", # "total_bikes": 5, # "num_bikes_for_rent": 2, # "bikes_for_rent":[1,2] # }] # ''' token = request.headers.environ['HTTP_AUTHORIZATION'].split()[1] user = verify_decode_jwt(token)['sub'] res = db.session.query(Customer).filter(Customer.name == user).all() if not res: tmp_customer = Customer(name=user) db.session.add(tmp_customer) db.session.commit() apartment_res = db.session.query(Apartment).all() all_apartments = [] for a in apartment_res: apartment = {} bike_query = db.session.query(Bike, Reservation).join( Reservation, isouter=True).filter(Bike.apartment_id == a.id) bike_res = bike_query.all() total_bikes = len(bike_res) apartment['id'] = a.id apartment['name'] = a.name apartment['address'] = a.address apartment['picture'] = a.picture apartment['total_bikes'] = total_bikes bikes_for_rent = [] for bike, reservation in bike_res: if not reservation: bikes_for_rent.append(bike.id) apartment['num_bike_for_rent'] = len(bikes_for_rent) apartment['bikes_for_rent'] = bikes_for_rent all_apartments.append(apartment) return jsonify(all_apartments)
def delete_board(board_id): try: # User info payload = verify_decode_jwt(get_token_auth_header()) # Update the board board = SudokuBoard.query.get(board_id) if board is None: raise TypeError board.delete() # Return all boards boards = SudokuBoard.query.filter(User.auth_id == payload["sub"]) boards_data = [board.format() for board in boards] except TypeError: abort(400, f"Board ID {board_id} was not found.") except Exception: abort(500) return jsonify(boards_data), 200
def get_my_reservation(): token = request.headers.environ['HTTP_AUTHORIZATION'].split()[1] user = verify_decode_jwt(token)['sub'] customer = db.session.query(Customer).filter(Customer.name == user).first() Reservations = db.session.query( Bike, Reservation).join(Bike).join(Apartment).filter( Reservation.customer_id == customer.id).all() res = [] for b, r in Reservations: a = db.session.query(Apartment).filter( Apartment.id == b.apartment_id).first() res.append( dict(bike_id=r.bike_id, bike_name=b.name, picture=b.picture, apartment=a.name, customer_id=r.customer_id, start=str(r.start), end=str(r.end))) return jsonify(res), 200
def get_user_boards_from_database(user_id): # Confirm the user_id matches the JWT claim payload = verify_decode_jwt(get_token_auth_header()) token_claim_user_id = payload["sub"] if payload["sub"] != user_id: abort( 401, f"Unauthorized; provided user ID, {user_id}, " + f"does not match token claim, {token_claim_user_id}.") try: boards = SudokuBoard.query.filter(User.auth_id == user_id) if boards.count() == 0: raise TypeError boards_data = [board.format() for board in boards] except TypeError: abort( 401, f"Request to user boards with user ID {user_id} " + "is not authorized.") except Exception: abort(500) return jsonify(boards_data), 200