def newMenuItem(restaurant_id): if request.method == 'POST': if database_query.addNewMenuItem(mySession, restaurant_id, request.form["name"], request.form["price"], request.form["description"]): return redirect(url_for("restaurantMenu", restaurant_id = restaurant_id)) else: return render_template("addmenuitem.html", restaurant = database_query.getRestaurant(mySession, restaurant_id)) return "Menu item cannot be found"
def newMenuItem(restaurant_id): output = "" r = database_query.getRestaurant(mySession, restaurant_id) if r: if request.method == 'GET': output += "<html><body>" output += "<h2>Add new menu item for %s</h2>" % r.name output += "<form method='POST' enctype='multipart/form-data' action='/restaurants/%s/new'><h3>Enter new menu item</h3>" % str( restaurant_id) output += "<input name='name' type='text' placeholder='Name' ><br/><input name='price' type='text' placeholder='Price'><br/><input name='desc' type='text' placeholder='Description' ><br/>" output += "<input type='submit' value='Add'> </form>" output += "</body></html>" return output elif request.method == 'POST': if database_query.addNewMenuItem(mySession, restaurant_id, request.form["name"], request.form["price"], request.form["desc"]): flash("new menu item created!") return redirect( url_for("restaurantMenu", restaurant_id=restaurant_id)) output = "Menu item cannot be added." return output
def restaurantMenu(restaurant_id): return render_template("menu.html", restaurant = database_query.getRestaurant(mySession, restaurant_id), items = database_query.getMenuItems(mySession, restaurant_id))
def deleteRestaurant(restaurant_id): if request.method == 'POST': if database_query.deleteRestaurant(mySession, restaurant_id): return redirect(url_for("getRestaurants")) else: return render_template("deleteRestaurant.html", restaurant = database_query.getRestaurant(mySession, restaurant_id) )
def editRestaurant(restaurant_id): if request.method == 'POST': if database_query.editName(mySession, restaurant_id, request.form["name"]): return redirect(url_for("getRestaurants")) else: return render_template("editrestaurant.html", restaurant = database_query.getRestaurant(mySession, restaurant_id) )
def do_GET(self): try: if self.path.endswith("/hello"): self.send_response(200) self.send_header('Content-type', 'text/html') self.end_headers() output = "" output += "<html><body>" output += "<h1>Hello!</h1>" output += "<form method='POST' enctype='multipart/form-data' action='/hello'><h2>What would you like me to say?</h2><input name='message' type='text' ><input type='submit' value='Submit'> </form>" output += "</body></html>" self.wfile.write(output) print(output) return elif self.path.endswith("/restaurants"): self.send_response(200) self.send_header('Content-type', 'text/html') self.end_headers() print("Try to query db.") output = "" output += "<html><body>" output += "<h1>List of restaurants</h1>" #output += "<ul style='list-style-type:none'>" restaurants = database_query.getRestaurants(mySession) for r in restaurants: print("Next one found: " + str(r.name)) output += "<h3>%s</h3>" % str(r.name) output += "<a href='/restaurant/{0}/edit'><b>Edit</b></a>".format( r.id) output += "   " params = {'id': r.id, 'name': r.name.strip()} params = urllib.urlencode(params) print("Params:" + str(params)) output += "<a href='/restaurant/delete?{0}'><b>Delete</b></a>".format( str(params)) #output += "</ul>" output += "</body></html>" print(output) self.wfile.write(output) return 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>Add new restaurant</h1>" output += "<form method='POST' enctype='multipart/form-data' action='/restaurants/new'><h3>What is the name of the new restaurant?</h3><input name='name' type='text' ><input type='submit' value='Add'> </form>" output += "</body></html>" print(output) self.wfile.write(output) elif self.path.endswith("/edit"): self.send_response(200) self.send_header('Content-type', 'text/html') self.end_headers() output = "" output += "<html><body>" findId = re.search("\d+(?=\/edit)", self.path) if findId: id = int(findId.group()) r = database_query.getRestaurant(mySession, id) if r: output += "<html><body>" output += "<form method='POST' enctype='multipart/form-data' action='/restaurants/{0}/edit'><h3>Rename the restaurant {1}:</h3><input name='name' type='text' placeholder='{1}'><input type='submit' value='Rename'> </form>".format( r.id, r.name) output += "</body></html>" if len(output) == 0: output += "<html><body>" output += "No restaurant found with this id" output += "</body></html>" print(output) self.wfile.write(output) elif re.search("\/delete(?=\?)", self.path): #print("URL:" + self.url) print("Path:" + self.path) self.send_response(200) self.send_header('Content-type', 'text/html') self.end_headers() o = urlparse.urlparse(self.path) print("Query:" + o.query) params = urlparse.parse_qs(o.query) id = int(params.get("id", ["-1"])[0]) name = params.get("name", "")[0] output = "" if id >= 0: output += "<html><body>" output += "<form method='POST' enctype='multipart/form-data' action='/restaurants/{0}/delete'><h3>Are you sure you want to delete {1}?</h3><input type='submit' value='Delete'> </form>".format( id, name) output += "</body></html>" if len(output) == 0: output += "<html><body>" output += "No restaurant found with this id" output += "</body></html>" print(output) self.wfile.write(output) except: self.send_error(404, 'File Not Found: %s' % self.path)