Changeset 298
- Timestamp:
- 06/12/05 10:30:36
- Files:
-
- trunk/cherrypy/lib/filter/sessionfilter/basesession.py (modified) (3 diffs)
- trunk/cherrypy/lib/filter/sessionfilter/basesessiondict.py (modified) (1 diff)
- trunk/cherrypy/lib/filter/sessionfilter/dbmsession.py (modified) (2 diffs)
- trunk/cherrypy/lib/filter/sessionfilter/filesession.py (modified) (2 diffs)
- trunk/cherrypy/lib/filter/sessionfilter/ramsession.py (modified) (3 diffs)
- trunk/cherrypy/tutorial/tut10_sessionfilter.conf (modified) (1 diff)
- trunk/cherrypy/tutorial/tut10_sessionfilter.py (modified) (4 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/cherrypy/lib/filter/sessionfilter/basesession.py
r292 r298 39 39 """ 40 40 This is the class from which all session storage types are derived. 41 The functions which need to be redefined are at the end of the file 41 42 """ 42 43 43 # configname is the string storageType value used by the 44 # configuration file 45 configName = 'BaseSession' 44 # these are the functions that need to rewritten 45 def delSession(self, sessionKey): 46 """ delete a session from storage """ 47 48 def getSession(self, sessionKey): 49 """ function to lookup the session """ 46 50 47 """ 48 autoKeys is used to tell the server if the session storage class 49 can automaticly determine all of the key names. This would be 50 true if you are using a relational database and false if you are 51 using python dictionaries. 51 def setSession(self, sessionData): 52 """ function to save sesion data """ 52 53 53 If autoKeys is false the key names must be provided at runtime. 54 If it is true any key names provied at runtime are ignored. 55 """ 56 autoKeys = True 57 54 def cleanUpOldSessions(self): 55 """This function cleans up expired sessions""" 56 57 def newSession(self): 58 """ Return a new sessiondict instance """ 59 60 # it might be usefull to redefine this function 61 def generateSessionKey(self): 62 """ Function to return a new sessioId """ 63 sessionKeyFunc = sessionconfig.retrieve('keyGenerator', self.sessionName, None) 64 65 if sessionKeyFunc: 66 newKey = cherrypy._cputil.getSpecialAttribute(sessionKeyFunc)() 67 else: 68 newKey = sha.new('%s%s' % (time.time(), random.random())).hexdigest() 69 70 return newKey 58 71 59 72 def __init__(self, sessionName): 73 """ 74 Create the session caceh and set the session name. Make if you write 75 a custom __init__ function make sure you make a call to 76 BaseSession.__init__(sessioName) 77 """ 78 60 79 self.__sessionCache = {} 61 self.defaultValues = {}62 80 self.sessionName = sessionName 63 """ 64 This is where the you initialize your session storage class. 65 sessionOptions is a direct mapping of the configuration 66 options. 67 68 The keys 'host', 'user', 'password', and 'database', 69 must be used by classes that need to connect to remote 70 databases. 'tableName' will contain the table name (duh!). 71 72 The keyw 'dataKeys' will map to a list of the variable names you wish 73 to store in your session object. If autoKeys is true you will use it 74 to set to create sessionMap instances. 75 """ 81 82 83 # there should never be a reason to modify the remaining functions, they used 84 # internally by the sessionFilter 76 85 77 86 def getDefaultAttributes(self): … … 83 92 } 84 93 85 def newSession(self):86 """ Return a new sessionMap instance """87 # this needs to check the config file for default values88 newData = self.getDefaultAttributes()89 newData.update(self.defaultValues)90 return SimpleSessionDict(newData)91 92 def generateSessionKey(self):93 """ Function to return a new sessioId """94 sessionKeyFunc = sessionconfig.retrieve('keyGenerator', self.sessionName, None)95 96 if sessionKeyFunc:97 newKey = cherrypy._cputil.getSpecialAttribute(sessionKeyFunc)()98 else:99 s = [random.choice(string.letters+string.digits) for i in xrange(50)]100 s.append('%s'%time.time())101 newKey = sha.sha(''.join(s)).hexdigest()102 103 return newKey104 105 94 def loadSession(self, sessionKey, autoCreate = True): 106 95 cpg = cherrypy.cpg … … 154 143 del self.__sessionCache[session.key] 155 144 156 def dropSession(self, sessionKey):157 self.delSession()158 """ delete a session from storage """159 160 def cleanUpOldSessions(self):161 """This function cleans up expired sessions"""162 trunk/cherrypy/lib/filter/sessionfilter/basesessiondict.py
r283 r298 39 39 return result 40 40 return _inner 41 42 import threading43 41 44 42 # this is a dictionary like class that will be exposed to the application trunk/cherrypy/lib/filter/sessionfilter/dbmsession.py
r280 r298 33 33 34 34 from sessionerrors import * 35 36 import cPickle as pickle 35 from simplesessiondict import SimpleSessionDict 37 36 38 37 class DBMSession(BaseSession): … … 45 44 sessionFile=cpg.config.get('%s.dbFile' % sessionName, '%s.db' % sessionName) 46 45 self.__data = shelve.open(sessionFile, 'c') 46 47 def newSession(self): 48 """ Return a new sessiondict instance """ 49 newData = self.getDefaultAttributes() 50 return SimpleSessionDict(newData) 47 51 48 52 def getSession(self, sessionKey): trunk/cherrypy/lib/filter/sessionfilter/filesession.py
r282 r298 37 37 38 38 import threading 39 from simplesessiondict import SimpleSessionDict 39 40 40 41 class FileSession(BaseSession): … … 49 50 return storageDir 50 51 52 def newSession(self): 53 """ Return a new sessiondict instance """ 54 newData = self.getDefaultAttributes() 55 return SimpleSessionDict(newData) 56 51 57 # all session writes are blocked 52 58 def getSession(self, sessionKey): trunk/cherrypy/lib/filter/sessionfilter/ramsession.py
r275 r298 31 31 32 32 from sessionerrors import * 33 from simplesessiondict import SimpleSessionDict 33 34 34 35 class RamSession(BaseSession): … … 36 37 BaseSession.__init__(self, sessionName) 37 38 self.__data = {} 38 39 40 def newSession(self): 41 """ Return a new sessiondict instance """ 42 newData = self.getDefaultAttributes() 43 return SimpleSessionDict(newData) 44 39 45 def getSession(self, sessionKey): 40 46 try: … … 44 50 45 51 def setSession(self, sessionData): 46 # since everything in in ram ethe52 # since everything in in ram the 47 53 # session we don't need to update the data 48 54 # unless int is a new session trunk/cherrypy/tutorial/tut10_sessionfilter.conf
r275 r298 4 4 server.environment = "production" 5 5 6 # by default we get 7 # sessionFilter.on=True 8 # sessionFilter.new='sessionMap' 9 # sessionMap.storageType='ram' 10 6 11 [/admin] 12 # this tell CherryPy to create a session, 13 # named 'adminSession' and to store the data in ram 7 14 sessionFilter.new='adminSession' 8 15 adminSession.storageType='ram' trunk/cherrypy/tutorial/tut10_sessionfilter.py
r279 r298 6 6 7 7 class HitCounter: 8 9 # this is just a primative template function 8 10 def __examplePage(self, poweredBy, count, links, sessionKey): 9 11 yield '<html><head><title>sessionFilter exampe</title><body>\n' … … 17 19 yield '\n</body></html>' 18 20 21 # a list of the pages used in the example so we add pages 22 # without changing any code 19 23 samplePages = ['admin', 'index', 'forum'] 20 21 def admin(self):22 adminCount = cpg.sessions.adminSession.get('adminCount', 0) + 123 cpg.sessions.adminSession['adminCount'] = adminCount24 25 def index(self): 26 # this function uses the sessionMap which is turned on by defualt 27 # it may not be the defualt in future versions 24 28 25 key = cpg.sessions.adminSession.key26 return self.__examplePage('ram', adminCount, self.samplePages, key)27 28 admin.exposed = True29 30 def forum(self):31 forumCount = cpg.sessions.forumSession.get('forumCount', 0) + 132 cpg.sessions.forumSession['forumCount'] = forumCount33 34 key = cpg.sessions.forumSession.key35 return self.__examplePage('ram', forumCount, self.samplePages, key)36 forum.exposed=True37 38 def index(self):39 29 # Increase the silly hit counter 40 30 count = cpg.sessions.sessionMap.get('count', 0) + 1 … … 50 40 index.exposed = True 51 41 52 # these functions do the same as the index but with a different session dictionary53 42 def admin(self): 43 # this function uses the adminSession which is defined in 44 # the config file "tut10_sessionFilter.conf", otherwise 45 # it mirrors the session function 46 54 47 adminCount = cpg.sessions.adminSession.get('adminCount', 0) + 1 55 48 cpg.sessions.adminSession['adminCount'] = adminCount … … 57 50 key = cpg.sessions.adminSession.key 58 51 return self.__examplePage('ram', adminCount, self.samplePages, key) 59 60 52 admin.exposed = True 61 53 62 54 def forum(self): 55 # this function uses the forumSession which is defined in 56 # the config file "tut10_sessionFilter.conf", otherwise 57 # it mirrors the session function 58 63 59 forumCount = cpg.sessions.forumSession.get('forumCount', 0) + 1 64 60 cpg.sessions.forumSession['forumCount'] = forumCount

