Changeset 299
- Timestamp:
- 06/12/05 22:05:24
- Files:
-
- branches/ooconf/cherrypy/lib/cptools.py (modified) (1 diff)
- branches/ooconf/cherrypy/lib/filter/logdebuginfofilter.py (modified) (2 diffs)
- branches/ooconf/cherrypy/lib/filter/nsgmlsfilter.py (modified) (1 diff)
- branches/ooconf/cherrypy/lib/filter/sessionauthenticatefilter.py (modified) (3 diffs)
- branches/ooconf/cherrypy/lib/filter/sessionfilter/basesession.py (modified) (3 diffs)
- branches/ooconf/cherrypy/lib/filter/sessionfilter/dbmsession.py (modified) (2 diffs)
- branches/ooconf/cherrypy/lib/filter/sessionfilter/filesession.py (modified) (2 diffs)
- branches/ooconf/cherrypy/lib/filter/sessionfilter/ramsession.py (modified) (3 diffs)
- branches/ooconf/cherrypy/lib/filter/sessionfilter/sqlobjectsession.py (modified) (2 diffs)
- branches/ooconf/cherrypy/test/test_logdebuginfo_filter.py (modified) (1 diff)
- branches/ooconf/cherrypy/tutorial/tut10_sessionfilter.conf (modified) (1 diff)
- branches/ooconf/cherrypy/tutorial/tut10_sessionfilter.py (modified) (4 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
branches/ooconf/cherrypy/lib/cptools.py
r118 r299 50 50 def __getattr__(self, key): 51 51 return self.items[key] 52 53 class PositionalParametersAware(object): 54 """ 55 Utility class that restores positional parameters functionality that 56 was found in 2.0.0-beta. 57 58 Use case: 59 60 from cherrypy.lib import cptools 61 from cherrypy import cpg 62 class Root(cptools.PositionalParametersAware): 63 def something(self, name): 64 return "hello, " + name 65 something.exposed 66 cpg.root = Root() 67 cpg.server.start() 68 69 Now, fetch http://localhost:8080/something/name_is_here 70 """ 71 def default( self, *args, **kwargs ): 72 # remap parameters to fix positional parameters 73 if hasattr( self, args[ 0 ] ): 74 return getattr( self, args[ 0 ] )( *args[ 1: ], **kwargs ) 75 default.exposed = True 76 branches/ooconf/cherrypy/lib/filter/logdebuginfofilter.py
r247 r299 72 72 logList.append("Page size: %.02fKB" % ( 73 73 len(body)/float(1024))) 74 74 ''' 75 # this is not compatible with the session filter 75 76 if (cpg.config.get('logDebugInfoFilter.logSessionSize', True) 76 77 and cpg.config.get('session.storageType')): … … 85 86 except: 86 87 logList.append("Session data size: Unable to pickle session") 88 ''' 87 89 88 90 debuginfo += ', '.join(logList) branches/ooconf/cherrypy/lib/filter/nsgmlsfilter.py
r258 r299 67 67 nsgmlsPath, catalogPath, errFile, pageFile) 68 68 command = command.replace('\\', '/') 69 #command = '"%s" -f%s -s -E10 %s' % (70 # nsgmlsPath, errFile, pageFile)71 print "** comand:", command72 69 os.system(command) 73 70 f = open(errFile, 'rb') 74 71 err = f.read() 75 72 f.close() 76 77 73 errList = err.splitlines() 78 74 newErrList = [] 79 for err in newErrList: 80 if err.find('characters in the document character set with numbers exceeding 65535 not supported') != -1: 81 continue 82 newErrList.append(err) 75 for err in errList: 76 ignore = False 77 for errIgn in cpg.config.get('nsgmlsFilter.errorsToIgnore', []): 78 if err.find(errIgn) != -1: 79 ignore = True 80 break 81 if not ignore: 82 newErrList.append(err) 83 83 if newErrList: 84 84 newBody = "Wrong HTML:<br />" + cgi.escape('\n'.join(newErrList)).replace('\n','<br />') branches/ooconf/cherrypy/lib/filter/sessionauthenticatefilter.py
r258 r299 66 66 cpg.request.sessionMap['username'] = None 67 67 cpg.threadData.user = None 68 cpg.response.body = httptools.redirect('/') 68 fromPage = cpg.request.paramMap.get('fromPage') 69 if fromPage is None: 70 fromPage = '/' 71 cpg.response.body = httptools.redirect(fromPage) 69 72 elif cpg.request.path.endswith('doLogin'): 70 73 fromPage = cpg.request.paramMap['fromPage'] … … 73 76 errorMsg = self.checkLoginAndPassword(login, password) 74 77 if errorMsg: 75 cpg.response.body = loginScreen(fromPage, login = login, errorMsg = errorMsg)78 cpg.response.body = self.loginScreen(fromPage, login = login, errorMsg = errorMsg) 76 79 else: 77 80 cpg.request.sessionMap['username'] = login 81 if not fromPage: 82 fromPage = '/' 78 83 cpg.response.body = httptools.redirect(fromPage) 79 84 return … … 83 88 self.notLoggedIn() 84 89 if not cpg.request.sessionMap.get('username'): 85 cpg.response.body = loginScreen(cpg.request.browserUrl)90 cpg.response.body = self.loginScreen(cpg.request.browserUrl) 86 91 return 87 92 branches/ooconf/cherrypy/lib/filter/sessionfilter/basesession.py
r283 r299 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 = ''100 for i in range(50):101 s += random.choice(string.letters+string.digits)102 s += '%s'%time.time()103 newKey = sha.sha(s).hexdigest()104 105 return newKey106 107 94 def loadSession(self, sessionKey, autoCreate = True): 108 95 cpg = cherrypy.cpg … … 156 143 del self.__sessionCache[session.key] 157 144 158 def dropSession(self, sessionKey):159 self.delSession()160 """ delete a session from storage """161 162 def cleanUpOldSessions(self):163 """This function cleans up expired sessions"""164 branches/ooconf/cherrypy/lib/filter/sessionfilter/dbmsession.py
r280 r299 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): branches/ooconf/cherrypy/lib/filter/sessionfilter/filesession.py
r282 r299 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): branches/ooconf/cherrypy/lib/filter/sessionfilter/ramsession.py
r275 r299 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 branches/ooconf/cherrypy/lib/filter/sessionfilter/sqlobjectsession.py
r284 r299 49 49 if attr in ['timestamp', 'timeout', 'lastAccess', 'key']: 50 50 return getattr(self.__sqlObject, self.__attrSub(attr)) 51 else: 52 return object.__getattr__(self, attr) 51 53 52 54 def __setattr__(self, attr, value): … … 112 114 def cleanUpOldSessions(self): 113 115 # running a single query would be a huge performace boost 114 for sessionKey in __data: 115 session = sessionData[sessionKey] 116 if session.expired(): 117 self.delSession(sessionKey) 118 119 ''' 120 Session.createTable() 121 p = Session(session_key = 'abcd', timestamp = 330, count = 123) 122 Session.delete(Session.q.session_key=='abcd') 123 124 smanage = SQLObjectSession('test') 125 print smanage.getSession('abcd') 126 ''' 116 pass branches/ooconf/cherrypy/test/test_logdebuginfo_filter.py
r267 r299 60 60 self.assert_('Build time' in cpg.response.body) 61 61 self.assert_('Page size' in cpg.response.body) 62 self.assert_('Session data size' in cpg.response.body) 62 # not compatible with the sessionFilter 63 #self.assert_('Session data size' in cpg.response.body) 63 64 64 65 branches/ooconf/cherrypy/tutorial/tut10_sessionfilter.conf
r275 r299 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' branches/ooconf/cherrypy/tutorial/tut10_sessionfilter.py
r279 r299 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

