def menuItems(restaurant_id):
    #get restaurant object as well
    restaurant = RestaurantQuery.get_restaurant(restaurant_id)
    #get the menu items from the restaurant
    items = RestaurantQuery.get_menu_items(restaurant_id)

    if len(items) == 0:
        flash("No menu items currently")

    return render_template('menu.html', restaurant=restaurant, items=items)
def newRestaurant():
    if request.method == 'POST':
        #get values from form
        name = request.form['name']

        #create the new item
        RestaurantQuery.add_restaurant(name)

        #flash message
        flash("new restaurant added")

        return redirect(url_for('restaurants'))
    else:
        return render_template('newRestaurant.html', restaurant_id=restaurant_id)
def deleteRestaurant(restaurant_id):
    #item to delete
    deletedRestaurant = RestaurantQuery.get_restaurant(restaurant_id)

    #if posted too, process the form
    #else just display the page and it's form
    if request.method == 'POST':
        RestaurantQuery.delete_restaurant(deletedRestaurant.id)

         #flash message
        flash("Successfully deleted "+deletedRestaurant.name)

        return redirect(url_for('restaurants'))
    else:
        return render_template('deleteRestaurant.html', restaurant_id=restaurant_id, item=deletedRestaurant)
def deleteMenuItem(restaurant_id, menu_id):
    #item to delete
    deletedItem = RestaurantQuery.get_menu_item(menu_id)

    #if posted too, process the form
    #else just display the page and it's form
    if request.method == 'POST':
        RestaurantQuery.delete_menu_item(deletedItem.id)

         #flash message
        flash("Successfully deleted "+deletedItem.name+" from the menu")

        return redirect(url_for('menuItems', restaurant_id=restaurant_id))
    else:
        return render_template('deleteMenuItem.html', restaurant_id=restaurant_id, menu_id=menu_id, item=deletedItem)
def editRestaurant(restaurant_id):
    #item to edit
    editedRestaurant = RestaurantQuery.get_restaurant(restaurant_id)
    #if posted too, process the form
    #else just display the page and it's form
    if request.method == 'POST':
        if request.form['name']:
            editedRestaurant.name = request.form['name']
        RestaurantQuery.edit_restaurant(editedRestaurant.name, editedRestaurant.id)

        #flash message
        flash("Updated "+editedRestaurant.name)

        return redirect(url_for('restaurants'))
    else:
        
        return render_template('editRestaurant.html', restaurant_id=restaurant_id, item=editedRestaurant)
def editMenuItem(restaurant_id, menu_id):
    #item to edit
    editedItem = RestaurantQuery.get_menu_item(menu_id)
    #if posted too, process the form
    #else just display the page and it's form
    if request.method == 'POST':
        if request.form['name']:
            editedItem.name = request.form['name']
        RestaurantQuery.edit_menu_item(editedItem.name, editedItem.id)

        #flash message
        flash("Updated "+editedItem.name)

        return redirect(url_for('menuItems', restaurant_id=restaurant_id))
    else:
        
        return render_template('editMenuItem.html', restaurant_id=restaurant_id, menu_id=menu_id, item=editedItem)
def newMenuItem(restaurant_id):

    if request.method == 'POST':
        #get values from form
        name=request.form['name']
        description=request.form['description']
        price=request.form['price']
        course=request.form['course']
        restaurant_id=restaurant_id

        #create the new item
        RestaurantQuery.add_menu_item(name, description, price, course, restaurant_id)

        #flash message
        flash("Successfully added "+name+" to the menu")

        return redirect(url_for('menuItems', restaurant_id=restaurant_id))
    else:
        return render_template('newMenuItem.html', restaurant_id=restaurant_id)
def restaurants():

    #get the list of restaurants.
    restaurants = RestaurantQuery.get_restaurants()

    return render_template('restaurants.html', restaurants=restaurants)
def restaurantMenuItemJSON(restaurant_id, menu_id):
    #get the menu items from the restaurant
    item = RestaurantQuery.get_menu_item(menu_id)

    return jsonify(MenuItem=[item.serialize])
def restaurantMenuJSON(restaurant_id):
    #get the menu items from the restaurant
    items = RestaurantQuery.get_menu_items(restaurant_id)

    return jsonify(MenuItems=[i.serialize for i in items])
	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)