def post(self, flight_id): """POST method to add a new flight booking""" data = request.get_json() seat = 1 if data: seat = data.get('seat') current_user = get_jwt_identity() try: flight = get_flight(flight_id) if not flight: return generate_response('Selected flight not available', 400) if seat == 1 and flight.booked_economy < flight.airplane.economy_seats: data = dict(booked_economy=flight.booked_economy + 1) save_booking(current_user, flight_id) flight.update(flight, **data) return generate_response( 'Economy seat flight reservation successfull', 201) if seat == 2 and flight.booked_business < flight.airplane.business_seats: data = dict(booked_business=flight.booked_business + 1) save_booking(current_user, flight_id) flight.update(flight, **data) return generate_response( 'Business seat flight reservation successfull', 201) except Exception as e: db.session.rollback() return jsonify({'error': str(e)}), 401
def post(self, name, country, city): """POST method to add a new airport details""" current_roles = get_jwt_claims()['roles'] if not current_roles: return generate_response("Forbidden, Admins only!", 403) try: save_airport(name.upper(), country.upper(), city.upper()) return generate_response('Airport registered successfully', 201) except Exception as e: db.session.rollback() return jsonify({'error': str(e)}), 401
def post(self, departure_date, departure_airport_id, arrival_date, arrival_airport_id, airplane_id): """POST method to add a new flight schedule""" current_roles = get_jwt_claims()['roles'] if not current_roles: return generate_response("Forbidden, Admins only!", 403) try: save_flight(departure_date, departure_airport_id, arrival_date, arrival_airport_id, airplane_id) return generate_response('Flight schedule added successfully', 201) except Exception as e: db.session.rollback() return jsonify({'error': str(e)}), 401
def post(self, reg_number, economy_seats, business_seats, first_class_seats): """POST method to add a new airplane details""" current_roles = get_jwt_claims()['roles'] if not current_roles: return generate_response("Forbidden, Admins only!", 403) try: save_airplane(reg_number.upper(), economy_seats, business_seats, first_class_seats) return generate_response('Airplane registered successfully', 201) except Exception as e: db.session.rollback() return jsonify({'error': str(e)}), 401
def delete(self): """Endpoint to delete passport photograph""" current_user = get_jwt_identity() data = dict(passport=None) try: user = get_user(current_user) if not user.passport: return generate_response('Cannot delete blank image file', 401) delete_image(user) user.update(user, **data) return generate_response('File deleted successfully', 200) except Exception as e: db.session.rollback() return jsonify({'message': str(e)}), 401
def get(self, flight_id): """Return a list of all reservations in a given day""" booking_date = request.args.get('bdate', datetime.now().strftime('%b %d %Y'), type=str) current_user = get_jwt_identity() try: flight = get_flight(flight_id) if not flight: return generate_response('Selected flight not available', 400) bookings_data = filter_booking_by_flight(flight_id) bookings = [ booking.serialize() for booking in bookings_data if booking.booking_date.strftime('%b %d %Y') == booking_date ] response = { 'booking_details': bookings, 'number_of_booking': len(bookings), 'message': "Data retrived successfully" } return jsonify(response), 200 except Exception as e: db.session.rollback() return jsonify({'error': str(e)}), 401
def put(self, passport_image): """Endpoint to upload passport photograph""" current_user = get_jwt_identity() filename = secure_filename(passport_image.filename) data = dict(passport=str(filename)) try: user = get_user(current_user) if not user: return generate_response('Login to continue', 401) if user.passport: delete_image(user) user.update(user, **data) except Exception as e: db.session.rollback() return jsonify({'message': str(e)}), 401 else: save_image(passport_image, filename) return generate_response('File uploaded successfully', 201)
def post(self, email, password, name): """POST method to register a user""" if not isinstance(check_email(email), str): return check_email(email) if check_name(name): return check_name(name) if check_password(password): return check_password(password) valid_email = check_email(email) if search_email(valid_email): return generate_response('User already exists, Please login', 409) try: save_user(valid_email, name, password) return generate_response('Account created successfully', 201) except Exception as e: db.session.rollback() return jsonify({'error': str(e)}), 401
def post(self, email, password): """Endpoint to login a user""" valid_email = normalize_email(email) try: user = search_email(valid_email) if user and user.password_is_valid(password): return generate_token('Login successfull', user) return generate_response( 'Invalid email or password' + ' Please try again', 401) except Exception as e: return jsonify({'message': str(e)}), 401
def get(self): """Return a list of all flights""" try: flights_data = Flight.get_all() if not flights_data: return generate_response('No data to display', 200) flights = [flight.serialize() for flight in flights_data] response = { 'flights': flights, 'message': "Data retrived successfully" } return jsonify(response), 200 except Exception as e: db.session.rollback() return jsonify({'error': str(e)}), 401
def get(self): """return a list of all airplanes""" try: airplanes_data = Airplane.get_all() if not airplanes_data: return generate_response('No data to display', 200) airplanes = [airplane.serialize() for airplane in airplanes_data] response = { 'airplanes': airplanes, 'number_of_airplanes': len(airplanes), 'message': "Data retrived successfully" } return jsonify(response), 200 except Exception as e: db.session.rollback() return jsonify({'error': str(e)}), 401