advertisement

A Django like messages framework for Tornado

100 %
0 %
advertisement
Information about A Django like messages framework for Tornado
Technology

Published on March 16, 2014

Author: ChaoGao1

Source: slideshare.net

advertisement

Introduction to a simple messages framework for Tornado Gao Chao 2014.03.14

Tornado • A high performance asynchronous web framework. • Originated in FriendFeed, acquired by Facebook later on. • We are using it!

Messages Framework • Taken from Django’s messages framework. • Flash a message after certain actions by user.

Design concern • Easy to use and simple implementation. • Messages cannot be dropped on redirection. • All unseen messages need to be displayed.

Problems • Implementation • Storage

Messages Lifetime • Request coming in. • Server execute an action based on passed parameters. • Server popup a message to user. • User sees the web page and the message.

Hook • def prepare(self) • def render(self, template_name, **kwargs)

Easy Solution def prepare(self): # Define a message array as a property of each # request, and just append every message to # it. self.request.messages = [] ! def MyHandler(tornado.web.RequestHandler): def get(self): self.request.messages.append("error") return self.render("my_page.html")

Redirection Message got lost.! why?

Cookies! or session storage

Solution • Save messages in cookies • Clear messages only after user has seen.

How? • def redirect(self, url, permanet=False, status=None) • def __iter__(self)

HTML {% if messages %} <ul class="messages"> {% for message in messages %} <li>{{ message }}</li> {% end %} </ul> {% end %}

Clear • We can erase all messages stored in cookies after __iter__ method of Message object was called. • We don’t need to update cookies when there is no redirections happen.

To sum up class BaseHandler(tornado.web.RequestHandler): def prepare(self): # Create Message object in order to store instant messages. self.messages = FlashMessage(handler=self) ! def redirect(self, url, permanent=False, status=None): if hasattr(self, "messages"): # Update messages in Cookie, write in or clear all. self.messages.update_messages() ! ! class LoginHandler(BaseHandler): def post(self): # Write message to object owned structure. messages.success(self.messages, "Login successfully!") ! # Redirect to another handler. self.redirect("/")

Message Insight ! ! class FlashMessage(object): """ A Django messages framework mockup, use cookie to store messages. """ … ! def __iter__(self): self.used = True if self._queued_messages: self._loaded_messages.extend(self._queued_messages) self._queued_messages = [] # Update messages stored in Cookie self.update_messages() return iter(self._loaded_messages) ! def update_messages(self): if self.used: self._store(self._queued_messages) elif self.added_new: messages = self._loaded_messages + self._queued_messages self._store(messages) ! def _update_cookie(self, encoded_data): if encoded_data and eval(encoded_data or ''): self._handler.set_secure_cookie(self.cookie_name, encoded_data, expires_days=None, domain=settings.SESSION_COOKIE_DOMAIN) else: self._handler.clear_cookie(self.cookie_name, domain=settings.SESSION_COOKIE_DOMAIN) ! def _store(self, messages): encoded_data = self._encode(messages) # Update cookie, remove if no data included. self._update_cookie(encoded_data)

Read the source code to get more of it!

Thank you!

Add a comment

Related presentations

Presentación que realice en el Evento Nacional de Gobierno Abierto, realizado los ...

In this presentation we will describe our experience developing with a highly dyna...

Presentation to the LITA Forum 7th November 2014 Albuquerque, NM

Un recorrido por los cambios que nos generará el wearabletech en el futuro

Um paralelo entre as novidades & mercado em Wearable Computing e Tecnologias Assis...

Microsoft finally joins the smartwatch and fitness tracker game by introducing the...

Related pages

Django and Tornado: Python Web Frameworks - CodeProject

Learn about Django and Tornado; ... Much like any web framework, ... and I'll respond to let you know that I got your message. Don't be shy ...
Read more

The messages framework | Django documentation | Django

The messages framework can use different backends to store temporary messages. Django provides ... like: {% if messages ... when the messages framework ...
Read more

Running Django on Tornado's HTTP server - Google Groups

web server/framework we built at ... frameworks like Django and web.py working on Tornado's server ... a Django app on Tornado's server ...
Read more

Integrating Django with Tornado's web server - Google Groups

(I just sent a message to django-users@ ... I'd prefer to see Django-Tornado support as a ... framework like Django where the initial rapid development ...
Read more

python - How to use tornado authentication in a django ...

... Django and Tornado for my website. Tornado has a wonderful authentication system and I would like to use it in the django ... django framework ...
Read more

Is Tornado the best framework to use for a REST API for a ...

Hide this message . Quora. Sign In . 4 ... REST framework, but people have said that Tornado is an ... tornado for an app like this, or is Django a better ...
Read more

[go-nuts] Any Pylon/Flask/Django like web framework for ...

... Any Pylon/Flask/Django like web framework for Golang and ... Tornado/Twisted like framework can handle ... You received this message because you are ...
Read more

mrjoes/sockjs-tornado · GitHub - GitHub · Where software ...

sockjs-tornado - WebSocket emulation ... Simplified echo SockJS server could look more or less like: from tornado import ... def on_message(self, msg ...
Read more

python - When to use Tornado, when to use Twisted ...

What is it your planning to do and could something like Django, ... "Tornado is a Python web framework and asynchronous networking library, ...
Read more

What exactly is the Tornado Framework? - Quora

It's a Python web framework for building web sites. It's like Ruby ... What exactly is the Tornado Framework? ... of Tornado over Django framework?
Read more