Instantly share code, notes, and snippets.

Embed
What would you like to do?
application: kebab-sub
version: 2
runtime: python27
threadsafe: no
api_version: 1
handlers:
- url: /static
static_dir: static
- url: /.*
script: main.py
indexes:
# AUTOGENERATED
# This index.yaml is automatically updated whenever the dev_appserver
# detects that a new type of query is run. If you want to manage the
# index.yaml file manually, remove the above marker line (the line
# saying "# AUTOGENERATED"). If you want to manage some indexes
# manually, move them above the marker line. The index.yaml file is
# automatically uploaded to the admin console when you next deploy
# your application using appcfg.py.
- kind: OrderEntity
properties:
- name: accomplished
- name: date
- kind: OrderEntity
properties:
- name: accomplished
- name: date
direction: desc
import cgi
import datetime
import time
from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app
from google.appengine.ext import db
headText = """
<style type='text/css'>
body { font-size:xx-large;}
table { text-align:center; vertical-align:middle; }
button { font-size:xx-large; }
input { font-size:xx-large; }
select { font-size:2em; }
td { width:3em; font-size:3em; }
p { border-style : dotted; border-width : 1px; }
div#order{ text-align:center }
p#seat{ color:SkyBlue }
p#hot { color: red; }
p#normal{ color: orange; }
#seat_{ text-align:center; background-color:SkyBlue; font-size:6em; width:200px;}
#box{}
#hot_{ text-align:center; background-color:orangered; font-size:3em; width:100px; float:left}
#normal_{ text-align:center; background-color:yellow; font-size:3em; width:100px; float:left}
#clear{ clear:both }
</style>
"""
def genCheckTable(OrderList):
tableText = "<table border=1>\n"
for elem in OrderList:
jpn_date = elem.date + datetime.timedelta(hours=9)
tableText += "<tr>"
if elem.accomplished:
tableText += "<td bgcolor='black' style='width:100px'></td>"
else:
tableText += "<td style='width:100px'>"
tableText += "<td bgcolor='SkyBlue' style='width:100px'>" + str(elem.seat) + "</td>"
tableText += "<td bgcolor='orangered' style='width:100px'>" + str(elem.hot) + "</td>"
tableText += "<td bgcolor='yellow' style='width:100px'>" + str(elem.normal) + "</td>"
tableText += "<td style='font-size:xx-large'>"+ jpn_date.strftime('%X') +"</td>"
tableText += "<td style='font-size:xx-large'>"+ elem.date.strftime("%x %X") + "</td>"
tableText += "</tr>\n"
tableText += "</table>"
return tableText
def genSelectElement(name, valueList, n=False):
if n == False:
text = """
<p id='""" + name + """'>
Select """ + name + """<br>
<select name='""" + name + "'>\n"
for value in valueList:
text += " <option value='" + str(value) +"'>" + str(value) + "</option>\n"
text += """ </select>
</p>"""
return text
else :
text = """<p id='""" + name + """'> Select """ + name + """<br><select name='""" + name + "'>"
for value in valueList:
text += "<option value='" + str(value) +"'>" + str(value) + "</option>"
text += """</select></p>"""
return text
def genTable(ValueList,button=False,button_name=None):
tableText = "<table border=1>\n"
tableText += "<tr bgcolor='SkyBlue'>"
for elem in ValueList:
tableText += "<td>" + elem.seat + "</td>"
tableText += "</tr>\n"
tableText += "<tr bgcolor='orangered'>"
for elem in ValueList:
tableText += "<td>" + str(elem.hot) + "</td>"
tableText += "</tr>\n"
tableText += "<tr bgcolor='yellow'>"
for elem in ValueList:
tableText += "<td>" + str(elem.normal) + "</td>"
tableText += "</tr>\n"
if button_name == None:
if button == True:
tableText += "<tr>"
for elem in ValueList:
tableText += "<td><button type='button' onClick='confirmation(" + str(elem.key().id()) + ")'>Del</button></td>"
tableText += "</tr>\n"
else :
tableText += "<tr>"
for elem in ValueList:
tableText += "<td><button type='button' onClick='confirmation(" + str(elem.key().id()) + ")'>Cancel</button></td>"
tableText += "</tr>\n"
tableText += "</table>\n"
return tableText
class OrderEntity(db.Model):
date = db.DateTimeProperty()
seat = db.StringProperty()
hot = db.IntegerProperty()
normal = db.IntegerProperty()
accomplished = db.BooleanProperty()
class HandleDataStore(webapp.RequestHandler):
def post(self):
if self.request.get('type') == 'order':
hot = int(self.request.get('hot'))
normal = int(self.request.get('normal'))
seat = self.request.get('seat')
o = OrderEntity()
o.date = datetime.datetime.now()
o.seat = seat
o.hot = hot
o.normal = normal
o.accomplished = False
o.put()
self.response.out.write("""
<!DOCTYPE html>
<html>
<head>
<title>Redirecting...</title>
<script type='text/javascript'>
function Redirect(){
location.href="/order"
}
</script>
</head>
<body onLoad='javascript:setTimeout("Redirect()",500)'>
You request has been sent!<br>
redirecting ...
</body>
</html>
""")
elif self.request.get('type') == 'accomplished':
q = OrderEntity.get_by_id(long(self.request.get('id')))
q.accomplished = True
q.put()
self.response.out.write("""
<!DOCTYPE html>
<html>
<head>
<title>Redirecting...</title>
<script type='text/javascript'>
function Redirect(){
location.href="/del"
}
</script>
</head>
<body onLoad='javascript:setTimeout("Redirect()",500)'>
You request has been sent!<br>
redirecting ...
</body>
</html>
""")
elif self.request.get('type') == 'cancel':
q = OrderEntity.get_by_id(long(self.request.get('id')))
q.accomplished = False
q.put()
self.response.out.write("""
<!DOCTYPE html>
<html>
<head>
<title>Redirecting...</title>
<script type='text/javascript'>
function Redirect(){
location.href="/del"
}
</script>
</head>
<body onLoad='javascript:setTimeout("Redirect()",500)'>
You request has been sent!<br>
redirecting ...
</body>
</html>
""")
class OrderPage(webapp.RequestHandler):
def get(self):
alphabetTable = genSelectElement("seat" , [""] + range(1,21), n=True)
htmlText = """<!DOCTYPE html>
<html>
<head>""" + headText +"""
<title>Order</title>
<script type='text/javascript'>
function send(){
var form = document.selectForm;
form.submit();
}
function eliminate(){
var enc = document.getElementById("enclosure");
var bod = document.body;
bod.removeChild(enc);
var order = document.getElementById("order");
order.style.display = "inline";
}
function confirmHTML() {
var order = document.getElementById("order");
order.style.display = "none";
var form = document.selectForm;
var enclosure = document.createElement("div");
enclosure.id = "enclosure";
var seat = document.createElement("div");
seat.id = "seat_";
seat.innerHTML = form.seat.options[form.seat.selectedIndex].text;
var box = document.createElement("div");
box.id = "box";
var hot = document.createElement("div");
hot.innerHTML = form.hot.options[form.hot.selectedIndex].text;
hot.id = "hot_";
var normal = document.createElement("div");
normal.innerHTML = form.normal.options[form.normal.selectedIndex].text;
normal.id = "normal_";
var sendButton = document.createElement("button");
sendButton.innerHTML = "Correct";
sendButton.onclick = send;
var returnButton = document.createElement("button");
returnButton.innerHTML = "Wrong";
returnButton.onclick = eliminate;
var clear = document.createElement("div");
clear.id = "clear";
box.appendChild(hot);
box.appendChild(normal);
enclosure.appendChild(seat);
enclosure.appendChild(box);
enclosure.appendChild(clear);
enclosure.appendChild(sendButton);
enclosure.appendChild(returnButton);
document.body.appendChild(enclosure);
}
</script>
</head>
<body>
<div id="order">
<form name='selectForm' action="/put" method="post">
<input type='hidden' name='type' value='order'>
"""
htmlText += "<div id='seat_selection'>\n"
htmlText += alphabetTable
htmlText += "</div>\n"
htmlText += genSelectElement("hot" , range(0,6))
htmlText += genSelectElement("normal", range(0,6))
htmlText += """
</form>
<button onclick='confirmHTML()'>Order</button>
</div>
</body>
</html>"""
self.response.out.write(htmlText)
class ViewPage(webapp.RequestHandler):
def get(self):
q = OrderEntity.all()
q.filter('accomplished =', False)
q.order('date')
result = q.fetch(limit=6) # more ?
htmlText = """
<!DOCTYPE html>
<html>
<head>""" + headText +"""
<title>View order</title>
<META HTTP-EQUIV='Refresh' CONTENT='10'>
</head>
<body>
"""
htmlText += genTable(result)
htmlText += """
</body>
</html>
"""
self.response.out.write(htmlText)
class DeletePage(webapp.RequestHandler):
def get(self):
q = OrderEntity.all()
q.filter('accomplished =', False)
q.order('date')
result = q.fetch(limit=6) # more ?
listText = "{"
for elem in result:
listText += "'" + str(elem.key().id()) + "':['" + elem.seat + "'," + str(elem.hot) + "," + str(elem.normal) + "],\n"
listText += "}"
htmlText = """
<!DOCTYPE html>
<html>
<head>""" + headText +"""
<title>Delete</title>
<script type="text/javascript">
var orderList = """ + listText +""";
function Cancel(){
location.href = '/can'
}
function ReLoad(){
window.location.reload();
}
function eliminate(){
var enc = document.getElementById("enclosure");
var bod = document.body;
bod.removeChild(enc);
var order = document.getElementById("table");
order.style.display = "inline";
}
confirmation = function(id){
var tableDiv = document.getElementById("table");
tableDiv.style.display = "none";
var enclosure = document.createElement("div");
enclosure.id = "enclosure";
var seat = document.createElement("div");
seat.id = "seat_";
seat.innerHTML = orderList[id][0];
var box = document.createElement("div");
box.id = "box";
var hot = document.createElement("div");
hot.innerHTML = orderList[id][1];
hot.id = "hot_";
var normal = document.createElement("div");
normal.innerHTML = orderList[id][2];
normal.id = "normal_";
var sendButton = document.createElement("button");
sendButton.innerHTML = "Correct";
sendButton.onclick = function(){send(id)};
var returnButton = document.createElement("button");
returnButton.innerHTML = "Wrong";
returnButton.onclick = eliminate;
var clear = document.createElement("div");
clear.id = "clear";
box.appendChild(hot);
box.appendChild(normal);
enclosure.appendChild(seat);
enclosure.appendChild(box);
enclosure.appendChild(clear);
enclosure.appendChild(sendButton);
enclosure.appendChild(returnButton);
document.body.appendChild(enclosure);
}
send = function(id){
var form = document.createElement('form');
document.body.appendChild( form );
var input = document.createElement( 'input' );
input.setAttribute( 'type' , 'hidden' );
input.setAttribute( 'name' , 'id' );
input.setAttribute( 'value' , id );
form.appendChild( input );
var input_ = document.createElement('input');
input_.setAttribute("type","hidden");
input_.setAttribute("name","type");
input_.setAttribute("value","accomplished");
form.appendChild(input_);
form.setAttribute( 'action' , '/put' );
form.setAttribute( 'method' , 'post' );
form.submit();
return false;
}
</script>
</head>
<body>
<div id='table'>
<button onclick='ReLoad()'>Reload</button>
<button onclick='Cancel()'>Cancel</button>"""
htmlText += genTable(result,button=True)
htmlText += """
</div>
</body>
</html>
"""
self.response.out.write(htmlText)
class CancelPage(webapp.RequestHandler):
def get(self):
q = OrderEntity.all()
q.filter('accomplished =', True)
q.order('-date')
result = q.fetch(limit=10) # more ?
listText = "{"
for elem in result:
listText += "'" + str(elem.key().id()) + "':['" + elem.seat + "'," + str(elem.hot) + "," + str(elem.normal) + "],\n"
listText += "}"
htmlText = """
<!DOCTYPE html>
<html>
<head>""" + headText +"""
<title>Restore</title>
<script type="text/javascript">
var orderList = """ + listText +""";
function ReLoad(){
window.location.reload();
}
function Delete(){
location.href = '/del';
}
function eliminate(){
var enc = document.getElementById("enclosure");
var bod = document.body;
bod.removeChild(enc);
var order = document.getElementById("table");
order.style.display = "inline";
}
confirmation = function(id){
var tableDiv = document.getElementById("table");
tableDiv.style.display = "none";
var enclosure = document.createElement("div");
enclosure.id = "enclosure";
var seat = document.createElement("div");
seat.id = "seat_";
seat.innerHTML = orderList[id][0];
var box = document.createElement("div");
box.id = "box";
var hot = document.createElement("div");
hot.innerHTML = orderList[id][1];
hot.id = "hot_";
var normal = document.createElement("div");
normal.innerHTML = orderList[id][2];
normal.id = "normal_";
var sendButton = document.createElement("button");
sendButton.innerHTML = "Correct";
sendButton.onclick = function(){send(id)};
var returnButton = document.createElement("button");
returnButton.innerHTML = "Wrong";
returnButton.onclick = eliminate;
var clear = document.createElement("div");
clear.id = "clear";
box.appendChild(hot);
box.appendChild(normal);
enclosure.appendChild(seat);
enclosure.appendChild(box);
enclosure.appendChild(clear);
enclosure.appendChild(sendButton);
enclosure.appendChild(returnButton);
document.body.appendChild(enclosure);
}
send = function(id){
var form = document.createElement('form');
document.body.appendChild( form );
var input = document.createElement( 'input' );
input.setAttribute( 'type' , 'hidden' );
input.setAttribute( 'name' , 'id' );
input.setAttribute( 'value' , id );
form.appendChild( input );
var input_ = document.createElement('input');
input_.setAttribute("type","hidden");
input_.setAttribute("name","type");
input_.setAttribute("value","cancel");
form.appendChild(input_);
form.setAttribute( 'action' , '/put' );
form.setAttribute( 'method' , 'post' );
form.submit();
return false;
}
</script>
</head>
<body>
<div id='table'>
<button onclick='ReLoad()'>Reload</button><button onClick='Delete()'>Delete Page</button>"""
htmlText += genTable(result,button=True,button_name='Cancel')
htmlText += """
</div>
</body>
</html>
"""
self.response.out.write(htmlText)
class CheckPage(webapp.RequestHandler):
def get(self):
q = OrderEntity.all()
q.order('-date')
#result = q.fetch(limit=40)
result = q
tableText = genCheckTable(result)
htmlText = """<!DOCTYPE html>
<html>
<head>
""" + headText + """
<script>
function ReLoad(){
window.location.reload();
}
</script>
</head>
<body>
<button onclick='ReLoad()'>Reload</button>
""" + tableText + """
</body>
</html>
"""
self.response.out.write(htmlText)
class StaticsPage(webapp.RequestHandler):
def get(self):
q = OrderEntity.all()
q.filter("date >=", datetime.datetime(2013,9,8,0,0,0))
q.order('date')
sumListText = "['sum',"
dt = datetime.datetime(2013,9,8,0,0,0)
td = datetime.timedelta(minutes=10)
count = 0
for elem in q:
if dt <= elem.date and elem.date < dt + td:
count += elem.hot + elem.normal
else:
sumListText += str(count) + ","
count = elem.hot + elem.normal
dt += td
sumListText += str(count) + ']'
dt = datetime.datetime(2013,9,8,0,0,0)
td = datetime.timedelta(minutes=10)
count = 0
hotListText = "['hot',"
for elem in q:
if dt <= elem.date and elem.date < dt + td:
count += elem.hot
else:
hotListText += str(count) + ","
count = elem.hot
dt += td
hotListText += str(count) + "]"
dt = datetime.datetime(2013,9,8,0,0,0)
td = datetime.timedelta(minutes=10)
count = 0
normalListText = "['normal',"
for elem in q:
if dt <= elem.date and elem.date < dt + td:
count += elem.normal
else:
normalListText += str(count) + ","
count = elem.normal
dt += td
normalListText += str(count) + "]"
htmlText = """<!DOCTYPE html>
<html>
<head>
<!--[if IE]><script type="text/javascript" src="/static/html5jp/excanvas/excanvas.js"></script><![endif]-->
<script type="text/javascript" src="/static/html5jp/graph/line.js"></script>
<script type="text/javascript">
window.onload = function() {
var lg = new html5jp.graph.line("sample");
var items = [""" + sumListText + """,""" + hotListText +""",""" + normalListText + """];
lg.draw(items);
var kg = new html5jp.graph.line("hot");
var hots = [items[1]];
kg.draw(hots);
var mg = new html5jp.graph.line("normal");
var normals = [items[2]];
mg.draw(normals);
}
</script>
</head>
<body>
<div><canvas width="800" height="300" id="sample"></canvas></div>
<div><canvas width="800" height="300" id="hot"></canvas></div>
<div><canvas width="800" height="300" id="normal"></canvas></div>
</body>
</html>"""
self.response.out.write(htmlText)
application = webapp.WSGIApplication(
[('/order', OrderPage),('/put',HandleDataStore),('/view', ViewPage),('/del',DeletePage),('/can',CancelPage),('/check',CheckPage),('/statics',StaticsPage)],
debug=True)
def main():
run_wsgi_app(application)
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment