-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.py
309 lines (252 loc) · 10.5 KB
/
server.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
import tornado.auth
import tornado.httpserver
import tornado.ioloop
import tornado.options
import tornado.database
import tornado.web
import os.path
from tornado.options import define, options
define("port", default=8889, help="run on the given port", type=int)
define("mysql_host", default="127.0.0.1:3306", help="default mysql host")
define("mysql_database", default="ss12", help="default mysql database")
define("mysql_user", default="ss12", help="default mysql user")
define("mysql_password", default="ss12", help="default mysql password")
class HomeHandler(tornado.web.RequestHandler):
def get(self):
self.render("home.html")
class MathParser(tornado.web.RequestHandler):
def parseMathXL(self, inString):
outString = ""
previous = False
i = 0
while(i < len(inString)):
if inString[i] == '-':
if not previous:
outString += " "
previous = True
outString += 'minus '
elif inString[i] == '*':
if not previous:
outString += " "
previous = True
outString += 'times '
elif inString[i] == '+':
if not previous:
outString += " "
previous = True
outString += 'plus '
elif inString[i] == '/':
if not previous:
outString += " "
previous = True
outString += 'divided by '
elif inString[i] == '(':
if not previous:
outString += " "
previous = True
outString += 'open parentheses '
elif inString[i] == ')':
if not previous:
outString += " "
previous = True
outString += 'close parentheses '
elif inString[i] == '^':
if not previous:
outString += " "
previous = True
outString += 'to the power of '
elif inString[i] == 's' and inString[i+1] == 'q' and inString[i+2] == 'r' and inString[i+3] == 't':
if not previous:
outString += " "
previous = True
outString += 'the square root of '
i += 3
elif inString[i] == 'p' and inString[i+1] == 'i':
if not previous:
outString += " "
previous = True
outString += 'pi '
i+=1
elif inString[i] == 's' and inString[i+1] == 'i' and inString[i+2] == 'n':
if not previous:
outString += " "
previous = True
outString += 'sine '
i += 2
elif inString[i] == 'c' and inString[i+1] == 'o' and inString[i+2] == 's':
if not previous:
outString += " "
previous = True
outString += 'cosine '
i += 2
elif inString[i] == 't' and inString[i+1] == 'a' and inString[i+2] == 'n':
if not previous:
outString += " "
previous = True
outString += 'tanget '
i += 2
else:
outString += inString[i]
previous = False
i+=1
return outString
def get(self):
text = self.get_argument("text")
self.write(self.parseMathXL(text))
class BaseHandler(tornado.web.RequestHandler):
@property
def db(self):
return self.application.db
class DecksListHandler(BaseHandler):
def get(self):
entries = self.db.query("SELECT * FROM DECK")
self.render("deckslist.html", entries=entries)
class CardsInDeckListHandler(BaseHandler):
def get(self):
deckid = self.get_argument("deckid")
entries = self.db.query("SELECT * FROM CARDS WHERE DECKID=%s", deckid)
self.render("cardsindecklist.html", entries=entries, deckid=deckid)
def post(self):
deckid = self.get_argument("deckid")
self.redirect("newcard?deckid="+deckid)
class CardHandler(BaseHandler):
def get(self):
deckid = self.get_argument("deckid")
cardid = self.get_argument("cardid")
cardindex = 0
entries = self.db.query("SELECT * FROM CARDS WHERE DECKID=%s", deckid)
for i in range(0,len(entries)):
if str(entries[i].ID) == str(cardid):
cardindex = i
self.render("card.html", entries=entries, cardid=cardid, cardindex=cardindex,deckid=deckid)
def post(self):
print "test"
action = self.get_argument("action")
cardindex = int(self.get_argument("cardindex"))
deckid = self.get_argument("deckid")
print str(deckid)
entries = self.db.query("SELECT * FROM CARDS WHERE DECKID=%s", deckid)
print str(len(entries))
if str(action) == "previous":
self.redirect("card?deckid="+str(deckid)+"&cardid="+str(entries[cardindex-1].ID))
else:
self.redirect("card?deckid="+str(deckid)+"&cardid="+str(entries[cardindex+1].ID))
class NewDeckHandler(BaseHandler):
def get(self):
self.render("newdeck.html")
def post(self):
name = self.get_argument("name", None)
if name:
entry = self.db.get("SELECT * FROM DECK WHERE name = %s", str(name))
if entry: raise tornado.web.HTTPError(404) #duplicate
self.db.execute("INSERT INTO DECK (userid,name) VALUES (%s,%s)",1, name)
entries = self.db.query("SELECT * FROM DECK WHERE name=%s",name)
self.redirect("newcard?deckid="+str(entries[0].ID))
class CardHandlerNew(BaseHandler):
def get(self):
deckid = self.get_argument("deckid", None)
self.render("newcard.html",deckid=deckid)
def post(self):
question = self.get_argument("cardquestion", None)
answer = self.get_argument("cardanswer", None)
deckid = self.get_argument("deckid", None)
print "%s %s %s", question, answer, deckid
if question:
entry = self.db.get("SELECT * FROM CARDS WHERE QUESTION = %s", str(question))
if entry: raise tornado.web.HTTPError(404) #duplicate<input type="hidden" value="{{deckid.value}}" name="deckid" id="deckid" />
self.db.execute(
"INSERT INTO CARDS (DECKID,QUESTION,ANSWER) VALUES (%s,%s,%s)",
deckid, question, answer)
self.redirect("cardsindecklist?deckid="+deckid)
class ModifyCardHandler(BaseHandler):
def get(self):
deckid = self.get_argument("deckid", None)
cardid = self.get_argument("cardid", None)
entries = self.db.query("SELECT * FROM CARDS WHERE ID=%s",cardid)
print len(entries)
entry = entries[0]
self.render("modcard.html", entry=entry)
def post(self):
deckid = self.get_argument("deckid", None)
cardid = self.get_argument("cardid", None)
question = self.get_argument("cardquestion", None)
answer = self.get_argument("cardanswer", None)
self.db.execute("UPDATE CARDS SET QUESTION=%s, ANSWER=%s WHERE ID=%s", question, answer, cardid)
self.redirect("cardsindecklist?deckid="+deckid)
class DeleteDeckHandler(BaseHandler):
def get(self):
deckid = self.get_argument("deckid")
self.db.execute("DELETE FROM CARDS WHERE DECKID=%s", deckid)
self.db.execute("DELETE FROM DECK WHERE ID=%s", deckid)
self.redirect("deckslist")
class DeleteCardHandler(BaseHandler):
def get(self):
cardid = self.get_argument("cardid")
deckid = self.get_argument("deckid")
self.db.execute("DELETE FROM CARDS WHERE ID=%s",cardid)
self.redirect("/cardsindecklist?deckid="+deckid)
class ConfirmDeleteHandler(BaseHandler):
def get(self):
deckid = self.get_argument("deckid", None)
cardid = self.get_argument("cardid", None)
self.render("confirmdelete.html", cardid=cardid, deckid=deckid)
def post(self):
deckid = self.get_argument("deckid", None)
cardid = self.get_argument("cardid", None)
result = self.get_argument("result", None)
print cardid
print deckid
if result == "confirm":
if str(cardid) == "None":
self.redirect("deldeck?deckid="+deckid)
else:
self.redirect("delcard?deckid="+str(deckid)+"&cardid="+str(cardid))
else:
if str(cardid) == "None":
self.redirect("deckslist")
else:
self.redirect("cardsindecklist?deckid="+deckid)
class ViewDeckHandler(tornado.web.RequestHandler):
def get(self):
self.render("viewdeck.html")
class NewCardHandler(tornado.web.RequestHandler):
def get(self):
self.render("newcard.html")
class ViewCardHandler(tornado.web.RequestHandler):
def get(self):
self.render("viewcard.html")
class MathHandler(tornado.web.RequestHandler):
def get(self):
self.render("math.html")
class Application(tornado.web.Application):
def __init__(self):
handlers = [
(r"/", HomeHandler),
(r"/card", CardHandler),
(r"/newdeck", NewDeckHandler),
(r"/viewdeck", ViewDeckHandler),
(r"/newcard", CardHandlerNew),
(r"/deldeck", DeleteDeckHandler),
(r"/confirmdelete", ConfirmDeleteHandler),
(r"/delcard", DeleteCardHandler),
(r"/modcard", ModifyCardHandler),
(r"/cardsindecklist", CardsInDeckListHandler),
(r"/viewcard", ViewCardHandler),
(r"/deckslist", DecksListHandler),
(r"/math", MathHandler),
(r"/mathparse", MathParser)]
settings = dict(
template_path=os.path.join(os.path.dirname(__file__), "templates"),
debug=True, static_path=os.path.join(os.path.dirname(__file__), "css"),
)
tornado.web.Application.__init__(self, handlers, **settings)
self.db = tornado.database.Connection(
host=options.mysql_host, database=options.mysql_database,
user=options.mysql_user, password=options.mysql_password)
def main():
tornado.options.parse_command_line()
http_server = tornado.httpserver.HTTPServer(Application())
http_server.listen(options.port)
tornado.ioloop.IOLoop.instance().start()
if __name__ == "__main__":
main()