| 412 |
ashish |
1 |
'''
|
|
|
2 |
Created on 04-Aug-2010
|
|
|
3 |
|
|
|
4 |
@author: ashish
|
|
|
5 |
'''
|
|
|
6 |
|
|
|
7 |
import tornado.httpserver
|
|
|
8 |
import tornado.ioloop
|
|
|
9 |
import tornado.web
|
|
|
10 |
from shop2020.logistics.external.xlsprocessor import get_xls_for_today
|
|
|
11 |
|
|
|
12 |
class BaseHandler(tornado.web.RequestHandler):
|
|
|
13 |
def get_current_user(self):
|
|
|
14 |
return self.get_secure_cookie("user")
|
|
|
15 |
|
|
|
16 |
def get_warehouse(self):
|
|
|
17 |
return self.get_secure_cookie("warehouse")
|
|
|
18 |
|
|
|
19 |
class MainHandler(BaseHandler):
|
|
|
20 |
@tornado.web.authenticated
|
|
|
21 |
def get(self):
|
|
|
22 |
name = tornado.escape.xhtml_escape(self.current_user)
|
|
|
23 |
self.write("Hello, " + name)
|
|
|
24 |
|
|
|
25 |
class ListHandler(BaseHandler):
|
|
|
26 |
@tornado.web.authenticated
|
|
|
27 |
def get(self):
|
|
|
28 |
self.name = tornado.escape.xhtml_escape(self.current_user)
|
|
|
29 |
self.warehouse = tornado.escape.xhtml_escape(self.get_warehouse())
|
|
|
30 |
self.set_header("Content-Type", "application/vnd.ms-excel")
|
|
|
31 |
self.set_header("Content-Disposition", "attachment;filename=List Of Shipment.xls;")
|
|
|
32 |
self.write(get_xls_for_today(self.warehouse))
|
|
|
33 |
|
|
|
34 |
def get_xls_for_warehouse(self):
|
|
|
35 |
pass
|
|
|
36 |
|
|
|
37 |
class LoginHandler(BaseHandler):
|
|
|
38 |
def get(self):
|
|
|
39 |
self.write('<html><body><form action="/login" method="post">'
|
|
|
40 |
'Name: <input type="text" name="name">'
|
|
|
41 |
'Password:<input type="password" name="password">'
|
|
|
42 |
'<input type="submit" value="Sign in">'
|
|
|
43 |
'</form></body></html>')
|
|
|
44 |
|
|
|
45 |
def post(self):
|
|
|
46 |
if self.get_auth(self.get_argument("name"), self.get_argument("password")):
|
|
|
47 |
self.set_secure_cookie("user", self.get_argument("name"))
|
|
|
48 |
self.set_secure_cookie("warehouse", self.get_warehouse_id(self.get_argument("name")))
|
|
|
49 |
self.redirect("/list")
|
|
|
50 |
else:
|
|
|
51 |
self.redirect("/login")
|
|
|
52 |
|
|
|
53 |
def get_auth(self, user, password):
|
|
|
54 |
return True
|
|
|
55 |
|
|
|
56 |
def get_warehouse_id(self, user):
|
|
|
57 |
if user == "Ashish":
|
|
|
58 |
return "all"
|
|
|
59 |
return "1";
|
|
|
60 |
|
|
|
61 |
settings = {
|
|
|
62 |
"cookie_secret": "61oETzKXQAGaYdkL5gEmGeJJFuYh7EQnp2XdTP1o/Vb=",
|
|
|
63 |
"login_url": "/login",
|
|
|
64 |
}
|
|
|
65 |
|
|
|
66 |
application = tornado.web.Application([
|
|
|
67 |
(r"/", MainHandler),
|
|
|
68 |
(r"/login", LoginHandler),
|
|
|
69 |
(r"/list", ListHandler),
|
|
|
70 |
], **settings)
|
|
|
71 |
|
|
|
72 |
|
|
|
73 |
if __name__ == "__main__":
|
|
|
74 |
http_server = tornado.httpserver.HTTPServer(application)
|
|
|
75 |
http_server.listen(8888)
|
|
|
76 |
tornado.ioloop.IOLoop.instance().start()
|