Skip to content

Commit

Permalink
objetive udacity#1 restaurat list
Browse files Browse the repository at this point in the history
list all the restaurant name in the database
  • Loading branch information
carloscorti committed Feb 24, 2020
1 parent 2be68b7 commit 59a83f1
Showing 1 changed file with 20 additions and 33 deletions.
53 changes: 20 additions & 33 deletions webserver.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
from database_setup import Base, Restaurant

from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer
import cgi

Expand All @@ -9,49 +13,32 @@ class webserverHanlder (BaseHTTPRequestHandler):
def do_GET(self):

try:
if self.path.endswith("/hello"):
engine = create_engine('sqlite:///restaurantmenu.db')
Base.metadata.bind = engine
DBSession = sessionmaker(bind = engine)
ses = DBSession()

restoList = ses.query(Restaurant).all()
if self.path.endswith("/restaurant"):
self.send_response(200)
self.send_header('Content-Type', 'text/html')
self.end_headers()

output=""
output+="<html><body>"
output+="<h1>Hello!</h1>"
output+= """
<form method='POST' enctype='multipart/form-data' action='/hello'
<h2>What would you like me to say?</h2>
<input name='message' type='text'>
<input type='submit' value='submit'>
</form>
"""
output+="</body></html>"
self.wfile.write(output)
print output
return

if self.path.endswith("/hola"):
self.send_response(200)
self.send_header('Content-Type', 'text/html')
self.end_headers()

output=""
output+="<html><body>"
output+="""
<h1>&#161Hola!</h1>
<a href='/hello'>Back to Hello</a>
"""
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+="</html></body>"
output+="<h1>Restaurant list</h1>"
output+="<div><ul>"

for resto in restoList:
output+="<li>%s</li><br>" % resto.name

output+="</ul></div>"
output+="</body></html>"
self.wfile.write(output)
print output
return

return

except IOError:
self.send_error(404, "File not Fount %s" % self.path)
Expand Down

0 comments on commit 59a83f1

Please sign in to comment.