def menuItems(restaurant_id): #get restaurant object as well restaurant = RestaurantQuery.get_restaurant(restaurant_id) #get the menu items from the restaurant items = RestaurantQuery.get_menu_items(restaurant_id) if len(items) == 0: flash("No menu items currently") return render_template('menu.html', restaurant=restaurant, items=items)
def deleteRestaurant(restaurant_id): #item to delete deletedRestaurant = RestaurantQuery.get_restaurant(restaurant_id) #if posted too, process the form #else just display the page and it's form if request.method == 'POST': RestaurantQuery.delete_restaurant(deletedRestaurant.id) #flash message flash("Successfully deleted "+deletedRestaurant.name) return redirect(url_for('restaurants')) else: return render_template('deleteRestaurant.html', restaurant_id=restaurant_id, item=deletedRestaurant)
def editRestaurant(restaurant_id): #item to edit editedRestaurant = RestaurantQuery.get_restaurant(restaurant_id) #if posted too, process the form #else just display the page and it's form if request.method == 'POST': if request.form['name']: editedRestaurant.name = request.form['name'] RestaurantQuery.edit_restaurant(editedRestaurant.name, editedRestaurant.id) #flash message flash("Updated "+editedRestaurant.name) return redirect(url_for('restaurants')) else: return render_template('editRestaurant.html', restaurant_id=restaurant_id, item=editedRestaurant)
def do_GET(self): #check the url being requested #and if it's a url we're expecting #return a a response if self.path.endswith("/restaurants"): #successful response code self.send_response(200) #headers to send back self.send_header('Content-type', 'text/html') self.end_headers() #begin our response message for the user message = "" message += "<html><body>" #link to make new restaurants message += "<a href='/restaurants/new'>Click here to add a new restaurant</a>" #create an instance of our query #class and get back the requested data #from our database. restaurants = RestaurantQuery.get_items(Restaurant) #append our query results to the message message += restaurants #close our message message += "</body></html>" #writeout the server response self.wfile.write(message) #print message print message return #get our /restaurant/new page if self.path.endswith("/restaurants/new"): #successful response code self.send_response(200) #headers to send back self.send_header('Content-type', 'text/html') self.end_headers() #begin our response message for the user message = "" message += "<html><body>" #add our form message += "<form method='POST', enctype='multipart/form-data' action='/restaurants'><h2>Add a new restaurant</h2>" message += "<input type='text' name='restaurant'><input type='hidden' name='operation' value='add'><input type='submit' value='submit'></form>" message += "</body></html>" #close our message message += "</body></html>" #writeout the server response self.wfile.write(message) #print message print message return #since the url is dependent on the id we #set up a small regex for the url to check. #if it is a match present the page if re.compile(r'/restaurants/([0-9]+)/edit').search(self.path): #we have a matching url so no another #regex is perfomed to extract the ID of the restaurant rID = re.search(r'[0-9]+', self.path).group() #successful response code self.send_response(200) #headers to send back self.send_header('Content-type', 'text/html') self.end_headers() #begin our response message for the user message = "" message += "<html><body>" #add our form message += "<form method='POST', enctype='multipart/form-data' action='/restaurants'><h2>Edit a restaurant?</h2>" message += "<input type='text' name='restaurant'><input type='hidden' name='operation' value='edit'>" message += "<input type='hidden' name='rID' value='"+rID+"'><input type='submit' value='submit'></form>" message += "</body></html>" #close our message message += "</body></html>" #writeout the server response self.wfile.write(message) #print message print message return #since the url is dependent on the id we #set up a small regex for the url to check. #if it is a match present the page if re.compile(r'/restaurants/([0-9]+)/delete').search(self.path): #we have a matching url so no another #regex is perfomed to extract the ID of the restaurant rID = re.search(r'[0-9]+', self.path).group() #successful response code self.send_response(200) #headers to send back self.send_header('Content-type', 'text/html') self.end_headers() #begin our response message for the user message = "" message += "<html><body>" #get the name of the restaurant #being operated on rName = RestaurantQuery.get_restaurant(int(rID), 'name') #add our form message += "<form method='POST', enctype='multipart/form-data' action='/restaurants'><h2>Are you sure you want to delete "+rName+"?</h2>" message += "<input type='hidden' name='operation' value='delete'>" message += "<input type='hidden' name='rID' value='"+rID+"'><input type='submit' value='delete'></form>" message += "</body></html>" #close our message message += "</body></html>" #writeout the server response self.wfile.write(message) #print message print message return else: self.send_error(404, 'File Not Found: %s' % self.path)