Session help in ajax

46 views
Skip to first unread message

Partho Ghosh

unread,
Jun 5, 2013, 11:11:04 AM6/5/13
to we...@googlegroups.com
Hi All,

I looked through the posts in group and didn't find any suitable solution for my query so I am posting it.



This is how I am creating session in my app:

web.config.debug=False

web.config.session_parameters['cookie_name'] = 'webpy_session_id'
web.config.session_parameters['cookie_domain'] = '/'
web.config.session_parameters['timeout'] = 86400, #24 * 60 * 60, # 24 hours   in seconds
web.config.session_parameters['ignore_expiry'] = True
web.config.session_parameters['ignore_change_ip'] = True
web.config.session_parameters['secret_key'] = 'fLjUfxqXtfNoIldA0A0J'
web.config.session_parameters['expired_message'] = 'Session expired'


store=web.session.DiskStore('sessions')

if web.config.get('_session') is None:
    session = web.session.Session(app, store, initializer={'username':'Guest','userId':'Guest','loggedIn':False})
    web.config._session = session
else:
    session = web.config._session
render = web.template.render('templates/', base='main', globals={'session':session})

And this how I have implemented login into my system:

class Login:
    def POST(self):
        sFormData = web.input()        
        url="<my app api>"
        response = urllib2.urlopen(url)
        user=json.load(response)        
        
        if user['available']:            
            session.username=user['username']
            session.userId=user['userid']
            session.loggedIn=True
                                        
        print "returning json dump"  
        print "Session username in login---->", session.username
        print "Session loggedIn in login---->", session.loggedIn
        print "Session userId in login---->", session.userId     

        return json.dumps(user)  


As you can see, the above controller returns a JSON which is then consumed in javascript and then app is updated accordingly. This is WIP. What my concern is that session is not getting saved. I dont want to store session in a DB.... what is currently happening in now that when I refresh page, my session data is lost. I am passing session as global in template so that I can access session data in my template. I tried every possible solution available. I am frankly unable to detect where I am going wrong in this. Please provide some help.


Regards,
Partho

Partho Ghosh

unread,
Jun 6, 2013, 3:54:42 PM6/6/13
to we...@googlegroups.com
Guys I have solved the issue. Needed to add cookie details.

Just added web.config.session_parameters.update(cookie_name="test_cookie", cookie_domain="/",cookie_path=store,timeout="60") in following manner and now session is working as intended:

if web.config.get('_session') is None:
    session = web.session.Session(app, store, initializer={'username':'Guest','userId':'Guest','loggedIn':False})
    web.config._session = session
else:
    session = web.config._session
web.config.session_parameters.update(cookie_name="test_cookie", cookie_domain="/",cookie_path=store,timeout="60")    
render = web.template.render('templates/', base='main', globals={'session':session})

Regards,
Partho
Reply all
Reply to author
Forward
0 new messages