-
Notifications
You must be signed in to change notification settings - Fork 0
/
webserver2.py
141 lines (125 loc) · 5.53 KB
/
webserver2.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
import locale
import json
import tornado.ioloop
import tornado.web
from requestManager import RequestManager
from matlab import engine
ERROR_MESSAGE = "Fuck. This wasn't supposed to happen."
eng = engine.start_matlab()
eng.cd('matlab')
class MainHandler(tornado.web.RequestHandler):
def get(self):
data = None
with open("hold.html", "r") as htmlfile:
data = htmlfile.read().replace("\n", "")
data = data if data is not None else ERROR_MESSAGE
self.set_cookie("digitATM", "")
self.write(data)
class DebugHandler(tornado.web.RequestHandler):
def get(self):
data = None
with open("debug.html", "r") as htmlfile:
data = htmlfile.read().replace("\n", "")
data = data if data is not None else ERROR_MESSAGE
self.write(data)
class TryAgainHandler(tornado.web.RequestHandler):
def get(self):
data = None
with open("tryagain.html", "r") as htmlfile:
data = htmlfile.read().replace("\n", "")
data = data if data is not None else ERROR_MESSAGE
self.write(data)
class NoMatchHandler(tornado.web.RequestHandler):
def get(self):
data = None
with open("nomatch.html", "r") as htmlfile:
data = htmlfile.read().replace("\n", "")
data = data if data is not None else ERROR_MESSAGE
self.write(data)
class MenuHandler(tornado.web.RequestHandler):
def get(self):
# reroute depending on success of matlab call
data = None
cookieID = self.get_cookie("digitATM")
if (cookieID == ""):
printID = eng.print_match()
if printID == -1: # nothing there. try again.
self.redirect(r"/tryagain")
return
elif printID == 0: # no match
self.redirect(r"/nomatch")
return
else:
# convert to user ID and store it in the cookie
# you have no idea how pathetic this practice is
printID = str(int(printID))
jsondata = None
with open("accounts.json", "r") as jsonfile:
jsondata = json.load(jsonfile)
customerID = None
if jsondata is not None and printID in jsondata:
customerID = jsondata[printID]
else:
self.write("Print ID {0} is not a valid customer ID.".format(printID))
return
self.set_cookie("digitATM", customerID)
with open("menu.html", "r") as htmlfile:
data = htmlfile.read().replace("\n", "")
data = data if data is not None else ERROR_MESSAGE
self.write(data)
class BalanceHandler(tornado.web.RequestHandler):
def get(self):
customerID = self.get_cookie("digitATM")
if customerID is not None:
balance = RequestManager.getBalance(customerID)
else:
self.write("An error occurred when attempting to get the hash value for this customer.".format(customerID))
return
if balance is not None:
self.write("<span style=\"text-align:center\">Customer ID: {0}<br>Balance: {1}<br>Return to <a href=\"/menu\"> the menu</a>.</span>".format(customerID, balance))
return
self.write("Customer ID {0} and its corresponding hash exist. However, the balance for this customer could not be determined.".format(customerID))
class WithdrawHandler(tornado.web.RequestHandler):
def post(self):
customerID = self.get_cookie("digitATM")
amount = self.get_argument("amount", "")
if customerID is not None:
RequestManager.withdraw(customerID, amount)
self.write("<span style=\"text-align:center\">Withdrew {0} from customer ID {1}'s account. Return to <a href=\"/menu\"> the menu</a>.</span>".format(locale.currency(float(amount), grouping=True), customerID))
else:
self.write("Although customer ID {0} exists, an error occurred when attempting to get the hash value for customer ID {0}.".format(customerID))
return
class DepositHandler(tornado.web.RequestHandler):
def post(self):
customerID = self.get_cookie("digitATM")
amount = self.get_argument("amount", "")
if customerID is not None:
RequestManager.withdraw(customerID, amount)
self.write("<span style=\"text-align:center\">Deposited {0} in customer ID {1}'s account. Return to <a href=\"/menu\"> the menu</a>.</span>".format(locale.currency(float(amount), grouping=True), customerID))
else:
self.write("Although customer ID {0} exists, an error occurred when attempting to get the hash value for customer ID {0}.".format(customerID))
return
class StyleHandler(tornado.web.RequestHandler):
def get(self):
data = None
with open("styles.css", "r") as htmlfile:
data = htmlfile.read() #.replace("\n", "")
data = data if data is not None else ERROR_MESSAGE
self.write(data)
def my_app():
return tornado.web.Application([
(r"/", MainHandler),
(r"/menu", MenuHandler),
(r"/debug", DebugHandler),
(r"/tryagain", TryAgainHandler),
(r"/nomatch", NoMatchHandler),
(r"/balance", BalanceHandler),
(r"/withdraw", WithdrawHandler),
(r"/deposit", DepositHandler),
(r"/styles.css", StyleHandler),
])
if __name__ == "__main__":
locale.setlocale( locale.LC_ALL, '' )
app = my_app()
app.listen(8888)
tornado.ioloop.IOLoop.current().start()