def setCookie(self, key, value, maxAge=None, Expires=None): cookie = Cookie(key, value) if maxAge: cookie.setMaxAge(maxAge) if Expires: cookie.setExpires(Expires) self.response().addCookie(cookie)
def setCookie(self, name, value, path='/', expires='ONCLOSE', secure=False): """ Set a cookie. You can also set the path (which defaults to /), You can also set when it expires. It can expire: 'NOW': this is the same as trying to delete it, but it doesn't really seem to work in IE 'ONCLOSE': the default behavior for cookies (expires when the browser closes) 'NEVER': some time in the far, far future. integer: a timestamp value tuple: a tuple, as created by the time module @@ sgd 2/5/2003 - removed optional DateTime for 0.8 release. Use of DateTime in this module has been broken for 3 months and the fix has not been in any of the beta releases. Support may be implemented in a future release. DateTime: an mxDateTime object for the time DeltaDateTime: a interval from the present, e.g., DateTime.DeltaDateTime(month=1) (1 month in the future) '+...': a time in the future, '...' should be something like 1w (1 week), 3h46m (3:45), etc. You can use y (year), b (month), w (week), d (day), h (hour), m (minute), s (second). This is done by the MiscUtils.DateInterval. """ cookie = Cookie(name, value) if expires == 'ONCLOSE' or not expires: pass # this is already default behavior elif expires == 'NOW' or expires == 'NEVER': t = time.gmtime(time.time()) if expires == 'NEVER': t = (t[0] + 10,) + t[1:] t = time.strftime("%a, %d-%b-%Y %H:%M:%S GMT", t) cookie.setExpires(t) else: t = expires if type(t) is StringType and t and t[0] == '+': interval = timeDecode(t[1:]) t = time.time() + interval if type(t) in (IntType, LongType,FloatType): t = time.gmtime(t) if type(t) in (TupleType, TimeTupleType): t = time.strftime("%a, %d-%b-%Y %H:%M:%S GMT", t) if DateTime and type(t) in \ (DateTime.DeltaDateTimeType, DateTime.RelativeDateTimeType): t = DateTime.now() + t if DateTime and type(t) is DateTime.DateTimeType: t = t.strftime("%a, %d-%b-%Y %H:%M:%S GMT") cookie.setExpires(t) if path: cookie.setPath(path) if secure: cookie.setSecure(secure) self.addCookie(cookie)
def delCookie(self, name): """ Deletes a cookie at the browser. To do so, one has to create and send to the browser a cookie with parameters that will cause the browser to delete it. """ if self._cookies.has_key(name): self._cookies[name].delete() else: cookie = Cookie(name, None) cookie.delete() self.addCookie(cookie)
def recordSession(self): """ Invoked by commit() to record the session id in the response (if a session exists). This implementation sets a cookie for that purpose. For people who don't like sweets, a future version could check a setting and instead of using cookies, could parse the HTML and update all the relevant URLs to include the session id (which implies a big performance hit). Or we could require site developers to always pass their URLs through a function which adds the session id (which implies pain). Personally, I'd rather just use cookies. You can experiment with different techniques by subclassing Session and overriding this method. Just make sure Application knows which "session" class to use. """ sess = self._transaction._session if debug: prefix = '>> recordSession:' if sess: cookie = Cookie('_SID_', sess.identifier()) cookie.setPath('/') if sess.isExpired() or sess.timeout() == 0: # Invalid -- tell client to forget the cookie. cookie.setMaxAge(0) cookie.setExpires(-365*24*60*60) self.addCookie(cookie) if debug: print prefix, 'setting sid =', sess.identifier() else: if debug: print prefix, 'did not set sid'
def page(self): loginerror = None if self.session.__session__.hasValue( 'username') and users.__contains__(str(self.session.username)): userid = users.index(str(self.session.username)) else: self.session.username = '******' userid = users.index(str(self.session.username)) if self.taskcomplete: log.log(self, 'taskcomplete', self.action, userid=userid, ipaddr=self.ip, query=str(self.query), sort=self.sort, groupby=self.group, facet=self.facet, count=self.count, offset=self.offset, indx=self.index, item=self.item, prevurl=self.prevurl) return div(self.taskcompleted()) if self.logout: self.session.history = [] self.session.facets = '' self.session.sortby = '' self.session.attrs = '' self.session.username = '******' cookie = Cookie('username', 'default') cookie.setPath('/') # return self.request.cookies() t = time.gmtime(time.time()) t = (t[0] - 10, ) + t[1:] t = time.strftime("%a, %d-%b-%Y %H:%M:%S GMT", t) cookie.setExpires(t) self.response.addCookie(cookie) log.log(self, 'logoutwindow', self.action, userid=userid, ipaddr=self.ip, query=str(self.query), sort=self.sort, groupby=self.group, facet=self.facet, count=self.count, offset=self.offset, indx=self.index, item=self.item, prevurl=self.prevurl) #return div(self.opening(), c='opening') return div(self.logoutwindow()) #check for new account creation elif self.form.get('createaccount', '') and not self.form.get('accountformfilled', ''): log.log(self, 'notloggedin', 'accountformfilled=0', userid=userid, ipaddr=self.ip, query=str(self.query), sort=self.sort, groupby=self.group, facet=self.facet, count=self.count, offset=self.offset, indx=self.index, item=self.item, prevurl=self.prevurl) return div(self.notloggedin(post=self.url( manage='1' and self.form.get('manage', '') or None, managestart='1' and self.form.get('managestart', '') or None)), c='popupwindow') #check if its a login/create request elif self.form.get('username', ''): if self.form.get('createaccount', ''): creationerror = '' #create account request if self.form.get('accountformfilled', ''): name, password = '', '' if not (self.form.get('username', '') and \ self.form.get('password', '') and \ self.form.get('password2', '') and \ self.form.get('email', '')): creationerror = 'Error: Please fill in all fields' else: name = self.form.get('username', '') if users.__contains__(name): creationerror = 'Error: That username is already taken' if not self.form.get('password', '') == \ self.form.get('password2', ''): creationerror = 'Error: Passwords did not match' elif len(name) < 4: creationerror = 'Error: Name too short' elif len(name) > 12: creationerror = 'Error: Name too long' #passes error checking. create new user if creationerror == '': idx = users.new() users[idx].name = name users[idx].password = self.form.get('password', '') users[idx].email = self.form.get('email', '') print "passes errory checking; create new user" print self.form.get('username', '') print self.form.get('password', '') print self.form.get('password2', '') print self.form.get('email', '') else: log.log(self, 'notloggedin', creationerror, userid=userid, ipaddr=self.ip, query=str(self.query), sort=self.sort, groupby=self.group, facet=self.facet, count=self.count, offset=self.offset, indx=self.index, item=self.item, prevurl=self.prevurl) return div(self.notloggedin( creationerror=creationerror, post=self.url( manage='1' and self.form.get('manage', '') or None, managestart='1' and self.form.get('managestart', '') or None, createaccount='1' and self.form.get('createaccount', '') or None)), c='popupwindow') #is login request, check for valid user name = self.form.get('username', '') if users.__contains__(name): idx = users.index(name) password = self.form.get('password', '') print "Login request" if users[idx].password == password: self.session.username = name print self.session.username if not users.__contains__(name): if self.form.get('popuphandle', ''): #log managegame activity in FrankenMatrix #log.log(self, 'managegame', self.task, userid=userid, ipaddr=self.ip, query=str(self.query), sort=self.sort, groupby=self.group, facet=self.facet, count=self.count, offset=self.offset, indx=self.index, item=self.item, prevurl=self.prevurl, detail='login request') return div( self.managegame(post=self.url(manage='1')), c='managegame') else: idx = users.index(name) if users[idx].password == password: self.session.username = name if users[idx].remember == '1': name = self.form.get('username', '') cookie = Cookie('username', name) cookie.setPath('/') t = time.gmtime(time.time()) t = (t[0] + 10, ) + t[1:] t = time.strftime("%a, %d-%b-%Y %H:%M:%S GMT", t) cookie.setExpires(t) self.response.addCookie(cookie) else: #existing user, wrong password error = 'Error: Incorrect password for given username' self.session.username = '******' if self.form.get('createaccount', ''): log.log(self, 'notloggedin', error, userid=userid, ipaddr=self.ip, query=str(self.query), sort=self.sort, groupby=self.group, facet=self.facet, count=self.count, offset=self.offset, indx=self.index, item=self.item, prevurl=self.prevurl) return div(self.notloggedin(post=self.url( manage='1' and self.form.get('manage', '') or None, managestart='1' and self.form.get('managestart', '') or None)), c='popupwindow') log.log(self, 'notloggedin', error, userid=userid, ipaddr=self.ip, query=str(self.query), sort=self.sort, groupby=self.group, facet=self.facet, count=self.count, offset=self.offset, indx=self.index, item=self.item, prevurl=self.prevurl) return div(self.notloggedin(post=self.url( manage='1' and self.form.get('manage', '') or None, managestart='1' and self.form.get('managestart', '') or None), loginerror=error), c='popupwindow') else: self.session.username = '******' error = 'Error: Specified username does not exist' #user doesnt exist if self.form.get('createaccount', ''): log.log(self, 'notloggedin', error, userid=userid, ipaddr=self.ip, query=str(self.query), sort=self.sort, groupby=self.group, facet=self.facet, count=self.count, offset=self.offset, indx=self.index, item=self.item, prevurl=self.prevurl) return div(self.notloggedin(post=self.url( manage='1' and self.form.get('manage', '') or None, managestart='1' and self.form.get('managestart', '') or None)), c='popupwindow') log.log(self, 'notloggedin', error, userid=userid, ipaddr=self.ip, query=str(self.query), sort=self.sort, groupby=self.group, facet=self.facet, count=self.count, offset=self.offset, indx=self.index, item=self.item, prevurl=self.prevurl) return div(self.notloggedin(post=self.url( manage='1' and self.form.get('manage', '') or None, managestart='1' and self.form.get('managestart', '') or None), loginerror=error), c='popupwindow') #add entry regardless of new user or not # dump session history # into the user_history table #entries from session history in format(item, index, timestamp) if self.session.__session__.hasValue('history'): print "Dumping session history" print self.session.history for entry in self.session.history: i = user_histories.new() user_histories[i].userid = str(idx) user_histories[i].itemidx = entry[1] user_histories[i].timestamp = entry[2] user_histories[i].item = entry[0] #user_histories[i].groupid = str(groupid) self.session.history = [] #cookie section # if self.form.get('rememberme', '') and \ # self.form.get('rememberme', '') == '1': #set the cookie # name=self.form.get('username', '') # cookie = Cookie('username', name) # cookie.setPath('/') # t=time.gmtime(time.time()) # t = (t[0]+10,) + t[1:] # t = time.strftime("%a, %d-%b-%Y %H:%M:%S GMT", t) # cookie.setExpires(t) # # self.response.addCookie(cookie) # idx=users.index(name) # users[idx].remember='1' #cookie section #cookie section #not login, not logout, check for cookie #elif self.request.hasCookie('username') and \ # self.session.__session__.hasValue('username') and \ # self.session.username == 'default': # elif self.request.hasCookie('username') and not self.session.__contains__('username'): #return self.request.cookie('username').expires() # return 'cookie detected' #cookie detected # print "Cookies" # print self.request.cookies() # self.session.username=self.request.cookies()['username'] # # if self.session.username=='': # self.session.username='******' # self.session.history=[] # self.session.facets='' # self.session.sortby='' # self.session.attrs='' #cookie section if not self.session.__session__.hasValue('username'): print "user not logged in\n\n\n" #return "user has not logged in, but is accessing protected content" self.session.username = '******' self.session.history = [] self.session.facets = '' self.session.sortby = '' self.session.attrs = '' if not users.__contains__(self.session.username): self.session.username = '******' #print "username set to default" if self.searchsave: log.log(self, 'savesearch', self.action, userid=userid, ipaddr=self.ip, query=str(self.query), sort=self.sort, groupby=self.group, facet=self.facet, count=self.count, offset=self.offset, indx=self.index, item=self.item, prevurl=self.prevurl) return div(self.searchsavewindow(), c='popupwindow') elif self.favesave: log.log(self, 'savefavorite', self.action, userid=userid, ipaddr=self.ip, query=str(self.query), sort=self.sort, groupby=self.group, facet=self.facet, count=self.count, offset=self.offset, indx=self.index, item=self.item, prevurl=self.prevurl) return div(self.favesavewindow(), c='popupwindow') #manage=1 means wants to access myflamenco, possible login #manage=1 createaccount=1 means access myflamenco after create #manage=1 createaccount=1 managestart=1 means start managegame to certain page elif self.manage: print 'entering managegame section' #as long as this is not handling a login request, we can assume #that this returns the managegame section, so set self.manage # to proper opening section if self.form.get('managestart', ''): if self.form.get('username', ''): name = self.form.get('username', '') if users.__contains__(name): idx = users.index(name) self.manage = users[idx].managegame_opening elif self.session.__contains__('username'): name = self.session.username if users.__contains__(name): idx = users.index(name) self.manage = users[idx].managegame_opening print self.manage if int(self.form.get('manage', '')) == 7: self.manage = 1 #log managegame activity in FrankenMatrix #log.log(self, 'managegame', 'manage', userid=userid, ipaddr=self.ip, # query=str(self.query), sort=self.sort, groupby=self.group, # facet=self.facet, count=self.count, offset=self.offset, # indx=self.index, item=self.item, prevurl=self.prevurl) return div(self.managegame(), c='managegame') elif self.help: log.log(self, 'helpgame', self.action, userid=userid, ipaddr=self.ip, query=str(self.query), sort=self.sort, groupby=self.group, facet=self.facet, count=self.count, offset=self.offset, indx=self.index, item=self.item, prevurl=self.prevurl) return div(self.helpgame(), c='helpgame') elif self.renamegname: if self.renamegname: name = 'renamegroup' else: name = 'newgroup' log.log(self, name, self.action, userid=userid, ipaddr=self.ip, query=str(self.query), sort=self.sort, groupby=self.group, facet=self.facet, count=self.count, offset=self.offset, indx=self.index, item=self.item, prevurl=self.prevurl) return div(self.managehandler(), c='managehandler') elif self.history: #history game log.log(self, 'historygame', self.action, userid=userid, ipaddr=self.ip, query=str(self.query), sort=self.sort, groupby=self.group, facet=self.facet, count=self.count, offset=self.offset, indx=self.index, item=self.item, prevurl=self.prevurl) return div(self.historygame(), c='historygame') elif self.index is not None or self.item: if self.form.get('morelike', ''): event = 'morelike' else: event = self.action log.log(self, 'endgame', event, userid=userid, ipaddr=self.ip, query=str(self.query), sort=self.sort, groupby=self.group, facet=self.facet, count=self.count, offset=self.offset, indx=self.index, item=self.item, prevurl=self.prevurl) return div(self.endgame(), c='endgame') elif self.query or self.action: if self.form.get('morelike', ''): event = 'morelike' else: event = self.action log.log(self, 'middle', event, userid=userid, groupby=self.group, ipaddr=self.ip, query=str(self.query), sort=self.sort, offset=self.offset, facet=self.facet, count=self.count, prevurl=self.prevurl) print 'QUERY' print self.query print self.query.serialize() return div(self.middlegame(), c='middlegame') else: print "username, apss" print self.form.get('username', '') print self.form.get('password', '') loginerror = None if self.form.get('username', ''): name = self.form.get('username', '') if not users.__contains__(name): loginerror = ' Error: Invalid Username/Password pair. Please try again' log.log(self, 'opening', self.action, userid=userid, ipaddr=self.ip, query=str(self.query), sort=self.sort, groupby=self.group, facet=self.facet, count=self.count, offset=self.offset, indx=self.index, item=self.item, prevurl=self.prevurl) return div(self.opening(loginerror=loginerror), c='opening') idx = users.index(name) if not users[idx].password == password: print 'loginerr' print self.session.username self.session.username = '******' loginerror = ' Error: Invalid Username/Password pair. Please try again' log.log(self, 'opening', self.action, userid=userid, ipaddr=self.ip, query=str(self.query), sort=self.sort, groupby=self.group, facet=self.facet, count=self.count, offset=self.offset, indx=self.index, item=self.item, prevurl=self.prevurl) return div(self.opening(loginerror=loginerror), c='opening') #self.session.username='******' log.log(self, 'opening', self.action, userid=userid, ipaddr=self.ip, query=str(self.query), sort=self.sort, groupby=self.group, facet=self.facet, count=self.count, offset=self.offset, indx=self.index, item=self.item, prevurl=self.prevurl) return div(self.opening(loginerror=loginerror), c='opening')