-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.py
79 lines (63 loc) · 2.23 KB
/
main.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
import tornado.ioloop
import tornado.web
import os
from sys import exit
import sqlite3
connection = sqlite3.connect('db')
cursor = connection.cursor()
class BaseHandler(tornado.web.RequestHandler):
def get_current_user(self):
return self.get_secure_cookie("user")
class MainHandler(BaseHandler):
def get(self):
cursor.execute("SELECT * FROM fruits")
x = cursor.fetchall()
self.render("index.html", fruits=x)
def post(self):
feedback = self.get_argument('feedback')
self.write("Thank you for the feedback: " + feedback)
class FruitHandler(BaseHandler):
@tornado.web.authenticated
def post(self, id):
cursor.execute("update fruits set quantity=" + self.get_argument("quantity") + " where id=" + id)
connection.commit()
self.redirect("/")
class LoginHandler(BaseHandler):
def get(self):
self.render("login.html")
def post(self):
cursor.execute("SELECT * FROM users WHERE username=\"{}\" AND password=\"{}\"".format(self.get_body_argument('username'), self.get_body_argument("password")))
a = cursor.fetchone()
if not a:
self.render("login.html")
return
self.set_secure_cookie("user", a[1])
self.redirect("/")
class LogoutHandler(BaseHandler):
def get(self):
self.clear_cookie("user")
self.redirect("/")
class Application(tornado.web.Application):
def __init__(self):
handlers = [
(r"/", MainHandler),
(r"/fruit/(.*)", FruitHandler),
(r"/login", LoginHandler),
(r"/logout", LogoutHandler),
]
settings = {
"template_path": os.path.join(
os.path.dirname(os.path.abspath(__file__)), "templates"
),
"static_path": os.path.join(
os.path.dirname(os.path.abspath(__file__)), "static"
),
"cookie_secret": "Chscf8wQI0rtYZBYE0Bx",
"login_url": "/login",
}
tornado.web.Application.__init__(self, handlers, **settings)
if __name__ == "__main__":
app = Application()
app.listen(8000)
print(f"App running: http://localhost:8000")
tornado.ioloop.IOLoop.current().start()