def as_dict(self) -> dict: """Return a representation of the user data as a dict""" return { 'username': self.username, 'user_type': self.user_type, 'external_id': self.external_id, 'created_at': timestamp.isoformat(self.created_at), 'last_seen_at': timestamp.isoformat(self.last_seen_at), 'email_address': self.email_address, 'display_name': self.display_name, 'groups': [g.name for g in self.groups], 'password': self._application.encrypt_value('password', self.password), 'permissions': self.permissions, 'last_refreshed_at': timestamp.isoformat(self.last_refreshed_at) }
def __init__(self, handler: web.RequestHandler) -> None: self._handler = handler self.authenticated = False self.id = self._get_id_from_cookie() or str(uuid.uuid4()) self.last_save = None self.start = timestamp.isoformat() self.user = None
async def add_duration(self, key, value): """Add a duration for the specified key :param str key: The value name :param float value: The value """ await self._pool.lpush('d:{}'.format(key), '{},{}'.format(timestamp.isoformat(), value))
async def clear(self) -> None: """Clear out the currently loaded session by clearing the cookie and removing the data from redis. """ LOGGER.debug('Deleting session %s', self.id) self._handler.clear_cookie('session') await self._redis.delete(self._redis_key) self.authenticated = False self.last_save = None self.start = timestamp.isoformat() self.user = None
async def save(self): """Save session data to redis""" LOGGER.debug('Saving session %s', self.id) user_data = {} if not self.user else self.user.as_dict() await self._redis.set(self._redis_key, json.dumps({ 'user': user_data, 'last_save': timestamp.isoformat(), 'start': self.start }), expire=self.TTL) self._handler.set_secure_cookie('session', self.id)
def test_format_and_parse(self): expectation = '2016-08-26 13:46:34-04:00' parsed = timestamp.parse(expectation) self.assertIsInstance(parsed, datetime.datetime) self.assertEqual(expectation, timestamp.isoformat(parsed))
def test_utcnow(self): self.assertEqual( timestamp.isoformat(timestamp.utcnow()), timestamp.isoformat( datetime.datetime.now(tz=tz.tzoffset(None, 0))))
def test_isoformat_now(self): expectation = \ datetime.datetime.utcnow().isoformat(' ').split('.')[0] + '+00:00' self.assertEqual(expectation, timestamp.isoformat())