def do_POST(self): try: #successful response self.send_response(301) self.end_headers() #read our data sent in. ctype, pdict = cgi.parse_header(self.headers.getheader('content-type')) #check for form data if ctype == 'multipart/form-data': #store the fields sent in fields = cgi.parse_multipart(self.rfile, pdict) try: name = fields.get('restaurant') except: pass #hidden field telling us which form is being sent operation = fields.get('operation') sucess_response = '' #determine our query based on which form was sent if operation[0] == 'add': #add our restaurant RestaurantQuery.add_restaurant(name[0]) sucess_response = "<h1>successfully added new restaurant %s</h1>" % name[0] if operation[0] == 'edit': #get our id value, we stored it in a hidden field rID = fields.get('rID') #update our restaurant RestaurantQuery.edit_restaurant(int(rID[0]), name[0]) sucess_response = "<h1>successfully edited %s</h1>" % name[0] if operation[0] == 'delete': #get our id value, we stored it in a hidden field rID = fields.get('rID') #delete our restaurant rName = RestaurantQuery.delete_restaurant(int(rID[0])) sucess_response = "<h1>successfully deleted %s</h1>" % rName #output the response output = "" output += "<html><body>" output += sucess_response #link to make new restaurants output += "<a href='/restaurants/new'>Click here to add a new restaurant</a>" #create another query instance, now we'll get #our new restaurant in the list restaurants = RestaurantQuery.get_items(Restaurant) #append our query results to the message output += restaurants output += "</body></html>" self.wfile.write(output) print output return except: pass
def do_GET(self): #check the url being requested #and if it's a url we're expecting #return a a response if self.path.endswith("/restaurants"): #successful response code self.send_response(200) #headers to send back self.send_header('Content-type', 'text/html') self.end_headers() #begin our response message for the user message = "" message += "<html><body>" #link to make new restaurants message += "<a href='/restaurants/new'>Click here to add a new restaurant</a>" #create an instance of our query #class and get back the requested data #from our database. restaurants = RestaurantQuery.get_items(Restaurant) #append our query results to the message message += restaurants #close our message message += "</body></html>" #writeout the server response self.wfile.write(message) #print message print message return #get our /restaurant/new page if self.path.endswith("/restaurants/new"): #successful response code self.send_response(200) #headers to send back self.send_header('Content-type', 'text/html') self.end_headers() #begin our response message for the user message = "" message += "<html><body>" #add our form message += "<form method='POST', enctype='multipart/form-data' action='/restaurants'><h2>Add a new restaurant</h2>" message += "<input type='text' name='restaurant'><input type='hidden' name='operation' value='add'><input type='submit' value='submit'></form>" message += "</body></html>" #close our message message += "</body></html>" #writeout the server response self.wfile.write(message) #print message print message return #since the url is dependent on the id we #set up a small regex for the url to check. #if it is a match present the page if re.compile(r'/restaurants/([0-9]+)/edit').search(self.path): #we have a matching url so no another #regex is perfomed to extract the ID of the restaurant rID = re.search(r'[0-9]+', self.path).group() #successful response code self.send_response(200) #headers to send back self.send_header('Content-type', 'text/html') self.end_headers() #begin our response message for the user message = "" message += "<html><body>" #add our form message += "<form method='POST', enctype='multipart/form-data' action='/restaurants'><h2>Edit a restaurant?</h2>" message += "<input type='text' name='restaurant'><input type='hidden' name='operation' value='edit'>" message += "<input type='hidden' name='rID' value='"+rID+"'><input type='submit' value='submit'></form>" message += "</body></html>" #close our message message += "</body></html>" #writeout the server response self.wfile.write(message) #print message print message return #since the url is dependent on the id we #set up a small regex for the url to check. #if it is a match present the page if re.compile(r'/restaurants/([0-9]+)/delete').search(self.path): #we have a matching url so no another #regex is perfomed to extract the ID of the restaurant rID = re.search(r'[0-9]+', self.path).group() #successful response code self.send_response(200) #headers to send back self.send_header('Content-type', 'text/html') self.end_headers() #begin our response message for the user message = "" message += "<html><body>" #get the name of the restaurant #being operated on rName = RestaurantQuery.get_restaurant(int(rID), 'name') #add our form message += "<form method='POST', enctype='multipart/form-data' action='/restaurants'><h2>Are you sure you want to delete "+rName+"?</h2>" message += "<input type='hidden' name='operation' value='delete'>" message += "<input type='hidden' name='rID' value='"+rID+"'><input type='submit' value='delete'></form>" message += "</body></html>" #close our message message += "</body></html>" #writeout the server response self.wfile.write(message) #print message print message return else: self.send_error(404, 'File Not Found: %s' % self.path)