Skip to content

Instantly share code, notes, and snippets.

@jblocksom
Created February 13, 2014 20:06
Show Gist options
  • Save jblocksom/8982808 to your computer and use it in GitHub Desktop.
Save jblocksom/8982808 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
#
# Copyright 2007 Google Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
import webapp2
import time
import model
from google.appengine.ext import db
ITEMS = """[
{
"name": "Hat",
"basePrice": "8",
"image" : "http://bnr-fruititems.appspot.com/images/beerhat.jpg",
"customizations" : {
"text" : "",
"logo" : ""
}
},
{
"name": "T-Shirt",
"basePrice": "15",
"image" : "http://bnr-fruititems.appspot.com/images/tshirt.jpg",
"customizations" : {
"text" : "What a hack",
"logo" : "sheesh",
"color" : ["#000000", "#ff0000", "#00ff00", "#0000ff"],
"sizes" : ["Small", "Medium", "Large", "X-Large", "2X-Large"]
}
},
{
"name": "Underwear",
"image" : "http://bnr-fruititems.appspot.com/images/boxers.jpg",
"basePrice": "6",
"customizations" : {
"text" : "",
"color" : [ "#000000", "#ff0000" ],
"sizes" : [ "Boys", "Small", "Medium" ]
}
},
]"""
HALLOWEEN = """[
{
"name": "Pumpkin",
"customizations" : {
"sizes" : ["Small", "Medium", "Large", "X-Large", "2X-Large"]
}
}
]"""
HISTORY_TEMPLATE = """<html>
<body>
</body>
</html>
"""
STATUS_TEMPLATE = """<html>
<head>
<title>Order Status</title>
<body>
<strong>Account %s<br/>
Order %s:<br/>
</strong>
<pre>
%s
</pre>
</body>
</html>
"""
class StatusHandler(webapp2.RequestHandler):
def get(self):
order_id = self.request.get('order_id')
# q = model.Order.all()
# q.filter(key=order_id)
# o = q.fetch(1)[0]
o = db.get(order_id)
self.response.out.write(STATUS_TEMPLATE % (o.account, o.key(), o.order_body))
class HistoryHandler(webapp2.RequestHandler):
def get(self):
self.response.out.write('History here')
q = model.Order.all()
account = self.request.get('acct')
if account:
q.filter("account = ", account)
results = q.fetch(10)
self.response.out.write('%d results' % len(results))
for o in results:
self.response.out.write('ACCOUNT %s, ORDER %s, KEY %s\n' % (o.account, o.order_body, o.key()))
self.response.out.write(' -- done')
class OrderHandler(webapp2.RequestHandler):
def get(self):
self.response.out.write('{ "result": "Error", "error" : "Request should be POST" }')
# print self.request.body
# self.response.out.write("ordered: %s" % self.request.get('foo'))
# self.response.out.write("you wanted: " + self.request.body)
def post(self):
try:
account = self.request.get('account')
except:
account = ''
if not account:
self.response.out.write('{ "result": "Error", "error" : "Account not specified", "account" : "%s"}' % account)
return
try:
items = self.request.get('items')
print items
except:
items = ''
if not items:
self.response.out.write('{ "result": "Error", "error" : "No items" }')
return
if account not in ['TZ123', 'TC456', 'JON']:
# Invalid account
self.response.out.write('{ "result": "Error", "error" : "Account %s is not valid" }' % account)
return
order = model.Order(account=account, order_body=items)
k = order.put()
# Account is valid
status_url = 'http://bnr-fruititems.appspot.com/status?order_id=%s' % k
self.response.out.write('{ "result": "OK", "status": "%s"}' % status_url)
class MainHandler(webapp2.RequestHandler):
def get(self):
# time.sleep(2)
self.response.out.write(ITEMS)
app = webapp2.WSGIApplication([
('/', MainHandler),
('/order', OrderHandler),
('/status', StatusHandler),
('/history', HistoryHandler)
],
debug=True)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment