Entries Tagged 'Pylons' ↓

How to write and read array from cookie in Pylons

How to write and read array from cookies ?

The easiest way to write an array in cookie is serialize it using pickle module and quote returned string with urllib module. For example:

import pickle, urllib
cookie = []
response.set_cookie('some_cookie', urllib.quote(pickle.dumps(cookie)))

And getting an array from cookie:

cookie = pickle.loads(str(urllib.unquote(request.cookies['some_cookie'])))

The easiest way to modify response headers in Pylons

Remember the trick!

The easiest way to modify response headers in Pylons is add:

response.headerlist = [('Content-type', 'image/png')]

just before render() function.

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')