Database backend for sessions

5 views
Skip to first unread message

just...@ntlworld.com

unread,
Apr 12, 2006, 7:11:15 AM4/12/06
to TurboGears

Can anyone point me to some info on using the database backend for CherryPy sessions within TurboGears?

I've done a bit of googling and looked at the CherryPy docs but I'm wondering if there's a TurboGears API layer for setting it up (config file specification etc).

Cheers,

Justin


-----------------------------------------
Email sent from www.ntlworld.com
Virus-checked using McAfee(R) Software
Visit www.ntlworld.com/security for more information

Alberto Valverde

unread,
Apr 12, 2006, 7:59:48 AM4/12/06
to turbo...@googlegroups.com
1) Create your table:

CREATE TABLE session (
id varchar(40),
data text,
expiration_time timestamp
);


2) Set:

session_filter.on = True
session_filter.storage_type = "PostgreSQL"

in your .cfg file

2) Inside your controllers.py, (or any file that get's imported, you
need to make sure it runs before any request) set:

turbogears.config.update({'session_filter.get_db':get_db_for_sessions})

where get_db_for_sessions is a function that returns a connecction
object to your DB (sorry, no TG hubs here, needs to be a psycopg
conn. object. Something like:

def get_db_for_sessions():
import psycopg2
dsn = config.get('postgres.dsn')
return psycopg2.connect(dsn)

(you need a line at your .cf file like this:
postgres.dsn="dbname = db user=user password=passwd host=host")

This should get you started... The rest of the config options are
common to all the session storage backends.

HTH, Alberto

On 12/04/2006, at 13:11, <just...@ntlworld.com>

just...@ntlworld.com

unread,
Apr 12, 2006, 11:14:24 AM4/12/06
to turbo...@googlegroups.com

Thank you Alberto - very helpful.
Reply all
Reply to author
Forward
0 new messages