def fetch_menu(restaurant_id): restaurant = db_utils.fetch_restaurant_by_id(restaurant_id) items = db_utils.fetch_menu_items_by_id(restaurant_id) return render_template('menu.html', items=items, restaurant_id=restaurant_id, restaurant=restaurant)
def delete_restaurant(restaurant_id): if 'email' not in session: return redirect('/login') if request.method == 'POST': name = db_utils.delete_restaurant(restaurant_id) flash('{} Successfully Deleted'.format(name)) return redirect(url_for('fetch_restaurants')) else: restaurant = db_utils.fetch_restaurant_by_id(restaurant_id) return render_template('deleterestaurant.html', restaurant=restaurant)
def edit_restaurant(restaurant_id): if 'email' not in session: return redirect('/login') if request.method == 'POST': if request.form['name']: name = request.form['name'] db_utils.edit_restaurant(restaurant_id, name) flash('Restaurant Successfully Edited {}'.format(name)) return redirect(url_for('fetch_restaurants')) else: restaurant = db_utils.fetch_restaurant_by_id(restaurant_id) return render_template('editrestaurant.html', restaurant=restaurant)
def do_GET(self): print("method do_GET") try: if self.path.endswith("/restaurants"): self.send_response(200) self.send_header('Content-type', 'text/html') self.end_headers() restaurants = db_utils.fetch_all_restaurants() output = "" output += "<html><body>" for restaurant in restaurants: output += restaurant.name output += "</br>" # Objective 2 -- Add Edit and Delete Links output += "<a href ='restaurants/{}/edit'>Edit </a> ".format( restaurant.id) output += "</br>" output += "<a href ='restaurants/{}/delete'> Delete </a>".format( restaurant.id) output += "</br></br></br>" output += "</body></html>" self.wfile.write(bytes(output, "utf-8")) elif self.path.endswith("/restaurants/new"): self.send_response(200) self.send_header('Content-type', 'text/html') self.end_headers() output = "" output += "<html><body>" output += "<h1>Create a New Restaurant</h1>" output += self.get_new_restaurant_form_body() output += "</body></html>" self.wfile.write(bytes(output, "utf-8")) elif self.path.endswith("/edit"): restaurant_id = self.path.split("/")[2] restaurant = db_utils.fetch_restaurant_by_id(restaurant_id) if restaurant: self.send_response(200) self.send_header('Content-type', 'text/html') self.end_headers() output = "<html><body>" output += "<h1>" output += restaurant.name output += "</h1>" output += self.get_edit_restaurant_form_body(restaurant) output += "</body></html>" self.wfile.write(bytes(output, "utf-8")) elif self.path.endswith("/delete"): restaurant_id = self.path.split("/")[2] restaurant = db_utils.fetch_restaurant_by_id(restaurant_id) if restaurant: self.send_response(200) self.send_header('Content-type', 'text/html') self.end_headers() output = "" output += "<html><body>" output += "<h1>Are you sure you want to delete {}?".format( restaurant.name) output += "</h1>" output += self.get_delete_restaurant_form_body(restaurant) output += "</body></html>" self.wfile.write(bytes(output, "utf-8")) else: self.send_error(404, 'File Not Found: {}'.format(self.path)) except IOError: self.send_error(404, 'File Not Found: {}'.format(self.path))
def fetch_menus_json(restaurant_id): restaurant = db_utils.fetch_restaurant_by_id(restaurant_id) items = db_utils.fetch_menu_items(restaurant) return jsonify(MenuItems=[i.serialize for i in items])