Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def load(self):
- dd = self._session_key.split("--")
- # make sure we got both elements
- if len(dd) == 2:
- data = re.sub('%3D','=',dd[0])
- # now make sure digest is valid
- if dd[1] == self.generate_digest(data):
- # valid. decode and load data
- decoded_data = base64.b64decode(data)
- # First load with YAML, if there is a YAML syntax error, then load with JSON
- try:
- print "trying yaml..."
- obj = yaml.load(decoded_data)
- except ValueError:
- print "trying json..."
- obj = simplejson.loads(decoded_data)
- except:
- print "Couldn't load data. A new session will be created."
- obj = False
- if obj:
- print "got object"
- # intercept _session_expiry
- if obj.has_key("_session_expiry"):
- obj['_session_expiry'] = datetime.datetime.fromtimestamp(int(obj['_session_expiry']))
- return obj
- else:
- # if we get here, it was invalid and we should generate a new session
- self.create()
- return {}
- def _get_session_key(self):
- obj = getattr(self, '_session_cache', {})
- # intercept _session_expiry
- if obj.has_key("_session_expiry") and isinstance(obj['_session_expiry'],datetime.datetime):
- obj['_session_expiry'] = obj['_session_expiry'].strftime("%s")
- # add session_id if it's not present
- if not obj.has_key("session_id"):
- obj['session_id'] = rand.bytes(16).encode('hex_codec')
- # Dump to YAML, then encode as base64
- enc = base64.b64encode(yaml.dump(obj))
- return "--".join([re.sub('=','%3D',enc),self.generate_digest(enc)])
Add Comment
Please, Sign In to add comment