Created
March 27, 2013 05:30
-
-
Save puentesarrin/5251927 to your computer and use it in GitHub Desktop.
Async @authenticated using Motor
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<html> | |
<body> | |
<p> | |
Current user: <strong>{{ current_user }}</strong> | |
</p> | |
<a href="/logout">Logout</a> | |
</body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<html> | |
<body> | |
<strong>{{ current_user }}</strong> | |
<form action="/login" method="post"> | |
<input type="text" name="email" /> | |
<input type="submit" /> | |
</form> | |
</body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 *-* | |
import urllib | |
import functools | |
import tornado.gen | |
import tornado.ioloop | |
import tornado.web | |
import motor | |
from motor import Op | |
def authenticated_async(f): | |
@functools.wraps(f) | |
@tornado.gen.engine | |
def wrapper(self, *args, **kwargs): | |
self._auto_finish = False | |
self.current_user = yield tornado.gen.Task(self.get_current_user_async) | |
if not self.current_user: | |
self.redirect(self.get_login_url() + '?' + | |
urllib.urlencode(dict(next=self.request.uri))) | |
else: | |
f(self, *args, **kwargs) | |
return wrapper | |
class BaseHandler(tornado.web.RequestHandler): | |
current_user = None | |
def prepare(self): | |
self.db = self.settings['db'] | |
@tornado.gen.engine | |
def get_current_user_async(self, callback): | |
user = self.get_secure_cookie('current_user', None) | |
if user: | |
user = yield Op(self.db.users.find_one, {"email": user}) | |
callback(user) | |
class HomeHandler(BaseHandler): | |
@tornado.web.asynchronous | |
@tornado.gen.engine | |
@authenticated_async | |
def get(self): | |
self.render('home.html') | |
class LoginHandler(BaseHandler): | |
def get(self): | |
self.render('login.html') | |
def post(self): | |
self.set_secure_cookie('current_user', self.get_argument('email')) | |
self.redirect('/') | |
class LogoutHandler(BaseHandler): | |
def get(self): | |
self.clear_cookie("current_user") | |
self.redirect("/") | |
app = tornado.web.Application([ | |
(r'/', HomeHandler), | |
(r'/login', LoginHandler), | |
(r'/logout', LogoutHandler) | |
], | |
template_path='.', | |
login_url='/login', | |
cookie_secret='test', | |
db=motor.MotorClient().open_sync().test | |
) | |
app.listen(8888) | |
tornado.ioloop.IOLoop.instance().start() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment