Skip to content

Instantly share code, notes, and snippets.

@DiegoTc
Created October 6, 2015 11:13
Show Gist options
  • Select an option

  • Save DiegoTc/6c4375c412b0e1ab758f to your computer and use it in GitHub Desktop.

Select an option

Save DiegoTc/6c4375c412b0e1ab758f to your computer and use it in GitHub Desktop.
import time
from datetime import date
from sqlalchemy import create_engine, func,select, asc, desc, Table, Column, Integer, String, MetaData, ForeignKey
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()
conn = engine.connect()
def getRestaurants():
return session.query(Restaurant).all()
import time
from datetime import date
from sqlalchemy import create_engine, func,select, asc, desc, Table, Column, Integer, String, MetaData, ForeignKey
from sqlalchemy.orm import sessionmaker
from puppies import Base, Shelter, Puppy
engine = create_engine('sqlite:///puppyshelter.db')
Base.metadata.bind = engine
DBSession = sessionmaker(bind=engine)
session = DBSession()
conn = engine.connect()
def firstExercise():
s = select([Puppy]).order_by(asc("name"))
results = []
for i in conn.execute(s) :
val = i["name"]
results.append(val)
return results
def secondExercise():
today = date.today().strftime("%Y/%m/%d")
sixthmonth = date(date.today().year, date.today().month-6,date.today().day).strftime("%Y/%m/%d")
for instance in session.query(Puppy.name, Puppy.weight, Puppy.dateOfBirth).\
filter(Puppy.dateOfBirth <= today, Puppy.dateOfBirth >= sixthmonth ).order_by(desc("dateOfBirth")):
print instance
def thirdExercise():
for instance in session.query(Puppy.name, Puppy.weight).order_by(desc("weight")):
print instance
def fourthExercise():
print session.query(func.count(Puppy.shelter_id)).group_by(Puppy.shelter_id).all
#for i in firstExercise() :
# print i
secondExercise()
from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer
import cgi
import database_logic
class WebServerHandler(BaseHTTPRequestHandler):
def do_GET(self):
firsthtml = "<html><body>"
lasthtml = "</body></html>"
if self.path.endswith("/hello"):
self.send_response(200)
self.send_header('Content-type', 'text/html')
self.end_headers()
message = ""
message += "<html><body>Hello!"
message += '''<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>'''
message += '</body></html>'
self.wfile.write(message)
print message
return
if self.path.endswith("/"):
self.send_response(200)
self.send_header('Content-type', 'text/html')
self.end_headers()
message = ""
message += "<html><h1>Welcome to the first page</h1>"
message += '''<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>'''
message += '</html>'
self.wfile.write(message)
print message
return
if self.path.endswith("/hola"):
self.send_response(200)
self.send_header('Content-type', 'text/html')
self.end_headers()
message = ""
message += "<html><body>Hola!<a href='/hello'>Back to Hello</a>"
message += '''<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>'''
message += '</body></html>'
self.wfile.write(message)
print message
return
if self.path.endswith("/restaurants"):
self.send_response(200)
self.send_header('Content-type', 'text/html')
self.end_headers()
message = ""
message += firsthtml + "<h1>Restaurants </h1>"
value = database_logic.getRestaurants()
for rest in value:
message += rest.name
message += "<br><br>"
message += lasthtml
self.wfile.write(message)
print message
return
else:
self.send_error(404, 'File Not Found: %s' % self.path)
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('message')
output = ""
output += "<html><body><h2>OK, 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
return
except:
pass
def main():
try:
port = 8080
server = HTTPServer(('', port), WebServerHandler)
print "Web Server running on port %s" % port
server.serve_forever()
except KeyboardInterrupt:
print " ^C entered, stopping web server...."
server.socket.close()
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment