def newRestaurant(): if request.method == 'POST': r = Restaurant() r.name = request.form['name'] session.add(r) session.commit() flash('new restaurant %s created!' % r) return redirect(url_for('restaurantList')) else: return render_template('newRestaurant.html')
def newRestaurant(): if request.method == 'POST': newRestaurant = Restaurant() newRestaurant.name = request.form['addRestaurant'] session.add(newRestaurant) session.commit() flash('New restaurant created!') return redirect(url_for('showRestaurants')) else: return render_template('newrestaurant.html')
def newRestaurant(): if request.method == "POST": restaurant = Restaurant(name = request.form["restaurant-name"], thumbnail_url = request.form["restaurant-image-url"]) if request.form["restaurant-description"]: restaurant.description = request.form["restaurant-description"] session.add(restaurant) session.commit() flash("New Restaurant Added") return redirect(url_for('index')) else: return render_template("new-restaurants.html", title="Add A New Restaurant")
def do_POST(self): try: self.send_response(301) self.end_headers() parts = self.path.split('/')[1:] if parts[0] == 'restaurants': if len(parts) == 2: if parts[1] == 'create': ctype, pdict = cgi.parse_header(self.headers.getheader('content-type')) if ctype == 'multipart/form-data': fields = cgi.parse_multipart(self.rfile, pdict) new_name = fields.get('name')[0] rest = Restaurant() rest.name = new_name session.add(rest) session.commit() output = "<html><body><h1>Added {0}.</h1>".format(new_name) output += "<a href='/restaurants'>Back to restaurant list</a>" output += "</body></html>" self.wfile.write(output) if len(parts) == 3: if parts[2] == 'edit': rest_id = parts[1] rest = session.query(Restaurant).filter_by(id=rest_id).one() ctype, pdict = cgi.parse_header(self.headers.getheader('content-type')) if ctype == 'multipart/form-data': fields = cgi.parse_multipart(self.rfile, pdict) new_name = fields.get('name')[0] old_name = rest.name rest.name = new_name session.add(rest) session.commit() output = '' output += "<html><body>" output += "<h2> The name was change from: %s</h2>" % old_name output += "<h1> to: %s </h1>" % new_name output += "<a href='/restaurants'>Back to restaurant list</a>" output += "</body></html>" self.wfile.write(output) if parts[2] == 'delete': rest_id = parts[1] rest = session.query(Restaurant).filter_by(id=rest_id).one() session.delete(rest) session.commit() output = '' output += "<html><body>" output += "<h2> The record has been deleted.</h2>" output += "<a href='/restaurants'>Back to restaurant list</a>" output += "</body></html>" self.wfile.write(output) except: pass
def do_POST(self): try: if self.path.endswith("/restaurants/new"): ctype, pdict = cgi.parse_header(self.headers.getheader('content-type')) if ctype == 'multipart/form-data': fields = cgi.parse_multipart(self.rfile, pdict) namecontent = fields.get('name')[0] restaurant = Restaurant(name=namecontent) session.add(restaurant) session.commit() self.send_response(301) self.send_header('Content-type', 'text/html') self.send_header('Location', '/restaurants') self.end_headers() if self.path.endswith("/edit"): ctype, pdict = cgi.parse_header(self.headers.getheader('content-type')) if ctype == 'multipart/form-data': fields = cgi.parse_multipart(self.rfile, pdict) new_name = fields.get('rename')[0] pathId = self.path.split('/')[2] session.query(Restaurant).filter_by(id=pathId).one() restaurant.name = new_name session.add(restaurant) session.commit() self.send_response(301) self.send_header('Content-type', 'text/html') self.send_header('Location', '/restaurants') self.end_headers() if self.path.endswith("/delete"): pathId = self.path.split('/')[2] restaurant = session.query(Restaurant).filter_by(id=pathId).one() session.delete(restaurant) session.commit() self.send_response(301) self.send_header('Content-type', 'text/html') self.send_header('Location', '/restaurants') self.end_headers() except: pass
def createRestaurant(): context = {"user_id": login_session["user_id"]} if request.method == "POST": new_restaurant = Restaurant(name=request.form["restaurant-name"]) new_restaurant.user_id = login_session["user_id"] try: session.add(new_restaurant) session.commit() message = "New Restaurant %s Successfully Created" % new_restaurant.name flash(message, "info") except: message = "There is an error adding this item" flash(message, "error") return redirect(url_for("newMenuItem", restaurant_id=new_restaurant.id)) else: return render_template("newRestaurant.html", context=context)
# database session object. Any change made against the objects in the # session won't be persisted into the database until you call # session.commit(). If you're not happy about the changes, you can # revert all of them back to the last commit by calling # session.rollback() session = DBSession() # Create dummy user User1 = User(name="Robo Barista", email="*****@*****.**", picture='https://pbs.twimg.com/profile_images/2671170543/18debd694829ed78203a5a36dd364160_400x400.png') session.add(User1) session.commit() # Menu for UrbanBurger restaurant1 = Restaurant(user_id=1, name="Urban Burger", picture="/static/img/1.jpg") session.add(restaurant1) session.commit() menuItem2 = MenuItem(user_id=1, name="Veggie Burger", description="Juicy grilled veggie patty with tomato mayo and lettuce", price="$7.50", course="Entree", restaurant=restaurant1) session.add(menuItem2) session.commit() menuItem1 = MenuItem(user_id=1, name="French Fries", description="with garlic and parmesan", price="$2.99", course="Appetizer", restaurant=restaurant1) session.add(menuItem1)
def do_POST(self): if self.path.endswith("/restaurants/add"): ctype, pdict = cgi.parse_header(self.headers['content-type']) pdict['boundary'] = bytes(pdict['boundary'], "UTF-8") if ctype == 'multipart/form-data': fields = cgi.parse_multipart(self.rfile, pdict) restaurant_name = fields.get('restaurant') newRestaurant = Restaurant(name=restaurant_name[0]) session.add(newRestaurant) session.commit() self.send_response(301) self.send_header('Content-type', 'text/html') self.send_header( 'Location', '/restaurants' ) # HTTP Header information needed for redirect self.end_headers() # Post if self.path.endswith("/edit"): ctype, pdict = cgi.parse_header( self.headers.getheader('content-type')) if ctype == 'multipart/form-data': fields = cgi.parse_multipart(self.rfile, pdict) restaurant_edit = fields.get('restaurantEdit') restaurantIDPath = self.path.split("/")[2] myRestaurantQuery = session.query(Restaurant).filter_by( id=restaurantIDPath).one() if myRestaurantQuery != []: myRestaurantQuery.name = restaurant_edit[0] session.add(myRestaurantQuery) session.commit() self.send_response(301) self.send_header('Content-type', 'text/html') self.send_header( 'Location', '/restaurants' ) # HTTP Header information needed for redirect self.end_headers() if self.path.endswith("/delete"): ctype, pdict = cgi.parse_header( self.headers.getheader('content-type')) if ctype == 'multipart/form-data': fields = cgi.parse_multipart(self.rfile, pdict) # restaurant_edit = fields.get('restaurantEdit') restaurantIDPath = self.path.split("/")[2] myRestaurantQuery = session.query(Restaurant).filter_by( id=restaurantIDPath).one() if myRestaurantQuery != []: # myRestaurantQuery.name = restaurant_edit[0] session.delete(myRestaurantQuery) session.commit() self.send_response(301) self.send_header('Content-type', 'text/html') self.send_header( 'Location', '/restaurants' ) # HTTP Header information needed for redirect self.end_headers()
# A DBSession() instance establishes all conversations with the database # and represents a "staging zone" for all the objects loaded into the # database session object. Any change made against the objects in the # session won't be persisted into the database until you call # session.commit(). If you're not happy about the changes, you can # revert all of them back to the last commit by calling # session.rollback() session = DBSession() # Create dummy user User1 = User(name="admin", email="*****@*****.**") session.add(User1) session.commit() # Menu for Mohannad's Kitchen restaurant1 = Restaurant(name="Mohannad's Kitchen", user_id="1") session.add(restaurant1) session.commit() menuItem1 = MenuItem(name="Dolma", description="Vegetables stiffed with rice" "different vegetables.", price="$9.99", restaurant=restaurant1, user_id=1) session.add(menuItem1) session.commit() menuItem2 = MenuItem(name="Sambar Idly",
# session won't be persisted into the database until you call # session.commit(). If you're not happy about the changes, you can # revert all of them back to the last commit by calling # session.rollback() session = DBSession() User1 = User( name="Vinay", email="*****@*****.**", picture= 'https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcSqHx9HEKprfUBgpfsgDNjaeCrXPGqwam_oAsREaXgYnt71HaDp' ) session.add(User1) session.commit() #Menu for UrbanBurger restaurant1 = Restaurant(user_id=1, name="Achuth-Breakfast Center") session.add(restaurant1) session.commit() menuItem1 = MenuItem(user_id=1, name="Pulihora", description="very good at it", price="$2.99", course="Breakfast", restaurant=restaurant1, photo='https://bit.ly/2sRPzE6') session.add(menuItem1) session.commit()
from sqlalchemy import create_engine from sqlalchemy.orm import sessionmaker from database_setup import Base, Restaurant, MenuItem engine = create_engine('sqlite:///restaurantmenu.db') Base.metadata.bind = engine DBSession = sessionmaker(bind=engine) session = DBSession() myFirstRestaurant = Restaurant(name='Pizza Palace') session.add(myFirstRestaurant) session.commit() cheesepizza = MenuItem(name = 'Cheese Pizza', description = 'Made with all natural ingredients and fresh mozzarella', course='Entree', price='$8.99', restaurant=myFirstRestaurant) session.add(cheesepizza) session.commit() session.close()
def do_POST(self): try: if self.path.endswith("/edit"): ctype, pdict = cgi.parse_header( self.headers.getheader('content-type')) if ctype == 'multipart/form-data': fields = cgi.parse_multipart(self.rfile, pdict) messagecontent = fields.get('newRestaurantName') restaurantIDPath = self.path.split("/")[2] myRestaurantQuery = session.query(Restaurant).filter_by( id=restaurantIDPath).one() if myRestaurantQuery != []: myRestaurantQuery.name = messagecontent[0] session.add(myRestaurantQuery) session.commit() self.send_response(301) self.send_header('Content-type', 'text/html') self.send_header('Location', '/restaurants') self.end_headers() if self.path.endswith("/delete"): ctype, pdict = cgi.parse_header( self.headers.getheader('content-type')) if ctype == 'multipart/form-data': fields = cgi.parse_multipart(self.rfile, pdict) messagecontent = fields.get('RestaurantName') restaurantIDPath = self.path.split("/")[2] myRestaurantQuery = session.query(Restaurant).filter_by( id=restaurantIDPath).one() if myRestaurantQuery != []: r = session.query(Restaurant).filter_by( id=restaurantIDPath).one() print(r.name) session.delete(r) session.commit() self.send_response(301) self.send_header('Content-type', 'text/html') self.send_header('Location', '/restaurants') self.end_headers() if self.path.endswith("/restaurants/new"): ctype, pdict = cgi.parse_header( self.headers.getheader('content-type')) if ctype == 'multipart/form-data': fields = cgi.parse_multipart(self.rfile, pdict) messagecontent = fields.get('newRestaurantName') # Create new Restaurant Object newRestaurant = Restaurant(name=messagecontent[0]) session.add(newRestaurant) session.commit() self.send_response(301) self.send_header('Content-type', 'text/html') self.send_header('Location', '/restaurants') self.end_headers() except: pass
def do_POST(self): try: if self.path.endswith('/edit'): ctype, pdict = cgi.parse_header( self.headers.getheader('content-type')) if ctype == 'multipart/form-data': fields = cgi.parse_multipart(self.rfile, pdict) messageData = fields.get('message') item_id = self.path.split('/')[2] print messageData myquery = session.query(Restaurant).filter_by(id=item_id).one() myquery.name = messageData[0] session.add(myquery) session.commit() self.send_response(301) self.send_header('content-type', 'text/html') self.send_header('location', '/restaurants') self.end_headers() if self.path.endswith('/delete'): ctype, pdict = cgi.parse_header( self.headers.getheader('content-type')) if ctype == 'multipart/form-data': fields = cgi.parse_multipart(self.rfile, pdict) messageData = fields.get('message') item_id = self.path.split('/')[2] print messageData myquery = session.query(Restaurant).filter_by(id=item_id).one() session.delete(myquery) session.commit() self.send_response(301) self.send_header('content-type', 'text/html') self.send_header('location', '/restaurants') self.end_headers() if self.path.endswith('/restaurants/new'): ctype, pdict = cgi.parse_header( self.headers.getheader('content-type')) if ctype == 'multipart/form-data': fields = cgi.parse_multipart(self.rfile, pdict) messageData = fields.get('message') print messageData restaurant = Restaurant(name=messageData[0]) session.add(restaurant) session.commit() self.send_response(301) self.send_header('content-type', 'text/html') self.send_header('location', '/restaurants') self.end_headers() except: self.send_error(404, 'FIle not found %s!' % self.path)
def do_POST(self): try: if self.path.endswith("/restaurants/new"): ctype, pdict = cgi.parse_header( self.headers.getheader('content-type')) if ctype == 'multipart/form-data': fields = cgi.parse_multipart(self.rfile, pdict) messagecontent = fields.get('name') myFirstRestaurant = Restaurant(name=messagecontent[0]) session.add(myFirstRestaurant) session.commit() self.send_response(301) self.send_header('Content-Type', 'text/html') self.send_header('Location', '/restaurants') self.end_headers() if self.path.endswith("/edit"): ctype, pdict = cgi.parse_header( self.headers.getheader('content-type')) if ctype == 'multipart/form-data': fields = cgi.parse_multipart(self.rfile, pdict) messagecontent = fields.get('name') restaurantID = self.path.split("/")[2] restuarantQuery = session.query(Restaurant).filter_by( id=restaurantID).one() if restuarantQuery != []: restuarantQuery.name = messagecontent[0] session.add(restuarantQuery) session.commit() self.send_response(301) self.send_header('Content-Type', 'text/html') self.send_header('Location', '/restaurants') self.end_headers() if self.path.endswith("/delete"): ctype, pdict = cgi.parse_header( self.headers.getheader('content-type')) if ctype == 'multipart/form-data': restaurantID = self.path.split("/")[2] restuarantQuery = session.query(Restaurant).filter_by( id=restaurantID).one() if restuarantQuery != []: session.delete(restuarantQuery) session.commit() self.send_response(301) self.send_header('Content-Type', 'text/html') self.send_header('Location', '/restaurants') self.end_headers() """ self.send_response(201) self.end_headers() ctype, pdict = cgi.parse_header(self.headers.getheader('content-type')) if ctype == 'multipart/form-data': fields = cgi.parse_multipart(self.rfile, pdict) messagecontent = fields.get('message') output = "" output += "<html><body>" output += " <h2> Okay, how about this: </h2>" output += "<h1> %s </h1>" % messagecontent[0] 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 """ except: pass
def do_POST(self): try: if self.path.endswith("/restaurants/new"): ctype, pdict = cgi.parse_header( self.headers.getheader('content-type')) if ctype == 'multipart/form-data': fields = cgi.parse_multipart(self.rfile, pdict) messagecontent = fields.get('restaurantName') print messagecontent #crate a restaurant newRestaurant = Restaurant(name=messagecontent[0]) session.add(newRestaurant) session.commit() self.send_response(301) self.send_header('Content-type', 'text/html') self.send_header('Location', '/restaurants') self.end_headers() if self.path.endswith("/edit"): ctype, pdict = cgi.parse_header( self.headers.getheader('content-type')) if ctype == 'multipart/form-data': fields = cgi.parse_multipart(self.rfile, pdict) messagecontent = fields.get('restaurantName') print messagecontent RestaurantId = self.path.split("/")[2] Restau = session.query(Restaurant).filter_by( id=RestaurantId).one() Restau.name = messagecontent[0] session.add(Restau) session.commit() self.send_response(301) self.send_header('Content-type', 'text/html') self.send_header('Location', '/restaurants') self.end_headers() if self.path.endswith("/delete"): RestaurantId = self.path.split("/")[2] toDelete = session.query(Restaurant).filter_by( id=RestaurantId).one() if toDelete != []: session.delete(toDelete) session.commit() self.send_response(301) self.send_header('Content-type', 'text/html') self.send_header('Location', '/restaurants') self.end_headers() # self.send_response(301) # self.end_headers() # ctype, pdict = cgi.parse_header(self.headers.getheader('content-type')) # if ctype == 'multipart/form-data': # fields=cgi.parse_multipart(self.rfile, pdict) # messagecontent = fields.get('message') # output = "" # output += "<html><body>" # output += "<h2>Okay how about this : </h2>" # output += "<h1>%s </h1>" % messagecontent[0] # 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 except: pass
def do_POST(self): try: ctype, pdict = cgi.parse_header(self.headers.getheader('content-type')) if ctype == 'multipart/form-data': fields = cgi.parse_multipart(self.rfile, pdict) if self.path.endswith("hello"): self.send_response(301) self.send_header('Content-type', 'text/html') messagecontent = fields.get('message') output = "" output += "<html><body>" output += " <h2> Okay, how about this: </h2>" output += "<h1> %s </h1>" % messagecontent[0] 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 self.end_headers() elif self.path.endswith("/restaurants/new"): new_restaurant = fields.get('new_restaurant') restaurant1 = Restaurant(name=new_restaurant[0]) self.session.add(restaurant1) self.session.commit() self.send_response(301) self.send_header('Content-type', 'text/html') self.send_header('Location', '/restaurants') self.end_headers() elif self.path.endswith("/edit"): restaurant_new_name = fields.get('restaurant_new_name') restaurant_id = self.path.split("/")[2] restaurant1 = self.session.query(Restaurant).filter_by(id = restaurant_id).one() if restaurant1 != [] : restaurant1.name = restaurant_new_name[0] self.session.add(restaurant1) self.session.commit() self.send_response(301) self.send_header('Content-type', 'text/html') self.send_header('Location', '/restaurants') self.end_headers() elif self.path.endswith("/delete"): restaurant_id = self.path.split("/")[2] restaurant1 = self.session.query(Restaurant).filter_by(id = restaurant_id).one() if restaurant1 != [] : self.session.delete(restaurant1) self.session.commit() self.send_response(301) self.send_header('Content-type', 'text/html') self.send_header('Location', '/restaurants') self.end_headers() except: pass
# revert all of them back to the last commit by calling # session.rollback() session = DBSession() locale.setlocale(locale.LC_ALL, 'en_US.UTF-8') fake = Faker() for i in range(0, 10): user = User(name=fake.name(), email=fake.email(), picture='https://pbs.twimg.com/profile_images/2671170543/' '18debd694829ed78203a5a36dd364160_400x400.png') session.add(user) session.commit() for i in range(0, 10): restaurant = Restaurant(user_id=i + 1, name=fake.text()) session.add(restaurant) session.commit() course_elements = ["Entree", "Dessert", "Beverage", "Appetizer"] for i in range(0, 1000): restaurant = session.query(Restaurant).filter_by( id=random.randint(1, 10)).first() menuItem = MenuItem(user_id=restaurant.user_id, name=fake.lexify("???????? ??????"), description=fake.text(), price=locale.currency( round(random.randint(100, 10000) / 100, 2)), course=fake.random_element(course_elements),
def do_POST(self): try: if self.path.endswith("/delete"): restaurantID = int(self.path.split("/")[2]) selectedRestaurant = session.query(Restaurant).filter_by( id=restaurantID).one() session.delete(selectedRestaurant) session.commit() self.send_response(301) self.send_header('Location', '/restaurants') self.end_headers() return if self.path.endswith("/edit"): restaurantID = int(self.path.split("/")[2]) selectedRestaurant = session.query(Restaurant).filter_by( id=restaurantID).one() ctype, pdict = cgi.parse_header( self.headers.getheader('content-type')) if ctype == 'multipart/form-data': fields = cgi.parse_multipart(self.rfile, pdict) messagecontent = fields.get('updated_rest_name') if (messagecontent[0] != ''): print messagecontent[0] selectedRestaurant.name = messagecontent[0] session.add(selectedRestaurant) session.commit() self.send_response(301) self.send_header('Location', '/restaurants') self.end_headers() return if self.path.endswith("/restaurants/new"): ctype, pdict = cgi.parse_header( self.headers.getheader('content-type')) if ctype == 'multipart/form-data': fields = cgi.parse_multipart(self.rfile, pdict) messagecontent = fields.get('new_rest_name') if (messagecontent[0] != ''): print messagecontent[0] newRestaurant = Restaurant(name=messagecontent[0]) session.add(newRestaurant) session.commit() self.send_response(301) self.send_header('Location', '/restaurants') self.end_headers() return # self.send_response(301) # self.end_headers() # ctype, pdict = cgi.parse_header(self.headers.getheader('content-type')) # if ctype == 'multipart/form-data': # fields = cgi.parse_multipart(self.rfile, pdict) # messagecontent = fields.get('message') # output = "" # output += "<html><body>" # output += "<h2>How about this:</h2>" # output += "<h1>%s</h1>" % messagecontent[0] # 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 except: pass
def do_POST(self): try: print self.path if self.path.endswith("/restaurants/new"): ctype, pdict = cgi.parse_header(self.headers.getheader('content-type')) if ctype == 'multipart/form-data': fields = cgi.parse_multipart(self.rfile, pdict) restaurantName = fields.get('name') restaurant = Restaurant(name=restaurantName[0]) session.add(restaurant) session.commit() self.send_response(301) self.send_header("Content-type", 'text/html') self.send_header('Location', '/restaurants') self.end_headers() if self.path.endswith("/edit"): ctype, pdict = cgi.parse_header(self.headers.getheader('content-type')) if ctype == 'multipart/form-data': path = self.path.split("/") restaurantId = path[2] fields = cgi.parse_multipart(self.rfile, pdict) restaurantName = fields.get('name')[0] query = session.query(Restaurant).filter(Restaurant.id == restaurantId) restaurant = query.first() restaurant.name = restaurantName # session.addrestaurant) session.commit() self.send_response(301) self.send_header("Content-type", 'text/html') self.send_header('Location', '/restaurants') self.end_headers() if self.path.endswith("/delete"): ctype, pdict = cgi.parse_header(self.headers.getheader('content-type')) if ctype == 'multipart/form-data': path = self.path.split("/") restaurantId = path[2] query = session.query(Restaurant).filter(Restaurant.id == restaurantId) restaurant = query.first() session.delete(restaurant) session.commit() self.send_response(301) self.send_header("Content-type", 'text/html') self.send_header('Location', '/restaurants') self.end_headers() if self.path.endswith("/zzz"): self.send_response(301) self.end_headers() ctype, pdict = cgi.parse_header(self.headers.getheader('content-type')) if ctype == 'multipart/form-data': fields = cgi.parse_multipart(self.rfile, pdict) messagecontent = fields.get('message') newName = messagecontent[0] self.wfile.write(output) print(output) except IOError: pass
def do_POST(self): try: if self.path.endswith("/edit"): ctype, pdict = cgi.parse_header( self.headers.getheader('content-type')) if ctype == 'multipart/form-data': fields = cgi.parse_multipart(self.rfile, pdict) messagecontent = fields.get('newRestaurantName') restaurantID = self.path.split("/")[2] myQuery = session.query(Restaurant).filter_by( id=restaurantID).one() # Update the restaurant name if myQuery != []: myQuery.name = messagecontent[0] session.add(myQuery) session.commit() print messagecontent, messagecontent[0] self.send_response(301) self.send_header('Content-type', 'text/html') self.send_header('Location', '/restaurants') self.end_headers() # Objective 5 Step 3 - Delete /restarants/x - deal with the POST submission if self.path.endswith("/delete"): ctype, pdict = cgi.parse_header( self.headers.getheader('content-type')) if ctype == 'multipart/form-data': fields = cgi.parse_multipart(self.rfile, pdict) #messagecontent = fields.get('newRestaurantName') restaurantID = self.path.split("/")[2] myQuery = session.query(Restaurant).filter_by( id=restaurantID).one() # Delete the restaurant name if myQuery != []: session.delete(myQuery) session.commit() self.send_response(301) self.send_header('Content-type', 'text/html') self.send_header('Location', '/restaurants') self.end_headers() # Objective 3 Step 3 - Create /restarants/new page - deal with the POST submission if self.path.endswith("/restaurants/new"): ctype, pdict = cgi.parse_header( self.headers.getheader('content-type')) if ctype == 'multipart/form-data': fields = cgi.parse_multipart(self.rfile, pdict) messagecontent = fields.get('newRestaurantName') # Create new Restaurant Object newRestaurant = Restaurant(name=messagecontent[0]) session.add(newRestaurant) session.commit() print messagecontent, messagecontent[0] self.send_response(301) self.send_header('Content-type', 'text/html') self.send_header('Location', '/restaurants') self.end_headers() except: pass
def do_POST(self): try: if self.path.endswith("/restaurants/new"): ctype, pdict = cgi.parse_header( self.headers.getheader('content-type')) if ctype == 'multipart/form-data': #collect fields in form fields = cgi.parse_multipart(self.rfile, pdict) messagecontent = fields.get('newRestaurantName') # add to db newRestaurant = Restaurant(name=messagecontent[0]) session.add(newRestaurant) session.commit() self.send_response(301) self.send_header('Content-type', 'text/html') # URL redirection self.send_header('Location', '/restaurants') self.end_headers() if self.path.endswith("/edit"): ctype, pdict = cgi.parse_header( self.headers.getheader('content-type')) if ctype == 'multipart/form-data': #collect fields in form fields = cgi.parse_multipart(self.rfile, pdict) messagecontent = fields.get('editRestaurantName') # update db if entry exists restaurantID = self.path.rsplit('/', 2)[-2] restaurant = session.query(Restaurant).filter_by(id=restaurantID).one() if restaurant != []: restaurant.name = messagecontent[0] session.add(restaurant) session.commit() self.send_response(301) self.send_header('Content-type', 'text/html') # URL redirection self.send_header('Location', '/restaurants') self.end_headers() if self.path.endswith("/delete"): # delete entry in db if entry exists restaurantID = self.path.rsplit('/', 2)[-2] restaurant = session.query(Restaurant).filter_by(id=restaurantID).one() if restaurant != []: session.delete(restaurant) session.commit() self.send_response(301) self.send_header('Content-type', 'text/html') # URL redirection self.send_header('Location', '/restaurants') self.end_headers() except: pass
def do_POST(self): print("==============I'm doing POST=================") try: print("================I'm trying================") # if self.path.endswith("/hello"): # self.send_response(301) # self.end_headers() # ctype, pdict = cgi.parse_header(self.headers.get('content-type')) # pdict['boundary'] = bytes(pdict['boundary'], "utf-8") # if ctype == 'multipart/form-data': # fields = cgi.parse_multipart(self.rfile, pdict) # messagecontent = fields.get('message') # output = "" # output += "<html><body>" # output += " <h2>Okay, how about this: </h2>" # output += " <h1> %s </h1>" % messagecontent[0].decode() # 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.encode()) # print(output) # return if self.path.endswith("/restaurants/new"): print("==============Right page!!!=================") ctype, pdict = cgi.parse_header( self.headers.get('content-type')) pdict['boundary'] = bytes(pdict['boundary'], "utf-8") if ctype == 'multipart/form-data': fields = cgi.parse_multipart(self.rfile, pdict) messagecontent = fields.get('restaurant_name') print(messagecontent[0]) #Create new Restaurant class new_restaurant = Restaurant( name=messagecontent[0].decode()) session.add(new_restaurant) session.commit() self.send_response(301) self.send_header('Content-type', 'text/html') self.send_header('Location', '/restaurants') self.end_headers() if self.path.endswith("/edit"): print("==============Edit page!!!=================") ctype, pdict = cgi.parse_header( self.headers.get('content-type')) pdict['boundary'] = bytes(pdict['boundary'], "utf-8") if ctype == 'multipart/form-data': fields = cgi.parse_multipart(self.rfile, pdict) messagecontent = fields.get('restaurant_name') print(messagecontent[0]) #Edit restaurant name restaurant_id_path = self.path.split("/")[2] restaurant_query = session.query(Restaurant).filter_by( id=restaurant_id_path).one() if restaurant_query != []: restaurant_query.name = messagecontent[0].decode() session.add(restaurant_query) session.commit() self.send_response(301) self.send_header('Content-type', 'text/html') self.send_header('Location', '/restaurants') self.end_headers() if self.path.endswith("/delete"): print("==============Delete page!!!=================") restaurant_id_path = self.path.split("/")[2] restaurant_query = session.query(Restaurant).filter_by( id=restaurant_id_path).one() if restaurant_query != []: session.delete(restaurant_query) session.commit() self.send_response(301) self.send_header('Content-type', 'text/html') self.send_header('Location', '/restaurants') self.end_headers() except: pass
def do_POST(self): try: if self.path.endswith("/delete"): restaurantIDPath = self.path.split("/")[2] restaurantDelete = session.query(Restaurant).filter_by( id=restaurantIDPath).one() if restaurantDelete != []: session.delete(restaurantDelete) session.commit() self.send_response(301) self.send_header('Content-type', 'text/html') self.send_header('Location', '/restaurant') self.end_headers() if self.path.endswith("/edit"): ctype, pdict = cgi.parse_header( self.headers.getheader('content-type')) if ctype == 'multipart/form-data': fields = cgi.parse_multipart(self.rfile, pdict) messagecontent = fields.get('newRestaurant') restaurantIDPath = self.path.split("/")[2] restaurantEdit = session.query(Restaurant).filter_by( id=restaurantIDPath).one() if restaurantEdit != []: restaurantEdit.name = messagecontent[0] session.add(restaurantEdit) session.commit() self.send_response(301) self.send_header('Content-type', 'text/html') self.send_header('Location', '/restaurant') self.end_headers() if self.path.endswith("/restaurant/new"): ctype, pdict = cgi.parse_header( self.headers.getheader('content-type')) # parses a html form header such as content type into a main value and dictionary # of paramaters if ctype == 'multipart/form-data': # check if this if form data being received fields = cgi.parse_multipart( self.rfile, pdict ) # then make variable fields, and use parse.multipart which will collect all the fields in a form #messagecontent = fields.get('message') # to get out the value of a specfic field or set of fields and store them in an anrray newRestaurant = fields.get('newRestaurant') # call this field 'message' here and when create html form. #So now that I've received a post request I can decide what to tell the client with the new information I've received. restaurant1 = Restaurant(name=newRestaurant[0]) session.add(restaurant1) session.commit() self.send_response(301) self.send_header('Content-type', 'text/html') self.send_header('Location', '/restaurant') self.end_headers() except: pass
def new_restaurant(name): userRestaurant = Restaurant(name=name) session.add(userRestaurant) session.commit() print("Added new restaurant, full list: ", [(e.id, e.name) for e in session.query(Restaurant).all()])
from sqlalchemy import create_engine from sqlalchemy.orm import sessionmaker from database_setup import Base, Restaurant, MenuItem ##inicializacion de base de datos engine = create_engine('sqlite:///restaurant.db') Base.metadata.bind = engine DBSession = sessionmaker(bind=engine) session = DBSession() ##Creating new restaurant myFirstResto = Restaurant(name="Dex rest") session.add(myFirstResto) session.commit() session.query(Restaurant).all() ##Creating new MenuItem pizza = MenuItem(name="Pizza", description="italian pizza", course="Entree", price="$8.99", restaurant=myFirstResto) session.add(pizza) session.commit() session.query(MenuItem).all()
# revert all of them back to the last commit by calling # session.rollback() session = DBSession() # Create dummy user User1 = User( name="Robo Barista", email="*****@*****.**", picture= 'https: //pbs.twimg.com/profile_images/2671170543/18debd694829ed78203a5a36dd364160_400x400.png' ) session.add(User1) session.commit() # Menu for UrbanBurger restaurant1 = Restaurant(user_id=1, name="Urban Burger") session.add(restaurant1) session.commit() menuItem2 = MenuItem( user_id=1, name="Veggie Burger", description="Juicy grilled veggie patty with tomato mayo and lettuce", price="$7.50", course="Entree", restaurant=restaurant1) session.add(menuItem2) session.commit()
from sqlalchemy import create_engine from sqlalchemy.orm import sessionmaker from database_setup import Restaurant, MenuItem ,Base engine = create_engine('sqlite:///myrestaurantapp.db') Base.metadata.bind = engine DBsession = sessionmaker(bind=engine) session = DBsession() #Restaurant-1 Fishes restaurant1 = Restaurant(name = ' Restaurant Fishes ') session.add(restaurant1) session.commit() menuitem1 = MenuItem(name ='Alaskan salmon', description = 'Salmon is a great option for your diet overall', price ='$5', course = 'fishes', restaurant = restaurant1) session.add(menuitem1) session.commit() menuitem2 = MenuItem(name ='Cod', description = 'This flaky white fish is a great source of vitamin B-12', price ='$10', course = 'fishes', restaurant = restaurant1) session.add(menuitem2) session.commit() menuitem3 = MenuItem(name ='Herring', description = 'A fatty fish similar to sardines', price ='$15', course = 'fishes', restaurant = restaurant1) session.add(menuitem3) session.commit()
from sqlalchemy import create_engine, asc from sqlalchemy.orm import sessionmaker, relationship from database_setup import Base, Restaurant, MenuItem, User engine = create_engine('sqlite:///restaurantmenu.db') Base.metadata.bind = engine DBSession = sessionmaker(bind=engine) session = DBSession() # Added fake database in menu items for our website before login. restaurant1 = Restaurant(id=1, name='Uncle jacks', image='https://pbs.twimg.com/media/Cu7Z4toUIAAx5bI.jpg', # noqa description='Awesome place') session.add(restaurant1) session.commit() restaurant2 = Restaurant(id=2, name='keventers', image='https://content4.jdmagicbox.com/comp/bangalore/k7/080pxx80.xx80.170407082326.i1k7/catalogue/keventers-new-bel-road-bangalore-milk-shake-retailers-1wjnylj.jpg', # noqa description='Awesome place') session.add(restaurant2) session.commit() restaurant3 = Restaurant(id=3, name='Beer cafe', image='https://content1.jdmagicbox.com/comp/delhi/n2/011pxx11.xx11.140529115354.n3n2/catalogue/the-beer-cafe-nehru-place-delhi-fine-dining-restaurants-rs-1000-to-rs-2000-2l9mtno.jpg', # noqa description='Awesome place') session.add(restaurant3) session.commit()
# database session object. Any change made against the objects in the # session won't be persisted into the database until you call # session.commit(). If you're not happy about the changes, you can # revert all of them back to the last commit by calling # session.rollback() session = DBSession() # Create dummy user User1 = User(name="Robo Barista", email="*****@*****.**", path="none", picture='https://pbs.twimg.com/profile_images/2671170543/18debd694829ed78203a5a36dd364160_400x400.png') session.add(User1) session.commit() # Menu for UrbanBurger restaurant1 = Restaurant(user_id=1, name="Urban Burger", cuisine="American", picture_id=8) session.add(restaurant1) session.commit() menuItem2 = MenuItem(user_id=1, name="Veggie Burger", description="Juicy grilled veggie patty with tomato mayo and lettuce", price="$7.50", course="Entree", restaurant=restaurant1) session.add(menuItem2) session.commit() menuItem1 = MenuItem(user_id=1, name="French Fries", description="with garlic and parmesan", price="$2.99", course="Appetizer", restaurant=restaurant1) session.add(menuItem1)
from config_db import DATABASE_URL from database_setup import Users, Restaurant, Base, MenuItem engine = create_engine(DATABASE_URL) Base.metadata.bind = engine DBSession = sessionmaker(bind=engine) session = scoped_session(DBSession) user = Users(name="root", email="*****@*****.**") session.add(user) session.commit() # Menu for UrbanBurger restaurant1 = Restaurant(name="Urban Burger", user=user) session.add(restaurant1) session.commit() menuItem2 = MenuItem(name="Veggie Burger", description=""" Juicy grilled veggie patty with tomato mayo and lettuce """, price="$7.50", course="Entree", restaurant=restaurant1, user=user) session.add(menuItem2) session.commit()
def do_POST(self): if self.path.endswith("edit"): print self.path ctype,pdict=cgi.parse_header(self.headers.getheader('content-type')) restaurantId=self.path.split("/")[2] if(ctype=='multipart/form-data'): ##get the fields from the webserverhandler that were filled in the form fields=cgi.parse_multipart(self.rfile,pdict) messagecontent=fields.get('newRestaurantName') resNewName=messagecontent[0] restaurant=session.query(Restaurant).filter_by(id=restaurantId).one() restaurant.name=resNewName session.commit() self.send_response(301) self.send_header('Content-type', 'text/html') self.send_header('Location', '/restaurants') self.end_headers() return if self.path.endswith("/restaurants/new"): ctype,pdict=cgi.parse_header(self.headers.getheader('content-type')) if(ctype=='multipart/form-data'): ##get the fields from the webserverhandler that were filled in the form fields=cgi.parse_multipart(self.rfile,pdict) messagecontent=fields.get('newRestaurantName') #Create a new resaurant and insert it to the database newRestaurant = Restaurant(name=messagecontent[0]) session.add(newRestaurant) session.commit() self.send_response(301) self.send_header('Content-type', 'text/html') self.send_header('Location', '/restaurants') self.end_headers() return if self.path.endswith("/hello"): self.send_response(301) self.send_header('Content-type', 'text/html') self.end_headers() ctype, pdict = cgi.parse_header( self.headers.getheader('content-type')) if ctype == 'multipart/form-data': fields = cgi.parse_multipart(self.rfile, pdict) messagecontent = fields.get('message') output = "" output += "<html><body>" output += " <h2> Okay, how about this: </h2>" output += "<h1> %s </h1>" % messagecontent[0] 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)
def do_POST(self): try: if self.path.endswith("/restaurants/new"): ctype, pdict = cgi.parse_header(self.headers.getheader('content-type')) if ctype == 'multipart/form-data': fields = cgi.parse_multipart(self.rfile, pdict) messagecontent = fields.get('newRestaurantName') # Create new Restaurant class newRestaurant = Restaurant(name = messagecontent[0]) session.add(newRestaurant) session.commit() self.send_response(301) self.send_header('Content-type', 'text/html') self.send_header('Location', '/restaurants') self.end_headers() return if self.path.endswith("/edit"): print("Request to edit Restaurant OK.") ctype, pdict = cgi.parse_header(self.headers.getheader('content-type')) if ctype == 'multipart/form-data': fields = cgi.parse_multipart(self.rfile, pdict) messagecontent = fields.get('newRestaurantName') restaurantIDPath = self.path.split("/")[2] print("Restaurant with id = %s changed." % restaurantIDPath) myRestaurantQuery = session.query(Restaurant).filter_by(id = restaurantIDPath).one() if myRestaurantQuery != []: myRestaurantQuery.name = messagecontent[0] print("New restaurant name: %s" % myRestaurantQuery.name) session.add(myRestaurantQuery) session.commit() self.send_response(301) self.send_header('Content-type', 'text/html') self.send_header('Location', '/restaurants') self.end_headers() return if self.path.endswith("/delete"): restaurantIDPath = self.path.split("/")[2] print("Request to delete Restaurant with ID = %s" % restaurantIDPath) myRestaurantQuery = session.query(Restaurant).filter_by(id = restaurantIDPath).one() if myRestaurantQuery != []: session.delete(myRestaurantQuery) session.commit() print("Restaurant %s successfully removed" % myRestaurantQuery.name) self.send_response(301) self.send_header('Content-type', 'text/html') self.send_header('Location', '/restaurants') self.end_headers() return # self.send_response(301) # self.send_header('Content-type', 'text/html') # self.end_headers() # ctype, pdict = cgi.parse_header( # self.headers.getheader('content-type')) # if ctype == 'multipart/form-data': # fields = cgi.parse_multipart(self.rfile, pdict) # messagecontent = fields.get('message') # output = "" # output += "<html><body>" # output += " <h2> Okay, how about this: </h2>" # output += "<h1> %s </h1>" % messagecontent[0] # 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 except: pass
def do_POST(self): try: print self.path print 2 engine = create_engine('sqlite:///restaurantmenu.db') Base.metadata.bind = engine DBSession = sessionmaker(bind=engine) session = DBSession() if self.path.endswith("/restaurants/new"): #if self.path.endswith("/restaurants/new"): ctype, pdict = cgi.parse_header( self.headers.getheader('content-type')) if ctype == 'multipart/form-data': fields = cgi.parse_multipart(self.rfile, pdict) restaurantName = fields.get('restaurantName') print(restaurantName[0]) restaurant1 = Restaurant(name=restaurantName[0]) session.add(restaurant1) session.commit() print(restaurant1) self.send_response(301) self.send_header('Content-type', 'text/html') self.send_header('Location', '/restaurants') self.end_headers() if self.path.endswith("/edit"): restaurantPathId = self.path.split('/')[2] print 4 print restaurantPathId restaurantQuery = session.query(Restaurant).filter_by( id=restaurantPathId).one() print restaurantQuery ctype, pdict = cgi.parse_header( self.headers.getheader('content-type')) if ctype == 'multipart/form-data': fields = cgi.parse_multipart(self.rfile, pdict) restaurantName = fields.get('newRestaurantName') print(restaurantName[0]) restaurantQuery.name = restaurantName[0] session.add(restaurantQuery) session.commit() print(restaurantQuery) self.send_response(301) self.send_header('Content-type', 'text/html') self.send_header('Location', '/restaurants') self.end_headers() if self.path.endswith("/delete"): restaurantPathId = self.path.split('/')[2] restaurantQuery = session.query(Restaurant).filter_by( id=restaurantPathId).one() session.delete(restaurantQuery) session.commit() print(restaurantQuery) self.send_response(301) self.send_header('Content-type', 'text/html') self.send_header('Location', '/restaurants') self.end_headers() except: pass
# revert all of them back to the last commit by calling # session.rollback() session = DBSession() # Create dummy user User1 = User( name="Robo Barista", email="*****@*****.**", picture= 'https://pbs.twimg.com/profile_images/2671170543/18debd694829ed78203a5a36dd364160_400x400.png' ) session.add(User1) session.commit() # Menu for UrbanBurger restaurant1 = Restaurant(user_id=1, name="Urban Burger") session.add(restaurant1) session.commit() menuItem2 = MenuItem( user_id=1, name="Veggie Burger", description="Juicy grilled veggie patty with tomato mayo and lettuce", price="$7.50", course="Entree", restaurant=restaurant1) session.add(menuItem2) session.commit()
from sqlalchemy import create_engine from sqlalchemy.orm import sessionmaker from database_setup import Base, Restaurant, MenuItem engine = create_engine('sqlite:///restaurantmenu.db') Base.metadata.bind = engine DBSession = sessionmaker(bind=engine) session = DBSession() # Add Restaurant myFirstRestaurant = Restaurant(name="Fish of Bangladesh") session.add(myFirstRestaurant) # Add Shrimp dopiaza shrimp_dopiaza = MenuItem( name='Shrimp Dopiaza', course='Entree', description='Made with lots of onion and spicy curry powder', price='BDT 200', restaurant=myFirstRestaurant) # Query/Read vegburg = session.query(MenuItem).filter_by(name='Veggie Burger') for item in vegburg: print item.id, item.name, item.price, item.restaurant.name session.query(MenuItem).all() session.query(MenuItem).first() somemenu = session.query(MenuItem).slice(2, 5) for item in somemenu: print item.id, item.name, item.price, item.restaurant.name urbanvb = session.query(MenuItem).filter_by(id=10).one()
from sqlalchemy import create_engine from sqlalchemy.orm import sessionmaker from database_setup import Base, Restaurant, MenuItem engine = create_engine('sqlite:///restaurantmenu.db') Base.metadata.bind = engine DBSession = sessionmaker(engine) session = DBSession() myFirstRestaurant = Restaurant(name="Pizza Palace") session.add(myFirstRestaurant) session.commit() print(session.query(Restaurant).all()) cheesepizza = MenuItem(name="Cheese Pizza", price="$8.99", description="tasty", course="Entree", restaurant=myFirstRestaurant) session.add(cheesepizza) session.commit() print(session.query(MenuItem).all())
from sqlalchemy import create_engine from sqlalchemy.orm import sessionmaker from database_setup import Base, Restaurant, MenuItem engine = create_engine('sqlite:///restaurantmenu.db') Base.metadata.bind = engine DBSession = sessionmaker(bind=engine) session = DBSession() myFirstRestaurant = Restaurant(name="Pizza palazio") cheesepizza = MenuItem(name="Chesse Pizza", description="made with natural ingredient", course="Entree", price="8.99", restaurant=myFirstRestaurant) session.add(myFirstRestaurant) session.add(cheesepizza) session.commit() print(session.query(Restaurant).all()) # NECESSARY SESSION #engine = create_engine('sqlite:///restaurantmenu.db') #Base.metadata.bind = engine #DBSession = sessionmaker(bind = engine) #session = DBSession() # CREATE DOC # newRestaurant = Restaurant(name = "Pizza palazio") # session.add(newRestaurant)
# revert all of them back to the last commit by calling # session.rollback() session = DBSession() # Create dummy user User1 = User( name="Corie Wilson", email="*****@*****.**", picture= 'https://image.shutterstock.com/image-photo/healthy-food-sources-folic-acid-260nw-543889855.jpg' ) session.add(User1) session.commit() # American Cookies Menu restaurant1 = Restaurant(user_id=4, name="American Cookies") session.add(restaurant1) session.commit() menuItem1 = MenuItem( user_id=4, name="Ice Cream Cookies Sandwich", description="Small Size Vanilla Ice Cream Cookie Sandwich", price="$12.99", course="Appetizer", restaurant=restaurant1) session.add(menuItem1) session.commit()
def do_POST(self): try: if self.path.endswith("/restaurants/new"): ctype, pdict = cgi.parse_header(self.headers.getheader('content-type')) if ctype == 'multipart/form-data': fields = cgi.parse_multipart(self.rfile, pdict) messagecontent = fields.get('name') #Create new Restaurant class restaurant = Restaurant(name = messagecontent[0]) session.add(restaurant) session.commit() self.send_response(301) self.send_header('Content-type', 'text/html') self.send_header('Location', '/restaurants') self.end_headers() return if self.path.endswith("/edit"): ctype, pdict = cgi.parse_header(self.headers.getheader('content-type')) if ctype == 'multipart/form-data': fields = cgi.parse_multipart(self.rfile, pdict) messagecontent = fields.get('name') restaurantIDPath = self.path.split("/")[2] #Edit new Restaurant class restaurant = session.query(Restaurant).filter_by(id = restaurantIDPath).one() if restaurant != []: restaurant.name = messagecontent[0] session.add(restaurant) session.commit() self.send_response(301) self.send_header('Content-type', 'text/html') self.send_header('Location', '/restaurants') self.end_headers() return if self.path.endswith("/delete"): # ctype, pdict = cgi.parse_header(self.headers.getheader('content-type')) # if ctype == 'multipart/form-data': # fields = cgi.parse_multipart(self.rfile, pdict) # messagecontent = fields.get('name') restaurantIDPath = self.path.split("/")[2] #Delete Restaurant class restaurant = session.query(Restaurant).filter_by(id = restaurantIDPath).one() if restaurant != []: session.delete(restaurant) session.commit() self.send_response(301) self.send_header('Content-type', 'text/html') self.send_header('Location', '/restaurants') self.end_headers() return except: pass