From: Michael Tremer Date: Mon, 23 Nov 2009 11:57:53 +0000 (+0100) Subject: Split into many seperate files. X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=880775b494967827688c074d1616b2adaf09d979;p=people%2Fms%2Fwebapp.git Split into many seperate files. --- diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..539da74 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +*.py[co] diff --git a/application/__init__.py b/application/__init__.py new file mode 100644 index 0000000..e089049 --- /dev/null +++ b/application/__init__.py @@ -0,0 +1,48 @@ +#!/usr/bin/python + +import os +import sys +import tornado.web + +BASEDIR = os.path.join(os.path.dirname(__file__), "..") + +from auth import LoginHandler, LogoutHandler +from handlers import MainHandler + +class Application(tornado.web.Application): + def __init__(self): + settings = dict( + template_path = os.path.join(BASEDIR, "templates"), + static_path = os.path.join(BASEDIR, "static"), + xsrf_cookies = True, + cookie_secret = "123456789", + login_url = "/login", + ) + + handlers = [ + (r"/", MainHandler), + (r"/login", LoginHandler), + (r"/logout", LogoutHandler), + ] #self.get_handlers() + + tornado.web.Application.__init__(self, handlers, **settings) + +# # XXX This is not a nice solution but works for the moment +# def get_handlers(self): +# handlers_dir = os.path.join(BASEDIR, "pages") +# sys.path.append(handlers_dir) +# handlers = [] +# +# for handler in os.listdir(handlers_dir): +# if not handler.endswith(".py"): +# continue +# +# handler = handler[:-3] +# handler = __import__(handler) +# +# if type(handler.handle) == type([]): +# handlers.extend(handler.handle) +# else: +# handlers.append(handler.handle) +# +# return handlers diff --git a/application/auth.py b/application/auth.py new file mode 100644 index 0000000..b8ea744 --- /dev/null +++ b/application/auth.py @@ -0,0 +1,17 @@ +#!/usr/bin/python + +from handlers import BaseHandler + +class LoginHandler(BaseHandler): + def get(self): + self.render("login.html", footer=self.footer()) + + def post(self): + self.set_secure_cookie("user", self.get_argument("user")) + self.redirect("/") + + +class LogoutHandler(BaseHandler): + def get(self): + self.clear_cookie("user") + self.render("logout.html", footer=self.footer()) diff --git a/application/handlers.py b/application/handlers.py new file mode 100644 index 0000000..0c6a73c --- /dev/null +++ b/application/handlers.py @@ -0,0 +1,21 @@ +#!/usr/bin/python + +import socket + +import tornado.web + +class BaseHandler(tornado.web.RequestHandler): + def get_current_user(self): + return self.get_secure_cookie("user") + + def footer(self): + return "%s" % socket.gethostname() + +class MainHandler(BaseHandler): + def get(self): + if not self.current_user: + self.redirect("/login") + return + + self.render("template.html", title="Testsite", slogan="Security now!", + footer=self.footer(), user=self.current_user) diff --git a/webapp.py b/webapp.py index ba72804..6e83f4b 100644 --- a/webapp.py +++ b/webapp.py @@ -1,62 +1,15 @@ #!/usr/bin/python -import os -import socket -import time +import application import tornado.httpserver import tornado.ioloop -import tornado.web +import tornado.options -BASEDIR = os.path.dirname(__file__) - -settings = { - "static_path" : os.path.join(BASEDIR, "static"), - "template_path" : os.path.join(BASEDIR, "templates"), - "login_url" : "/login", - "cookie_secret" : "123456789", - "xsrf_cookies" : True, -} - -class BaseHandler(tornado.web.RequestHandler): - def get_current_user(self): - return self.get_secure_cookie("user") - - def footer(self): - return "%s" % socket.gethostname() - - -class MainHandler(BaseHandler): - def get(self): - if not self.current_user: - self.redirect("/login") - return - - self.render("template.html", title="Testsite", slogan="Security now!", - footer=self.footer(), user=self.current_user) - - -class LoginHandler(BaseHandler): - def get(self): - self.render("login.html", footer=self.footer()) - - def post(self): - self.set_secure_cookie("user", self.get_argument("user")) - self.redirect("/") - -class LogoutHandler(BaseHandler): - def get(self): - self.clear_cookie("user") - self.render("logout.html", footer=self.footer()) - - -application = tornado.web.Application([ - (r"/", MainHandler), - (r"/login", LoginHandler), - (r"/logout", LogoutHandler), -], **settings) +app = application.Application() if __name__ == "__main__": - http_server = tornado.httpserver.HTTPServer(application) + tornado.options.enable_pretty_logging() + http_server = tornado.httpserver.HTTPServer(app) http_server.listen(8080) tornado.ioloop.IOLoop.instance().start()