Simple solution – how to use own decorators in Pylons actions

This is a simple solution to create own decorator function for any action in Pylons.

First, you have to import some functions:

from decorator import decorator
from pylons.decorators.util import get_pylons # to get request environment

Now create decorator function, for example myowndecorator:

def myowndecorator():
    def wrapper(func, *args, **kwargs):
        request = get_pylons(args).request # use it for any operation with request
        # do something
        return func(*args, **kwargs)
    return decorator(wrapper)

Save it in your project lib directory as mylib.py.

In last step you have to import function from you library, and use decorator in your controller:

from myproject.lib.mylib import myowndecorator

class PanelController(BaseController):
    @myowndecorator()
    def index(self):
        return render('/panel/index.mako')
Share and Enjoy:
  • Google Bookmarks
  • Twitter
  • BLIP - Bardzo Lubię Informować Przyjaciół
  • pinger.pl - Nie taki zwykły blog.
  • Facebook
  • del.icio.us
  • Digg
  • Wrzuć to na Flakera - powiadom swoich Znajomych
  • grono.net - internetowa społeczność przyjaciół
  • Netvibes
  • LinkedIn
  • Technorati
  • Dodaj niusa « OSnews.pl

0 comments ↓

There are no comments yet...Kick things off by filling out the form below.

Leave a Comment