MongoSessions / mongosessions / session.py

import cherrypy
from pymongo import Connection

class MongoSession(cherrypy.lib.sessions.Session):
    
    def __init__(self, id, **kw):
        port = kw.get('port', 27017)
        host = kw.get('host', '127.0.0.1')
        self._dbname = kw.get('dbname', 'cp_session_db')
        self._collection = kw.get('collection', 'cp_sessions')
        self._conn = Connection(host, port)
        cherrypy.lib.sessions.Session.__init__(self, id, **kw)

    def acquire_lock(self):
        self.locked = True
        print 'locked'        
        
    def release_lock(self):
        self.locked = False
        print 'unlocked'

    def _get_store(self):
        return self._conn[self._dbname][self._collection]
    db = property(_get_store)

    def _load(self):
        doc = self.db.find_one({'session_id': self.id})
        if doc:
            return doc['session_data'], doc['expiration_time']
        return None

    def _save(self, expiration_time):
        print 'calling save', self._data
        doc = {
            'session_id': self.id,
            'session_data': self._data,
            'expiration_time': expiration_time,
        }
        doc = self.db.update({'session_id': self.id}, doc, True)
        doc = self.db.find_one({'session_id': self.id})
        print doc

    def _delete(self):
        self.db.remove({'session_id': self.id})
        

cherrypy.lib.sessions.MongoSession = MongoSession


if __name__ == '__main__':

    from textwrap import dedent
    from pprint import pformat

    class SampleApp(object):
        @cherrypy.expose
        def index(self):
            cherrypy.session['info'] = {
                'foo': 'bar',
                'user_agent': cherrypy.request.headers['User-Agent']
            }
            return dedent('''
                <html>
                <head><title>test it!</title></head>
                <body>
                          <a href="/sess_value">Check out this session info</a>
                <hr />
                <body>
            ''')

        @cherrypy.expose
        def sess_value(self):
            cherrypy.response.headers['Content-Type'] = 'text/plain'
            return pformat(cherrypy.session['info'])


    config = {
        '/': {
            'tools.sessions.on' : True ,
            'tools.sessions.storage_type'  : 'mongo',
            'tools.sessions.name' : 'MongoSes',
        }
    }

    cherrypy.config.update({
        'server.socket_port': 9000,
        'server.socket_host': '0.0.0.0',
    })
    cherrypy.tree.mount(SampleApp(), script_name='/', config=config)

    cherrypy.server.quickstart()
    cherrypy.engine.start()
    
    
        
        

        

    
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.