def delete_table(table_id): """ Deletes a table Error status codes: 401 -- The request has been sent by an unauthenticated user, or the user is not the owner of the table's restaurant 404 -- A table with id table_id has not been found 409 -- The table has pending reservations, those must be deleted first 500 -- Error, try again later Success codes: 302 -- The deletion is accepted """ table, status = get_getaway().get_restaurants_table(current_user.rest_id, table_id) if table is None or status != 200: if status == None: flash("Sorry, an error occured. Please, try again.","error") return make_response(render_template('error.html', error = status),500) flash("A table with such id has not been found","error") return make_response(render_template("error.html", error = status), status) if current_user.rest_id != table.restaurant_id: return make_response(render_template('error.html', error="Area reserved for the restaurant operator"), 401) table, status = get_getaway().delete_restaurants_table(current_user.rest_id, table_id) if status != 204: if status == None or status == 404 or status == 500: flash("Sorry, an error occured. Please, try again.","error") return make_response(render_template('error.html', error = status),500) elif status == 409: flash("The table has pending reservations, those must be deleted first","error") return make_response(render_template("error.html", error = status), status) return redirect(f'/restaurants/{current_user.rest_id}')
def user_contacts(user_id): """ Allows you to search, given the id the contacts of a positive user Error status codes: 404 -- User not found or user not positive """ user, status = get_getaway().get_user(user_id) if user is None or status != 200: flash("User not found", "error") return make_response(render_template('error.html', error='404'), 404) if not user.is_positive: flash("User not found", "error") return make_response(render_template('error.html', error='404'), 404) users, status = get_getaway().get_user_contacts( user_id, begin=str(datetime.today() - timedelta(days=14)), end=str(datetime.today())) if status == 404: flash("User not found", "error") return make_response(render_template('error.html', error='404'), 404) elif status != 200: return make_response(render_template('error.html', error=status), status) users2 = [] for usr in users: if "flag" not in usr: users2.append(usr) users = users2 return render_template("users.html", users=users)
def _register_entrance(reservation_id): """ It allows the operator ro register the entrance of the users into the restaurant (Given a reservation) Error status code: 401 -- The user is not the restaurant operator 404 -- The reservation does not exist 409 -- Impossible to set the entrance 500 -- An error occured """ booking, code = get_getaway().get_a_booking(id=reservation_id) if booking is None or code != 200: return make_response(render_template('error.html', error=code), code) if booking['restaurant_id'] != current_user.rest_id: return make_response(render_template('error.html', error='401'), 401) _, code = get_getaway().edit_booking(booking_id=reservation_id, entrance=True) if code is not None and code == 200: flash("Entrance registered!", "success") elif code is not None and code == 404: flash("Booking not found!", "error") return make_response(render_template('error.html', error='404'), 404) elif code is not None and code == 409 or code == 400: flash("Impossible to edit the requested booking", "warning") else: flash("Sorry, an error occured. Please, try again.", "error") return make_response( render_template("reservations.html", reservations=[], title="Today's Reservations"), 500) return redirect(f"/reservations/{reservation_id}")
def restaurant_sheet(restaurant_id): """ Shows the profile page of a restaurant, which includes its name, ratings, position on the map, opening hours etc. Error status code: 404 -- No restaurant with id restaurant_id was found 500 -- Error try again later Success codes: 200 """ restaurant, status = get_getaway().get_restaurant(restaurant_id) if restaurant is None or status != 200: if status == 404: flash('No restaurant with id restaurant_id was found', "error") else: flash("Sorry, an error occured. Please, try again.","error") return make_response(render_template('error.html', error = status),status) tables, status = get_getaway().get_restaurants_tables(restaurant_id) if tables is None or status != 200: if status == None: flash("Sorry, an error occured. Please, try again.","error") return make_response(render_template('error.html', error = status),500) elif status == 404: flash("No restaurant with id restaurant_id was found", "error") return make_response(render_template("error.html", error = status), status) elif status == 204: tables = [] days = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"] closed_days = [] for day in restaurant.closed_days: closed_days.append(days[int(day) - 1]) form = RatingAddForm() return render_template("restaurantsheet.html", name=restaurant.name, rating_val=restaurant.rating_val, rating_num=restaurant.rating_num, lat=restaurant.lat, lon=restaurant.lon, phone=restaurant.phone, id=restaurant_id, closed_days=closed_days, first_opening_hour=restaurant.first_opening_hour, first_closing_hour=restaurant.first_closing_hour, second_opening_hour=restaurant.second_opening_hour, second_closing_hour=restaurant.second_closing_hour, cuisine_type=restaurant.cuisine_type, menu=restaurant.menu, tables=tables, form=form)
def _today_booking_list(restaurant_id): """ It allows the current operator to view all bookings of today Error status code: 401 -- The user is not the restaurant operator 404 -- The restaurant does not exist 500 -- An error occured """ rest, code = get_getaway().get_restaurant(restaurant_id) if code is not None and code == 404: flash("Restaurant not found!", "error") return make_response(render_template('error.html', error='404'), 404) elif code is None or code != 200 or rest is None or rest == {}: flash("Sorry, an error occured. Please, try again.", "error") return make_response( render_template("reservations.html", reservations=[], title="Today's Reservations"), 500) if current_user.rest_id != restaurant_id: return make_response(render_template('error.html', error='401'), 401) today = datetime.datetime.now() from_datetime = today.replace(hour=0, minute=0, second=0, microsecond=0) to_datetime = today.replace(hour=23, minute=59, second=59, microsecond=999999) qry, status_code = get_getaway().get_bookings( rest=restaurant_id, begin=from_datetime.isoformat(), end=to_datetime.isoformat(), with_user=True) if status_code is None or status_code != 200: flash("Sorry, an error occured. Please, try again.", "error") return make_response( render_template("reservations.html", reservations=[], title="Today's Reservations"), 500) elif qry is None: qry = [] flash("No reservations were found", "warning") return make_response( render_template("reservations.html", reservations=qry, title="Today's Reservations"), 200)
def _edit_restaurant(restaurant_id): """ Show the page for editing the restaurant with GET, the form can be submitted with POST Error status codes: 400 -- The request is not valid, the form is filled out incorrectly or a generic error has occurred 404 -- The restaurant does not exists 409 -- The current user is not an operator or the operator of this restaurant 500 -- Error try again later Success codes: 200 GET -- The form is sent to the user 302 POST -- The modification was accepted """ restaurant, status = get_getaway().get_restaurant(restaurant_id) if restaurant is None or status != 200: if status == 404: flash('No restaurant with id restaurant_id was found', "error") else: flash("Sorry, an error occured. Please, try again.","error") return make_response(render_template('error.html', error = status),status) if current_user.rest_id != restaurant_id: return make_response(render_template('error.html', error="Area reserved for the restaurant operator"), 401) if request.method == 'POST': form = RestaurantEditForm() if form.validate_on_submit(): json = DotMap() form.populate_obj(json) json = json.toDict() json["closed_days"] = [int(i) for i in json["closed_days"]] restaurant,status = get_getaway().edit_restaurant(restaurant_id, json) if restaurant is None or status != 200: if status == None: flash("Sorry, an error occured. Please, try again.","error") return make_response(render_template('error.html', error = status),500) elif status == 400: flash('Bad request or Restaurant already rated by the user', "error") elif status == 404: flash('No restaurant with id restaurant_id was found', "error") elif status == 409: flash('The restaurant has pending reservations tha conflict with the new openings, those must be deleted first', "error") elif status == 500: flash("Sorry, an error occured. Please, try again. [2]","error") return make_response(render_template("error.html", error = status), status) return redirect(f"/restaurants/{current_user.rest_id}") flash("Bad form", "error") return make_response(render_template('edit_restaurant.html', form=form), 400) form = RestaurantEditForm(obj=restaurant) return render_template('edit_restaurant.html', form=form)
def user_bookings(): """ Displays the current user's future bookings list. You must be logged in as a customer Error status codes: 404 -- If the current user is not a customer 500 -- An error occured """ if current_user.is_admin or current_user.is_health_authority or current_user.is_operator: return make_response(render_template('error.html', error='404'), 404) now = datetime.now().replace(hour=0, minute=0, second=0, microsecond=0) bookings, status_code = get_getaway().get_bookings(user=current_user.id, begin=now) if status_code is None or status_code != 200: flash("Sorry, an error occured. Please, try again.", "error") bookings = [] elif bookings is None or bookings == []: bookings = [] flash("No reservations were found", "warning") return make_response( render_template('bookings.html', bookings=bookings, title="Your Reservations"), 200)
def map_page(): """ returns a map of all restaurants in GoOutSafe You must logged as customer Error status codes: 401 -- If a user other than customer tries to view it """ if current_user.is_admin or current_user.is_health_authority or current_user.rest_id is not None: return make_response(render_template('error.html', error='401'), 401) restaurants, status = get_getaway().get_restaurants() markers_to_add = [] for restaurant in restaurants: rest = { 'icon': 'http://maps.google.com/mapfiles/ms/icons/blue-dot.png', 'lat': restaurant['lat'], 'lng': restaurant['lon'], 'infobox': restaurant['name'] } markers_to_add.append(rest) sndmap = Map( identifier="sndmap", lat=43.72, lng=10.40, markers=markers_to_add, zoom_control=True, style="height:600px;width:1000px;margin:0;", zoom=16 ) return render_template('map.html',restaurants=restaurants, sndmap=sndmap)
def _add_tables(): """ Adds a new table to the current user's restaurant Error status codes: 400 -- The requested table capacity is invalid, or the request is malformed 401 -- The request has been sent by an unauthenticated user, or the user is not the owner of a restaurant 500 -- Error, try again later Success codes: 200 -- The form is sent to the user 302 -- The addition is accepted """ form = TableAddForm() if request.method == 'POST': if form.validate_on_submit(): table, status = get_getaway().post_restaurants_tables(current_user.rest_id, int(request.form['capacity'])) if table is None or status != 201: if status == None or status == 404: flash("Sorry, an error occured. Please, try again.","error") return make_response(render_template('error.html', error = status),500) flash("Bad form", "error") return make_response(render_template("error.html", error = status), status) return redirect(f"/restaurants/{current_user.rest_id}") flash("Bad form", "error") return make_response(render_template('form.html', form=form), 400) return make_response(render_template('form.html', form=form), 200)
def _rate(restaurant_id): """ Submits a rating to a restaurant, in a range from 1 to 5 Error status code: 400 -- The currently logged-in user has already rated this restaurant, or the request is malformed 401 -- The user is not logged-in 404 -- No restaurant with id restaurant_id was found 500 -- Error try again later Success codes: 200 """ form = RatingAddForm() if form.validate_on_submit(): rating,status = get_getaway().post_restaurant_rate(restaurant_id, current_user.id, form.rating.data) if rating is None or status != 202: if status == None: flash("Sorry, an error occured. Please, try again.","error") return make_response(render_template('error.html', error = status),500) elif status == 400: flash('Bad request or Restaurant already rated by the user', "error") elif status == 404: flash('No restaurant with id restaurant_id was found', "error") elif status == 500: flash("Sorry, an error occured. Please, try again. [2]","error") return make_response(render_template("error.html", error = status), status) flash('Rating submitted', "success") return restaurant_sheet(restaurant_id) flash("Bad form", "error") return make_response(restaurant_sheet(restaurant_id), 400)
def _restaurants(message=''): """ get the list of restaurants Error status code: 500 -- Error try again later """ allrestaurants, status = get_getaway().get_restaurants() if allrestaurants is None or status != 200: flash("Sorry, an error occured. Please, try again.","error") return make_response(render_template('error.html', error = status),500) markers_to_add = [] for restaurant in allrestaurants: rest = { 'icon': 'http://maps.google.com/mapfiles/ms/icons/blue-dot.png', 'lat': restaurant.lat, 'lng': restaurant.lon, 'infobox': restaurant.name } markers_to_add.append(rest) sndmap = Map( identifier="sndmap", lat=43.72, lng=10.40, markers=markers_to_add, zoom_control=True, style="height:600px;width:1000px;margin:0;", zoom=15 ) return render_template("restaurants.html", message=message, sndmap=sndmap, restaurants=allrestaurants, base_url="/restaurants")
def login(): form = LoginForm() if request.method == 'POST': if form.validate_on_submit(): user, status = get_getaway().get_users(email=request.form['email']) if status == 200 and user is not None: password = request.form['password'] user = user[0] password_hash = user['password'] checked = check_password_hash(password_hash, password) if checked: usr = User(user['id'], user['is_operator'], user['is_admin'], user['is_health_authority'], password_hash, user['rest_id'], user['is_positive']) login_user(usr) return redirect('/') else: flash('Wrong password', 'error') return make_response( render_template('login.html', form=form), 401) flash('Wrong email', 'error') return make_response(render_template('login.html', form=form), 401) flash('Bad form', 'error') return make_response(render_template('login.html', form=form), 400) return make_response(render_template('login.html', form=form), 200)
def _edit_tables(table_id): """ Edits the info of a table Error status codes: 400 -- The requested table capacity is invalid, or the request is malformed 401 -- The request has been sent by an unauthenticated user, or the user is not the owner of the table's restaurant 404 -- A table with id table_id has not been found 409 -- The table has pending reservations tha conflict with the new capacity, those must be deleted first 500 -- Error, try again later Success codes: 200 GET -- The form is sent to the user 302 POST -- The edit is accepted """ table, status = get_getaway().get_restaurants_table(current_user.rest_id, table_id) if table is None or status != 200: if status == None: flash("Sorry, an error occured. Please, try again.","error") return make_response(render_template('error.html', error = status),500) flash("A table with such id has not been found","error") return make_response(render_template("error.html", error = status), status) if current_user.rest_id != table.restaurant_id: return make_response(render_template('error.html', error="Area reserved for the restaurant operator"), 401) if request.method == 'POST': form = TableAddForm() if form.validate_on_submit(): table, status = get_getaway().edit_restaurants_table(current_user.rest_id, table_id, capacity = int(request.form['capacity'])) if table is None or status != 200: if status == None or status == 500 or status == 404: flash("Sorry, an error occured. Please, try again.","error") return make_response(render_template('error.html', error = status),500) elif status == 400: flash("Bad form", "error") elif status == 409: flash("The table has pending reservations tha conflict with the new capacity, those must be deleted first","error") return make_response(render_template("error.html", error = status), status) return redirect(f"/restaurants/{current_user.rest_id}") flash("Bad form", "error") return make_response(render_template('form.html', form=form), 400) form = TableAddForm(obj=table) return make_response(render_template('form.html', form=form), 200)
def load_user(user_id): user, status = get_getaway().get_user(user_id) if status == 200: usr = User(user['id'], user['is_operator'], user['is_admin'], user['is_health_authority'], user["password"], user['rest_id'], user['is_positive']) usr._authenticated = True return usr else: return None
def _create_restaurant(): """ Show the page for editing the restaurant with GET, the form can be submitted with POST Error status codes: 400 -- The request is not valid, the form is filled out incorrectly or a generic error has occurred 404 -- The restaurant does not exists 409 -- The current user is not an operator or the operator of this restaurant 500 -- Error try again later Success codes: 200 GET -- The form is sent to the user 302 POST -- The modification was accepted """ if current_user.rest_id != None: return make_response(render_template('error.html', error="Restaurant already created"), 401) if request.method == 'POST': form = RestaurantEditForm() if form.validate_on_submit(): json = DotMap() form.populate_obj(json) json = json.toDict() json["closed_days"] = [int(i) for i in json["closed_days"]] restaurant,status = get_getaway().post_restaurants(json) if restaurant is None or status != 201: if status == None or status == 500: flash("Sorry, an error occured. Please, try again. [1,%d]"%status,"error") return make_response(render_template('error.html', error = status),500) elif status == 400: flash('Bad request', "error") return make_response(render_template("error.html", error = status), status) user,status = get_getaway().set_user_restaurant(current_user.id, restaurant.id) if user is None or status != 200: restaurant,status2 = get_getaway().delete_restaurant(restaurant.id) flash("Sorry, an error occured. Please, try again. [2,%d]"%status,"error") return make_response(render_template('error.html', error = status),500) current_user.rest_id = restaurant.id return redirect(f"/restaurants/{current_user.rest_id}") flash("Bad form", "error") return make_response(render_template('edit_restaurant.html', form=form), 400) form = RestaurantEditForm() return render_template('edit_restaurant.html', form=form)
def index(): """ returns The Homepage """ notifications = None restaurants = None import sys print(current_user.is_authenticated, file=sys.stderr) print(current_user, file=sys.stderr) if current_user is not None and hasattr(current_user, 'id'): if current_user.rest_id is not None: restaurant, status_rest = get_getaway().get_restaurant( current_user.rest_id) restaurants = [restaurant] notifications, status_noti = get_getaway().get_notifications( current_user.id) return render_template("index.html", current_user=current_user, restaurants=restaurants, notifications=notifications)
def _users(): """ Returns the list of registered users You must be logged in as admin Error status codes: 401 -- If a user other than admin tries to view it """ users, status = get_getaway().get_users() if users is None or status != 200: return render_template("error.html", error=500), 500 return render_template("users.html", users=users)
def mark_as_read(notification_id): # pragma: no cover """ Mark as read the notification with id equal to notification_id You must logged as customer or operator, the notification must be addressed to the logged in user Error status codes: 404 -- If a user other than customer or operator tries to view it 401 -- If a user try to mark as read a notification addressed to an other user """ notification_with_id, status = get_getaway().get_notification( notification_id) if notification_with_id is None or status == 404: return make_response(render_template('error.html', error='404'), 404) if current_user is not None and hasattr(current_user, 'id'): if current_user.is_admin or current_user.is_health_authority: return make_response(render_template('error.html', error='401'), 401) if notification_with_id['user_id'] != current_user.id: return make_response(render_template('error.html', error='401'), 401) data, status = get_getaway().mark_notif_as_read(notification_id) return redirect("/")
def positives(): """ Allows you to view the list of currently positive users """ positives, status = get_getaway().get_positive_users() if status == 404: flash('There are no positives account', 'warning') return make_response( render_template("positives.html", positives=None, title="Positives"), 302) if positives is None or status != 200: return make_response(render_template("error.html", error=status), status) return render_template("positives.html", positives=positives, title="Positives")
def _reservation_delete(reservation_id): """ It allows the restaurant operator or the user that made the reservation to delete the reservation Error status code: 401 -- The user is not the restaurant operator or the user that made the reservation 403 -- Impossible to delete the reservation 404 -- The reservation does not exist """ booking, status = get_getaway().get_a_booking(id=reservation_id) if status == 404: flash("Booking not found!", "error") return make_response(render_template('error.html', error='404'), 404) if booking is None or status is None or status != 200: flash("Sorry, an error occured. Please, try again.", "error") return make_response(render_template("index.html"), 500) if booking['restaurant_id'] != current_user.rest_id and booking[ 'user_id'] != current_user.id: return make_response(render_template('error.html', error='401'), 401) _, code = get_getaway().delete_booking(id=reservation_id) if code is not None and code == 204: flash("Booking deleted!", "success") elif code is not None and code == 404: flash("Booking not found!", "error") return make_response(render_template('error.html', error='404'), 404) elif code is not None and code == 403: flash("Impossible to delete the requested booking", "warning") else: flash("Sorry, an error occured. Please, try again.", "error") return make_response(render_template("index.html"), 500) return redirect("/")
def _unmark_as_positive_by_id(pos_id): """ Allows you to mark as negative a positive user given his or her id Useful when embedding functionality in a button Error status codes: 404 -- User not found or user not positive """ user, status = get_getaway().unmark_user(pos_id) flash(user, "success") flash(status, "warning") if status == 404 or user is None: flash("User not found", "error") return make_response(render_template('error.html', error='404'), 404) elif status != 200 and status != 201: return make_response(render_template("error.html", error=status), status) return redirect("/positives")
def _reservation(reservation_id): """ It allows the restaurant operator to view the details of a reservation Error status code: 401 -- The user is not the restaurant operator 404 -- The reservation does not exist 500 -- An error occured """ booking, status = get_getaway().get_a_booking(id=reservation_id, with_user=True) if status is None or status != 200 or booking is None or booking == {}: return make_response(render_template('error.html', error=status), status) elif booking['restaurant_id'] != current_user.rest_id: return make_response(render_template('error.html', error='401'), 401) return render_template("reservation.html", reservation=booking)
def list_notifications(): # pragma: no cover """ Shows a page with notifications for the current use You must logged as customer or operator Error status codes: 404 -- If a user other than customer tries to view it """ if current_user is not None and hasattr(current_user, 'id'): if current_user.is_admin or current_user.is_health_authority: return make_response(render_template('error.html', error='404'), 404) notifications, status = get_getaway().get_notifications( current_user.id) return render_template("notifications.html", notifications=notifications, title="Notifications")
def _mark_as_positive_by_id(pos_id): """ Allows you to mark as positive a user given his or her id A positive user can be marked positive again Useful when embedding functionality in a button Notifications are sent to all his contacts (other customers or operators) And to the operators who have received your booking Error status codes: 404 -- User not found """ user, status = get_getaway().mark_user(pos_id) flash(status) if status == 404 or user is None: flash("User not found", "error") return make_response(render_template('error.html', error='404'), 404) elif status != 200 and status != 201: return make_response(render_template("error.html", error=status), status) return redirect("/positives")
def _reservation_edit(reservation_id): """ It allows the restaurant operator or the user that made the reservation to edit the reservation Error status code: 401 -- The user is not the restaurant operator or the user that made the reservation 404 -- The reservation does not exist """ booking, status = get_getaway().get_a_booking(id=reservation_id) if status == 404: flash("Booking not found!", "error") return make_response(render_template('error.html', error='404'), 404) if booking is None or status is None or status != 200: flash("Sorry, an error occured. Please, try again.", "error") return make_response(render_template("index.html"), 500) if booking['restaurant_id'] != current_user.rest_id and booking[ 'user_id'] != current_user.id: return make_response(render_template('error.html', error='401'), 401) if current_user.is_positive: flash("You cannot book as long as you are positive", "error") return make_response(render_template('error.html', error="401"), 401) old_booking_datetime = dateutil.parser.parse(booking["booking_datetime"]) timezone = old_booking_datetime.tzinfo now = datetime.datetime.now(timezone) + datetime.timedelta( hours=1) #timezone aware computation if old_booking_datetime <= now: flash("The reservation has expired", "error") return make_response(render_template('error.html', error='404'), 404) restaurant_id = booking["restaurant_id"] form = BookingForm() obj = {} if request.method == 'POST': if form.validate_on_submit(): number_of_people = request.form["number_of_people"] booking_date = request.form["booking_date"] booking_hr = request.form["booking_hr"] booking_min = request.form["booking_min"] day, month, year = (int(x) for x in booking_date.split('/')) booking_datetime = now.replace(year=year, month=month, day=day, hour=int(booking_hr), minute=int(booking_min), second=0, microsecond=0) if booking_datetime < now: flash("You cannot book before now", "error") return make_response( render_template('form.html', form=form, title="Edit your booking"), 400) booking, code = get_getaway().edit_booking( booking_id=reservation_id, number_of_people=int(number_of_people), booking_datetime=booking_datetime.isoformat()) if booking is None or code is None: flash("Sorry, an error occured. Please, try again.", "error") return make_response( render_template('form.html', form=form, title="View reservations"), 500) elif code == 200: flash("The booking was edited!", "success") return redirect(f"/restaurants/{restaurant_id}") elif code == 400: flash("Bad Request", "warning") return make_response( render_template('form.html', form=form, title="View reservations"), 400) elif status == 404: flash("Booking not found!", "error") return make_response( render_template('error.html', error='404'), 404) elif code == 409: flash(booking["detail"], "warning") return make_response( render_template('form.html', form=form, title="View reservations"), 400) else: flash("Sorry, an error occured. Please, try again.", "error") return make_response( render_template('form.html', form=form, title="View reservations"), 500) elif request.method == "GET": obj["number_of_people"] = booking["number_of_people"] obj["booking_date"] = old_booking_datetime.strftime('%d/%m/%Y') obj["booking_hr"] = int(old_booking_datetime.strftime('%H')) obj["booking_min"] = int((int(old_booking_datetime.strftime('%M')) / 15)) * 15 #round to a multiple of 15 return render_template('edit-form.html', form=form, edited=obj, title="Edit your booking")
def delete_user(): """ Delete the current user profile and log out The user must confirm the request by entering email and password. The request is approved only if the user is not positive. If the user is an operator, the restaurant is also deleted. In that case, a notification is sent to all users who had active bookings, and bookings are canceled. The functionality is not active for the health authority or for the admin. Error status codes: 400 -- The request is not valid, the form is filled out incorrectly or a generic error has occurred 401 -- The current user is not a customer or operator Success codes: 200 -- The form is sent 302 -- The elimination was carried out """ if current_user.is_admin or current_user.is_health_authority: return make_response(render_template('error.html', error='401'), 401) form = LoginForm() if request.method == 'POST': if form.validate_on_submit(): users, status_code = get_getaway().get_users( email=form.data['email']) if status_code != 200: flash('Wrong password or mail', 'success') return make_response( render_template('error.html', title="Unregister"), 400) if users is not None: email, password = form.data['email'], form.data['password'] user = users[0].toDict() else: flash('Wrong email', 'error') return make_response( render_template('delete_profile.html', form=form, title="Unregister"), 400) checked = check_password_hash(user['password'], password) if checked: usr, status = get_getaway().delete_user(user['id']) if status == 204: flash('Account deleted', 'success') logout_user() return redirect("/") if status == 400: flash(usr.detail, 'warning') return make_response( render_template('error.html', title="Unregister"), 400) if status == 500: flash('Please try again', 'error') return make_response( render_template('error.html', title="Unregister"), 500) else: flash('Wrong password', 'error') return make_response( render_template('delete_profile.html', form=form, title="Unregister"), 400) else: flash('Bad form', 'error') return make_response( render_template('delete_profile.html', form=form, title="Unregister"), 400) return render_template('delete_profile.html', form=form, title="Unregister")
def _book(restaurant_id): """ It allows you to book in a specific restaurant Error status code: 400 -- The form is filled in incorrectly or it is not possible to make a reservation with that data 401 -- The user cannot make the reservation (it is positive, he is not a customer, he is not logged in) 404 -- The restaurant does not exist 409 -- Impossible to make the reservation 500 -- An error occured """ form = BookingForm() rest, code = get_getaway().get_restaurant(restaurant_id) if code is not None and code == 404: flash("Restaurant not found!", "error") return make_response(render_template('error.html', error='404'), 404) elif code is None or code != 200 or rest is None or rest == {}: flash("Sorry, an error occured. Please, try again.", "error") return make_response( render_template('form.html', form=form, title="View reservations"), 500) if current_user.is_admin or current_user.is_operator or current_user.is_health_authority: flash("Please log as customer to book a table", "error") return make_response(render_template('error.html', error="401"), 401) if current_user.is_positive: flash("You cannot book as long as you are positive", "error") return make_response(render_template('error.html', error="401"), 401) if request.method == 'POST': if form.validate_on_submit(): number_of_people = request.form["number_of_people"] booking_date = request.form["booking_date"] booking_hr = request.form["booking_hr"] booking_min = request.form["booking_min"] now = datetime.datetime.now() + datetime.timedelta( hours=1) # timezone aware day, month, year = (int(x) for x in booking_date.split('/')) booking_datetime = now.replace(year=year, month=month, day=day, hour=int(booking_hr), minute=int(booking_min), second=0, microsecond=0) if booking_datetime < now: flash("You cannot book before now", "error") return make_response( render_template('form.html', form=form, title="Book a table!"), 400) booking, code = get_getaway().new_booking( user_id=current_user.id, rest_id=restaurant_id, number_of_people=int(number_of_people), booking_datetime=booking_datetime.isoformat()) if booking is None or code is None: flash("Sorry, an error occured. Please, try again.", "error") return make_response( render_template('form.html', form=form, title="View reservations"), 500) elif code == 201: flash("The booking was confirmed", "success") return redirect(f"/restaurants/{restaurant_id}") elif code == 400: flash("Bad Request", "warning") return make_response( render_template('form.html', form=form, title="View reservations"), 400) elif code == 404: flash("Booking not found!", "error") return make_response( render_template('error.html', error='404'), 404) elif code == 409: flash(booking["detail"], "warning") return make_response( render_template('form.html', form=form, title="View reservations"), 400) else: flash("Sorry, an error occured. Please, try again.", "error") return make_response( render_template('form.html', form=form, title="View reservations"), 500) return render_template('form.html', form=form, title="Book a table!")
def _booking_list(restaurant_id): """ It allows the current operator to view all bookings in a given period of time Error status code: 400 -- The form is filled in incorrectly 401 -- The user is not the restaurant operator 404 -- The restaurant does not exist 500 -- An error occured """ form = BookingList() rest, code = get_getaway().get_restaurant(restaurant_id) if code is not None and code == 404: flash("Restaurant not found!", "error") return make_response(render_template('error.html', error='404'), 404) elif code is None or code != 200 or rest is None or rest == {}: flash("Sorry, an error occured. Please, try again.", "error") return make_response( render_template('form.html', form=form, title="View reservations"), 500) if current_user.rest_id != restaurant_id: return make_response( render_template('error.html', error="Area reserved for the restaurant operator"), 401) if request.method == 'POST': if form.validate_on_submit(): now = datetime.datetime.now() from_date = request.form["from_date"] from_hr = request.form["from_hr"] from_min = request.form["from_min"] from_day, from_month, from_year = (int(x) for x in from_date.split('/')) from_datetime = now.replace(year=from_year, month=from_month, day=from_day, hour=int(from_hr), minute=int(from_min), second=0, microsecond=0) to_date = request.form["to_date"] to_hr = request.form["to_hr"] to_min = request.form["to_min"] to_day, to_month, to_year = (int(x) for x in to_date.split('/')) to_datetime = now.replace(year=to_year, month=to_month, day=to_day, hour=int(to_hr), minute=int(to_min), second=0, microsecond=0) if from_datetime >= to_datetime: flash("Invalid time interval", "error") return make_response( render_template('form.html', form=form, title="View reservations"), 400) qry, status_code = get_getaway().get_bookings( rest=restaurant_id, begin=from_datetime.isoformat(), end=to_datetime.isoformat(), with_user=True) if status_code is None or status_code != 200: flash("Sorry, an error occured. Please, try again.", "error") return make_response( render_template('form.html', form=form, title="View reservations"), 500) elif qry is None or qry == []: qry = [] flash("No reservations were found", "warning") return make_response( render_template("reservations.html", reservations=qry, title="View reservations"), 200) return make_response( render_template('form.html', form=form, title="View reservations"), 200)
def restaurant_reservations_overview(restaurant_id, year, month, day): """ Renders an overview of a day's reservations If GET parameters from_h, from_m, to_h, to_m are provided, also renders the list of reservations in the particular time range. Error status codes: 400 -- The requested time range is invalid 401 -- The request has been sent by an unauthenticated user, or the user is not the owner of the restaurant 404 -- The restaurant or the requested day do not exist """ try: date_start = datetime(year, month, day) prev_day = date_start - timedelta(days=1) date_end = date_start + timedelta(days=1) except: return make_response(render_template('error.html', error='404'), 404) if current_user.rest_id != restaurant_id: return make_response(render_template('error.html', error="Area reserved for the restaurant operator"), 401) restaurant, status = get_getaway().get_restaurant(restaurant_id) if restaurant is None or status != 200: return make_response(render_template('error.html', error=status), status) reservations, status = get_getaway().get_bookings( None, # User restaurant_id, # Restaurant None, # Table date_start.isoformat(), # Begin date_end.isoformat(), # End None, # Entrance begin None, # Entrance end False) # With user if reservations is None or status != 200: return make_response(render_template('error.html', error=status), status) lunch_reservations = [] dinner_reservations = [] if restaurant.first_opening_hour is not None: slot_begin = datetime(year, month, day, restaurant.first_opening_hour) slot_end = datetime(year, month, day, restaurant.first_closing_hour) for reserv in reservations: booking_datetime = parser.parse(reserv.booking_datetime, ignoretz = True) if booking_datetime >= slot_begin and booking_datetime < slot_end: lunch_reservations.append(reserv) if restaurant.second_opening_hour is not None: slot_begin = datetime(year, month, day, restaurant.second_opening_hour) slot_end = datetime(year, month, day, restaurant.second_closing_hour) for reserv in reservations: booking_datetime = parser.parse(reserv.booking_datetime, ignoretz = True) if booking_datetime >= slot_begin and booking_datetime < slot_end: dinner_reservations.append(reserv) slot_begin = None slot_end = None slot_reservations = None people_total = None if request.args.get('from_h') is not None and \ request.args.get('from_m') is not None and \ request.args.get('to_h') is not None and \ request.args.get('to_m') is not None: try: from_h = int(request.args.get('from_h')) from_m = int(request.args.get('from_m')) to_h = int(request.args.get('to_h')) to_m = int(request.args.get('to_m')) slot_begin = datetime(year, month, day, from_h, from_m) slot_end = datetime(year, month, day, to_h, to_m) except: return make_response(render_template('error.html', error='Invalid slot range'), 400) if slot_end < slot_begin: return make_response(render_template('error.html', error='Slot start must be before slot end'), 400) slot_reservations, status = get_getaway().get_bookings( None, # User restaurant_id, # Restaurant None, # Table slot_begin.isoformat(), # Begin slot_end.isoformat(), # End None, # Entrance begin None, # Entrance end False) # With user if slot_reservations is None or status != 200: return make_response(render_template('error.html', error=status), status) people_total = 0 for reserv in slot_reservations: people_total = people_total + reserv.number_of_people return render_template('overview.html', restaurant = restaurant, lunch=lunch_reservations, dinner=dinner_reservations, current = date_start, prev = prev_day, next = date_end, today = datetime.today(), slot_begin = slot_begin, slot_end = slot_end, slot_reservations = slot_reservations, slot_people = people_total)
def search_res(): """ It allows you to filter the search for restaurants based on the desired characteristics Error status code: 400 -- The form is filled out incorrectly 404 -- No restaurant with those characteristics was found 500 -- Error try again later Success codes: 200 """ form = SearchRestaurantForm() if request.method == 'POST': if form.validate_on_submit(): opening_time = request.form["opening_time"] if opening_time.lower() == "not specified": opening_time = None open_day = request.form["open_day"] if open_day == "0": open_day = None name = request.form["name"] if name == "": name = None cuisine_type = request.form["cuisine_type"] if cuisine_type == "": cuisine_type = None menu = request.form["menu"] if menu == "": manu = None allrestaurants, status = get_getaway().get_restaurants(name, opening_time, open_day, cuisine_type, menu) if allrestaurants is None or status != 200: if status == None: flash("Sorry, an error occured. Please, try again.","error") return make_response(render_template('error.html', error = status),500) flash("Bad form", "error") return make_response(render_template("error.html", error = status), status) markers_to_add = [] matches = [] for restaurant in allrestaurants: rest = { 'icon': 'http://maps.google.com/mapfiles/ms/icons/blue-dot.png', 'lat': restaurant.lat, 'lng': restaurant.lon, 'infobox': restaurant.name } markers_to_add.append(rest) matches.append(restaurant) if matches == []: flash("No restaurant found", "error") return make_response(render_template('form.html', form=form, title="Find a restaurant!"), 404) sndmap = Map( # make the maps of restaurants identifier="sndmap", lat=43.72, lng=10.40, markers=markers_to_add, zoom_control=True, style="height:600px;width:1000px;margin:0;", zoom=15 ) return render_template("restaurants.html", sndmap=sndmap, restaurants=matches, base_url="/restaurants") flash("Bad form", "error") return make_response(render_template('form.html', form=form, title="Find a restaurant!"), 400) return make_response(render_template('form.html', form=form, title="Find a restaurant!"), 200)