示例#1
0
async def gen_api_key(user_id, *, request=None, sessions=None, **kwargs):
    if user_id:
        kwargs['uid'] = str(user_id)

    old_key = get_api_key(request)

    if old_key:
        full_key = redis_key(request.app.name, old_key, 'session')
        u = await sessions.hgetall(full_key)
        u = {k.decode(): v.decode() for k, v in u.items()}
        if 'uid' in u:  # not anon
            raise exceptions.HTTPConflict()
        u.update(kwargs)
        api_key = old_key
    else:
        u = kwargs
        u['c'] = utils.now(ts=True)
        api_key = str(uuid.uuid4())
        full_key = redis_key(request.app.name, api_key, 'session')

    pairs = functools.reduce(operator.add, u.items())
    await sessions.hmset(full_key, *pairs)

    request.api_key = api_key
    return api_key
示例#2
0
 async def delete_account(self, connection=None):
     self.date_deleted = utils.now()
     self.is_active = False
     # Add random string to email to allow new registration with such address
     self.email = '#'.join((self.email[:230], get_random_string()))
     await self.save(fields=['email', 'date_deleted', 'is_active'],
                     connection=connection)
示例#3
0
    def set_defaults(cls, data: dict):
        data.setdefault('date_joined', utils.now())
        data.setdefault('is_active', False)
        data.setdefault('is_superuser', False)
        data.setdefault('is_staff', False)
        data.setdefault('first_name', '')
        data.setdefault('last_name', '')

        if 'password' not in data:
            password = ''.join(
                random.sample(string.ascii_letters + string.digits, 10))
            data['password'] = make_password(password)
示例#4
0
 def set_defaults(cls, data: dict):
     data.setdefault('created_at', utils.now())
示例#5
0
 def set_defaults(cls, data: dict):
     data.setdefault('uuid', uuid.uuid4())
     data.setdefault('created_at', utils.now())
     data.setdefault('status', UserConfirmationRequestStatus.sent.value)
     data['updated_at'] = utils.now()
示例#6
0
 async def on_login(self, connection=None):
     # Update login timestamp
     self.last_login = utils.now()
     await self.save(fields=['last_login'], connection=connection)