Most Powerful Open Source ERP

How To Use Session Tool

How To showing how to get a global RAM based session object associated to a user.
  • Last Update:2016-02-09
  • Version:001
  • Language:en

Table of Contents


The Session Tool (portal_sessions) allows the developer to get a global RAM based session object associated to user. This session object acts as a plain Python dictionary and can be used to save user preferences, emulate shopping cart behaviour and so on. Because it's accessible from any Python/Zope script it's easy to store and share any kind of information associated to current user within application.

How to Setup

It's a portal tool which currently is not added after ERP5 site setup. In order to use it you have:

  • Add the tool from ZMI, selecting "ERP5Type Tool" in the list of object to add.
  • Create the needed Cache Factory under portal_caches tool and rename it to erp5_session_cache.
  • Create default Ram Cache Plugin inside Cache Factory (id is not important).
  • Set cache_duration for both Cache Factory and Ram Cache Plugin to a reasonable value. For example 360000 (100 hours) is a good start. This value determines the "life" duration of a Session Object.
  • Restart Zope instance so new Cache Factory gets initialized.

How to Use it

There are some restrictions when using Session Tool:

  • By design Session Tool will NOT associate user with a session_id. This have to be implemented by developer by setting/getting a cookie on user side (browser) or any other method applicable in current environment.
  • Session Tool uses lazy Cache as a storage backend you do not need to initialize it first time you use it (Cache will take care for that).
  • It's a very bad idea to store reference to persistent ZODB objects inside Session Object. This can cause "RuntimeErrors" at any time because an ZODB objects is valid within the context of the current ZODB connection which might change at any time. Instead it's recommended that you use temporary RAM based objects having as container portal_sessins tool.
  • Session Tool is not intended to replace the current ERP5 way. Instead it can be used for some session based applications like online shopping magazines where other methods for tracking user selection can not be applied.

Example #1 (two logically connected scripts):

Script A

from Products.ERP5Type.Document import newTempOrder
session_id = '1234567' # you can acquire it from cookies
session = context.portal_sessions[session_id] 
session['shopping_cart'] = newTempOrder(context, '987654321')
#you can also use 'session.edit(shopping_cart= newTempOrder(context, '987654321'))' 

Script B

session_id = '1234567' # you can acquire it from cookies
session = context.portal_sessions[session_id]
shopping_cart = session['shopping_cart']
# do something with it .. 

Example #2 (using newContent API):

session_id = '1234567' # you can acquire it from cookies
session = context.portal_sessions.newSession(
                            shopping_cart=newTempOrder(context, '987654321'))

Example #3 (using zope BrowserIdManager to generate an unique session ID):

session_id = context.browser_id_manager.getBrowserId(create=1)

This will use browser_id_manager from zope root (unless you added another one in acquisition context) to generate and set a cookie automatically. The cookie name can be configured from browser_id_manager's management interface.

Example #4 (setting/getting cookie manually):

request = context.REQUEST
now = DateTime()
expire_timeout_days = 90
session_id = request.get('session_id', None)
if session_id is None:
  ## first call so generate session_id and send back via cookie
  session_id =  # generate it yourself
  request.RESPONSE.setCookie('erp5_session_id', session_id, expires=(now +expire_timeout_days).fCommon(), path='/') 

Related Articles