def do_POST(self): try: if self.path.endswith('restaurants/new'): # get post params from request 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') # create restaurant in database restaurant = Restaurant() restaurant.name=messagecontent[0] session.add(restaurant) session.commit() # send response - redirect to restaurant list self.send_response(301) self.send_header('Content-type', 'text/html') self.send_header('Location', '/restaurants') self.end_headers() elif self.path.endswith('/edit'): # get post params from request spl = self.path.split('/') restaurant_id = int(spl[-2]) 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') # get restaurant from database restaurant = session.query(Restaurant).filter_by(id=restaurant_id).first() restaurant.name=messagecontent[0] session.add(restaurant) session.commit() # send response - redirect to restaurant list self.send_response(301) self.send_header('Content-type', 'text/html') self.send_header('Location', '/restaurants') self.end_headers() elif self.path.endswith('/delete'): # get post params from request spl = self.path.split('/') restaurant_id = int(spl[-2]) # remove restaurant from database session.query(Restaurant).filter_by(id=restaurant_id).delete() session.commit() # send response - redirect to restaurant list 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: 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): ctype, pdict = cgi.parse_header(self.headers.getheader('content-type')) if self.path.endswith('/restaurants/new'): if ctype == 'multipart/form-data': fields = cgi.parse_multipart(self.rfile, pdict) messagecontent = fields.get('message') rest_names = fields.get("rest_name") myFirstRestaurant = Restaurant(name=rest_names[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() elif self.path.endswith('/edit'): if ctype == 'multipart/form-data': fields = cgi.parse_multipart(self.rfile, pdict) messagecontent = fields.get('message') rest_names = fields.get("rest_name") idx = int(re.findall("ants/(\d+)/edit", self.path)) some_restaurant = Restaurant(id=idx) some_restaurant = session.query(Restaurant).filter_by(id=idx).one() some_restaurant.name = rest_names[0] session.add(some_restaurant) session.commit() self.send_response(301) self.send_header('Content-type', 'text/html') self.send_header('Location', '/restaurants') self.end_headers()
def restaurant_create(self, name): print("CREATE: {}".format(name)) restaurant = Restaurant() restaurant.name = namem stage = DBSessionMaker() stage.add(restaurant) stage.commit()
def post(self, pdict): fields = cgi.parse_multipart(self.rfile, pdict) if 'delete' in fields.keys(): post_data = fields['delete'][0].split('Delete ')[1] del_rest = session.query(Restaurant).filter_by( name=post_data).one() session.delete(del_rest) session.commit() restaurants = session.query(Restaurant).all() self.render('hello.html', restaurants=restaurants) elif 'name' in fields.keys(): rest_name = fields['name'][0] rest = Restaurant(name=rest_name) session.add(rest) session.commit() restaurants = session.query(Restaurant).all() self.render('hello.html', restaurants=restaurants) elif 'rename_s' in fields.keys(): name = fields['rename_s'][0].split('Rename ')[1] rest_name = fields['rename'][0] print rest_name rest = session.query(Restaurant).filter_by(name=name).one() rest.name = rest_name session.add(rest) session.commit() restaurants = session.query(Restaurant).all() self.render('hello.html', restaurants=restaurants)
def create_restaurant(): # create new restaurant body = request.json name = body['name'] if name is None or name == "": raise ApiError(u"Введите название ресторана.") rest = Restaurant() rest.name = name rest.user_id = current_user.id created_rest = Restaurant.get_by_name(rest.name) if created_rest is not None: raise ApiError(u"Ресторан с данным именем уже существует.") location = body.get('location') if location is not None: rest.latitude = location.get('longitude') rest.latitude = location.get('latitude') session.add(rest) session.commit() return jsonify(rest.serialize)
def do_POST(self): try: if self.path.endswith('/restaurants/new'): engine = create_engine('sqlite:///restaurantmenu.db') Base.metadata.bind = engine session = orm.sessionmaker(bind=engine)() ctype, pdict = cgi.parse_header( self.headers.getheader('content-type')) if ctype == 'multipart/form-data': fields = cgi.parse_multipart(self.rfile, pdict) restaurant = Restaurant(name=fields.get('name')[0]) session.add(restaurant) session.commit() self.send_response(301) self.send_header('Location', '/restaurants') self.end_headers() return if re.match(pattern='/restaurants/[0-9]+/edit', string=self.path): engine = create_engine('sqlite:///restaurantmenu.db') Base.metadata.bind = engine session = orm.sessionmaker(bind=engine)() ctype, pdict = cgi.parse_header( self.headers.getheader('content-type')) if ctype == 'multipart/form-data': fields = cgi.parse_multipart(self.rfile, pdict) restaurant_id = self.path.split('/')[2] restaurant = session.query(Restaurant).filter_by( id=restaurant_id).one() restaurant.name = fields.get('name')[0] session.add(restaurant) session.commit() self.send_response(301) self.send_header('Location', '/restaurants') self.end_headers() if re.match(pattern='/restaurants/[0-9]+/delete', string=self.path): engine = create_engine('sqlite:///restaurantmenu.db') Base.metadata.bind = engine session = orm.sessionmaker(bind=engine)() restaurant_id = self.path.split('/')[2] restaurant = session.query(Restaurant).filter_by( id=restaurant_id).one() session.delete(restaurant) session.commit() self.send_response(301) 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': fields = cgi.parse_multipart(self.rfile, pdict) message_content = fields.get('name') restaurant_name = message_content[0] restaurant = Restaurant(name = restaurant_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("/edit"): ctype, pdict = cgi.parse_header( self.headers.getheader('content-type')) if ctype == 'multipart/form-data': fields = cgi.parse_multipart(self.rfile, pdict) message_content = fields.get('new_name') new_name = message_content[0] restaurant_id = self.path.split("/")[2] restaurant = session.query(Restaurant).filter_by(id=restaurant_id).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"): restaurant_id = self.path.split("/")[2] restaurant = session.query(Restaurant).filter_by(id=restaurant_id).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() except: pass
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': newRestaurant = Restaurant(name=request.form['name']) if request.form['name']: newRestaurant.name = request.form['name'] session.add(newRestaurant) session.commit() return redirect(url_for('showRestaurants')) else: return render_template('newrestaurant.html')
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 add_restaurant(): if request.method == 'POST': name = request.form['restaurant_name'] restaurant = Restaurant() restaurant.name = name session.add(restaurant) session.commit() flash("new restaurant created") return redirect("/restaurant") elif request.method == 'GET': return render_template('add_category.html', verified=verified)
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': #check if form data fields=cgi.parse_multipart(self.rfile, pdict) #collect all data messagecontent = fields.get('restaurant_name') #get specific restaurant = Restaurant(name=messagecontent[0]) session.add(restaurant) session.commit() self.send_response(301) #redirect self.send_header('Content-type', 'text/html') self.send_header('Location', '/restaurants') self.end_headers() elif self.path.endswith("/edit"): ctype, pdict = cgi.parse_header(self.headers.getheader('content-type')) if ctype =='multipart/form-data': #check if form data fields=cgi.parse_multipart(self.rfile, pdict) #collect all data messagecontent = fields.get('restaurant_name') #get specific restaurantId = self.path.split("/")[2] restaurant = session.query(Restaurant).filter_by(id = restaurantId).first() 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() elif self.path.endswith("/delete"): ctype, pdict = cgi.parse_header(self.headers.getheader('content-type')) if ctype =='multipart/form-data': #check if form data fields=cgi.parse_multipart(self.rfile, pdict) #collect all data messagecontent = fields.get('delete') #get specific if messagecontent != None: restaurantId = self.path.split("/")[2] restaurant = session.query(Restaurant).filter_by(id = restaurantId).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() except: print 'EXCEPTION' 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('newRestaurantName') restaurant = Restaurant(name=messagecontent[0]) self.db.addRestaurant(restaurant) 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('newRestaurantName') restaurantId = self.path.split("/")[2] restaurant = self.db.filterRestaurants(id=restaurantId)[0] restaurant.name = messagecontent[0] self.db.addRestaurant(restaurant) 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] restaurant = self.db.filterRestaurants(id=restaurantId)[0] self.db.removeRestaurant(restaurant) 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': 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 newRestaurant(): """Create a new restaurant.""" # If there is no email in the login session AKA no current login user. if 'email' not in login_session: return redirect('/login') session = DBSession() # Prevent threading error. restaurant = Restaurant(name="") if request.method == 'POST': if request.form['name']: restaurant.name = request.form['name'] session.add(restaurant) session.commit() flash("New restaurant created.") return redirect(url_for('showRestaurant')) else: return render_template('newrestaurant.html')
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) message_content = fields.get('restaurantname') restaurant = Restaurant(name=message_content[0]) session.add(restaurant) session.commit() self.send_response(301) 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) message_content = fields.get('restaurantname') restaurant_id = int(self.path.split('/')[-2]) restaurant = session.query(Restaurant).get(restaurant_id) restaurant.name = message_content[0] session.commit() self.send_response(301) 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': restaurant_id = int(self.path.split('/')[-2]) restaurant = session.query(Restaurant).get(restaurant_id) session.delete(restaurant) session.commit() self.send_response(301) self.send_header('Location', '/restaurants') self.end_headers() return except: pass
def do_POST(self): try: 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('restaurant_name') restaurant_edit = fields.get('name_to_be_edited') restaurant_delete = fields.get('name_to_be_deleted') name = messagecontent[0] if restaurant_delete is not None: name_delete = restaurant_delete[0] print "++++++++++++" print name_delete print "++++++++++++" if name_delete: print "deleting section" restaurant_delete_query = session.query( Restaurant).filter_by(name='%s' % name_delete).all()[0] session.delete(restaurant_delete_query) session.commit() if restaurant_edit is not None: name2 = restaurant_edit[0] restaurant = session.query(Restaurant).filter_by( name='%s' % name2).all() newrestaurant = restaurant[0] else: newrestaurant = Restaurant() newrestaurant.id = random.randint(1, 10000) print newrestaurant.name print newrestaurant.id print messagecontent newrestaurant.name = messagecontent[0] session.add(newrestaurant) session.commit() except: print(traceback.print_exc())
def newRestaurant(): if request.method == 'POST': restaurant = Restaurant() restaurant.name = request.form['name'] restaurant.category = request.form['category'] restaurant.description = request.form['description'] restaurant.rating = request.form['rating'] restaurant.price_range = request.form['price_range'] restaurant.address = request.form['address'] restaurant.latlng = request.form['latlng'] restaurant.telephone = request.form['telephone'] restaurant.url = request.form['url'] restaurant.hours = request.form['hours'] restaurant.image_url = request.form['image_url'] sqlsession.add(restaurant) sqlsession.commit() flash("New Restaurant Created") return redirect(url_for('showRestaurants')) else: return render_template('newRestaurant.html')
def do_POST(self): print(self.log_request()) try: if self.path.endswith(NEW_LINK) or self.path.endswith(EDIT_LINK) or self.path.endswith(DELETE_LINK): self.send_response(301) self.send_header("Content-type", "text/html") self.send_header('Location', '/restaurants') out = "" if self.path.endswith(NEW_LINK): fields = self._get_post_param() nameofrestaurant = fields.get("restaurantName")[0] if fields else None with DBSession() as sess: restaurant = Restaurant(name=nameofrestaurant) sess.add(restaurant) out += wrapwithh1("Restaurant {} was added successfully!".format(nameofrestaurant)) elif self.path.endswith(EDIT_LINK): fields = self._get_post_param() nameofrestaurant = fields.get("restaurantName")[0] if fields else None if nameofrestaurant: id = self.path.split("/")[-2] with DBSession() as sess: restaurant = sess.query(Restaurant).filter_by(id=id).first() restaurant.name = nameofrestaurant out += wrapwithh1("Restaurant {} was edited successfully!".format(nameofrestaurant)) elif self.path.endswith(DELETE_LINK): id = self.path.split("/")[-2] with DBSession() as sess: restaurant = sess.query(Restaurant).filter_by(id=id).first() nameofrestaurant = restaurant.name sess.delete(restaurant) out += wrapwithh1("Restaurant {} was deleted successfully!".format(nameofrestaurant)) out += "<br/>" out += link(text="Back to Restaurants list", link=LIST_LINK) self.end_headers() self._send_responce(out) except Exception as e: raise
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': message = cgi.parse_multipart(self.rfile, pdict) messagecontent = message.get('restaurant_name') 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.startswith("/restaurant") and self.path.endswith( "/edit"): restaurant_id = self.path.split('/')[2] restaurant = session.query(Restaurant).filter_by( id=restaurant_id).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('restaurant_name') 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.startswith("/restaurant") and self.path.endswith( "/delete"): restaurant_id = self.path.split('/')[2] restaurant = session.query(Restaurant).filter_by( id=restaurant_id).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() 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')) print ctype print pdict 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) return except Exception as e: print e pass
def do_POST(self): try: #handle the create new restaurant if self.path.endswith("/restaurants/create"): 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) newRestaurantName = fields.get('name') # Create new Restaurant Object restaurant = Restaurant(name=newRestaurantName[0]) session.add(restaurant) session.commit() self.displayRestaurants() # handler for renaming restaurant if self.path.endswith("rename"): parts = self.path.split('/') print(parts) print(len(parts)) if len(parts) >= 2: restaurantID = parts[len(parts) - 2] print(restaurantID) 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) newRestaurantName = fields.get('name') restaurant = session.query(Restaurant).filter( Restaurant.id == restaurantID).one() restaurant.name = newRestaurantName[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 # handler for deleting restaurants if self.path.endswith("delete"): parts = self.path.split('/') print(parts) print(len(parts)) if len(parts) >= 2: restaurantID = parts[len(parts) - 2] print(restaurantID) ctype, pdict = cgi.parse_header( self.headers.get('content-type')) pdict['boundary'] = bytes(pdict['boundary'], "utf-8") if ctype == 'multipart/form-data': restaurant = session.query(Restaurant).filter( Restaurant.id == restaurantID).delete() session.commit() self.send_response(301) self.send_header('Content-type', 'text/html') self.send_header('Location', '/restaurants') self.end_headers() return except: pass
def do_POST(self): try: if self.path == '/new': 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('restaurant') # saving the data from the db engine = create_engine('sqlite:///restaurantmenu.db') Base.metadata.bind = engine DBSession = sessionmaker(bind=engine) session = DBSession() restaurant1 = Restaurant(name=messagecontent[0]) session.add(restaurant1) session.commit() session.close() output = "" output += "<html><body>" output += " <h2> Restaurant added: </h2>" output += "<h1> %s </h1>" % messagecontent[0] output += "</body></html>" self.wfile.write(output) if self.path == '/edit': ctype, pdict = cgi.parse_header( self.headers.getheader('content-type')) if ctype == 'multipart/form-data': fields = cgi.parse_multipart(self.rfile, pdict) new_restaurant_name = fields.get('restaurant')[0] restaurant_id = int(fields.get('id')[0]) # getting the data from the db engine = create_engine('sqlite:///restaurantmenu.db') Base.metadata.bind = engine DBSession = sessionmaker(bind=engine) session = DBSession() restaurant1 = session.query(Restaurant).filter_by(id=restaurant_id).first() if restaurant1: restaurant1.name = new_restaurant_name session.add(restaurant1) session.commit() session.close() # redirecting to restaurants list self.send_response(301) self.send_header("Location", "/restaurants") self.end_headers() if self.path == '/delete': ctype, pdict = cgi.parse_header( self.headers.getheader('content-type')) if ctype == 'multipart/form-data': fields = cgi.parse_multipart(self.rfile, pdict) restaurant_id = int(fields.get('id')[0]) # deleteting the restaurant from the db engine = create_engine('sqlite:///restaurantmenu.db') Base.metadata.bind = engine DBSession = sessionmaker(bind=engine) session = DBSession() restaurant1 = session.query(Restaurant).filter_by(id=restaurant_id).first() if restaurant1: session.delete(restaurant1) session.commit() session.close() # redirecting to restaurants list self.send_response(301) 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': 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
def do_POST(self): try: if self.path.endswith("/delete"): restaurant_id = self.path.split("/")[2] restaurant_object = session.query(Restaurant).filter_by( id=restaurant_id).one() if restaurant_object: session.delete(restaurant_object) session.commit() self.send_response(301) self.send_header('content-type', 'text/html') self.send_header('Location', '/restaurants') if self.path.endswith("/hola"): 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='/hola'><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 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('newRestaurant') newRest = Restaurant() newRest.name = messagecontent[0] print newRest.name session.add(newRest) session.commit() print newRest.name 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('updateRestaurant') restaurant_id = self.path.split("/")[2] restaurant_object = session.query(Restaurant).filter_by( id=restaurant_id).one() if restaurant_object != []: restaurant_object.name = messagecontent[0] session.add(restaurant_object) session.commit() self.send_response(301) self.send_header('content-type', 'text/html') self.send_header('Location', '/restaurants') 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: 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("/restaurants/new"): ctype, pdict = cgi.parse_header( self.headers.getheader('Content-type')) if ctype == 'multipart/form-data': fields = cgi.parse_multipart(self.rfile, pdict) newRestaurant = fields.get('newRestaurant') # entry created to be inserted into the database restaurant = Restaurant(name=newRestaurant[0]) # entry added to the session session.add(restaurant) # commit session to update the database 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) restaurantName = fields.get('restaurantName')[0] restaurantId = self.path.split('/')[2] restaurant = session.query(Restaurant).filter_by( id=restaurantId).one() if restaurant != []: restaurant.name = restaurantName # add updated values to the session session.add(restaurant) # commit the session 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] restaurant = session.query(Restaurant).filter_by( id=restaurantId).one() if restaurant != []: # deleting the entry session.delete(restaurant) # commit the session 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: 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
def do_POST(self): try: self.send_response(301) self.send_header('Content-type', 'text/html') self.end_headers() ctype, pdict = cgi.parse_header(self.headers.get('content-type')) pdict['boundary'] = pdict['boundary'].encode('utf-8') if ctype == 'multipart/form-data': fields = cgi.parse_multipart(self.rfile, pdict) output = "" output += "<html><body>" restaurant = None #Handle POST requests from diff forms if self.path.endswith( '/restaurant/new' ): #getvalue returns empty string, a string, or a list of strings depending on use case content = fields.get('new_restaurant_name') restName = content[0].decode('utf-8') restaurant = Restaurant(name=restName) output += "<h1>%s has been added to the list of Restaurants</h1>" % restaurant.name output += "<a href='/restaurant'>Return to the list of restaurants</a>" elif self.path.endswith('/edit'): r_id = self.path.split('/')[2] content = fields.get('edit_restaurant_name') restaurant = self.server.session.query(Restaurant).filter_by( id=r_id).one() newName = content[0].decode('utf-8') #Check new name length and display corresponding message if len(newName) > 0: restaurant = self.server.session.query( Restaurant).filter_by(id=r_id).one() oldName = restaurant.name restaurant.name = newName output += "<h1>%s has been renamed to %s</h1>" % (oldName, newName) else: output += "<h1>Invalid name. Please input a name that has a length greater than 0.</h1>" output += "<a href='/restaurant'>Return to the list of restaurants</a>" elif self.path.endswith('/delete'): r_id = self.path.split('/')[2] restaurant = self.server.session.query(Restaurant).filter_by( id=r_id).one() r_name = restaurant.name self.server.session.delete(restaurant) self.server.session.commit() output += "<h1>%s has been deleted from the list</h1>" % r_name output += "<a href='/restaurant'>Return to the list of restaurants</a>" output += "</html></body>" #maybe belongs in each specific if-else block - in case deadlock. exa: Message shows to client but doesn't push to DB insp = inspect( restaurant ) #inspect state of object since delete commits inside another block of code if restaurant != None and not insp.detached: self.server.session.add(restaurant) self.server.session.commit() print(output) self.wfile.write(output.encode('utf-8')) except Exception as e: print(e)
def do_POST(self): try: # add restaurant if self.path.endswith('/restaurants/new'): ctype, pdict = cgi.parse_header(self.headers['content-type']) pdict['boundary'] = pdict['boundary'].encode() if ctype == 'multipart/form-data': fields = cgi.parse_multipart(self.rfile, pdict) name_content = fields.get('name') name = name_content[0].decode() restaurant = Restaurant(name=name) db.add(restaurant) db.commit() self.default_header(301, redirect='/restaurants') # edit restaurant regex = self.regex['restaurant-edit'] m = re.search(regex, self.path) if m: id_ = m.group('id') ctype, pdict = cgi.parse_header(self.headers['content-type']) pdict['boundary'] = pdict['boundary'].encode() if ctype == 'multipart/form-data': fields = cgi.parse_multipart(self.rfile, pdict) name_content = fields.get('name') name = name_content[0].decode() restaurant = db.query(Restaurant).get(id_) if restaurant is None: raise IOError restaurant.name = name db.add(restaurant) db.commit() self.default_header(301, redirect='/restaurants') # delete restaurant regex = self.regex['restaurant-delete'] m = re.search(regex, self.path) if m: id_ = m.group('id') restaurant = db.query(Restaurant).get(id_) if restaurant is None: raise IOError db.delete(restaurant) db.commit() self.default_header(301, redirect='/restaurants') elif self.path.endswith('/hello'): self.default_header(301) ctype, pdict = cgi.parse_header(self.headers['content-type']) pdict['boundary'] = pdict['boundary'].encode() if ctype == 'multipart/form-data': fields = cgi.parse_multipart(self.rfile, pdict) messagecontent = fields.get('message') message = messagecontent[0].decode() with self.page() as page: page.write("<h2>Okay, how about this?</h2>" f"<h1>{message}</h1>") page.write(self.forms['hello']) except Exception as e: raise
def do_POST(self): if self.path.endswith("/restaurants/new"): try: 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('restaurant') 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() except: pass if self.path.endswith("/edit"): try: 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('restaurant') id_number = self.path.split('/')[2] restaurant = session.query(Restaurant).filter_by( id=id_number).one() restaurant.name = messagecontent[0] session.commit() self.send_response(301) self.send_header('Content-type', 'text/html') self.send_header('Location', '/restaurants') self.end_headers() except: pass if self.path.endswith("/delete"): try: id_number = self.path.split('/')[2] restaurant = session.query(Restaurant).filter_by( id=id_number).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 else: try: 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 self.send_response(301) self.send_header('Content-type', 'text/html') self.end_headers() except: pass