Ejemplo n.º 1
0
async def reset_test_db():
    from tortoise import Tortoise
    from tortoise.backends.asyncpg import AsyncpgDBClient

    await Tortoise.close_connections()

    for cn in config.tortoise_config()['connections']:
        config.tortoise_config()['connections'][cn]['credentials']['database'] = 'test_' + config.tortoise_config()[
            'connections'][cn]['credentials']['database']

    conn_name = 'conn_users'
    db_users = AsyncpgDBClient(
        connection_name=conn_name,
        **config.tortoise_config()['connections'][conn_name]['credentials']
    )

    await db_users.create_connection(with_db=False)
    await db_users.db_delete()
    await db_users.db_create()
    await db_users.close()

    await Tortoise.init(
        config=config.tortoise_config()
    )
    await Tortoise.generate_schemas()
async def seed_users(args):

    config.load_from_yaml(
        f'{_root}/config/config.{os.getenv("ENVIRONMENT", "local")}.yaml')
    await Tortoise.init(config=config.tortoise_config())

    _exists = await models.users.AuthUser.all().count()
    if _exists:
        # do not add seed
        return

    _file = f'{_current_path}/{args.file}'
    with open(_file) as f:
        csv_reader = csv.reader(f)
        for csv_user in csv_reader:
            print(csv_user)

            auth_user = models.AuthUser()
            auth_user.username = csv_user[0]
            auth_user.password = csv_user[1]
            auth_user.role_flags = int(csv_user[2])
            # on new users platform user's roles are changed ADMIN -> SUPERUSER
            if auth_user.role_flags == user_roles.ADMIN and auth_user.username != 'admin':
                auth_user.role_flags = user_roles.SUPERUSER
            if auth_user.username == '*****@*****.**':
                auth_user.role_flags = user_roles.ADMIN

            auth_user.scopes = {'OPENWASTE': 3, 'OPENLIGHT': 3}
            await auth_user.save()

            user = models.User()
            user.auth_user = auth_user
            user.first_name = csv_user[3]
            user.last_name = csv_user[4]
            user.email = csv_user[5]
            user.alarm_type = int(csv_user[6])
            user.notification_type = int(csv_user[7])
            user.phone = PHONES[
                auth_user.
                username]['number'] if auth_user.username in PHONES else None
            await user.save()
Ejemplo n.º 3
0
async def main():

    f = Faker()
    config.load_from_yaml(
        f'{_root}/config/config.{os.getenv("ENVIRONMENT", "local")}.yaml')
    await Tortoise.init(config=config.tortoise_config())

    _exists = await models.users.AuthUser.all().count()
    if _exists:
        # do not add seed
        return

    _roles = [
        (user_roles.USER, 10),
        (user_roles.ADMIN, 5),
        (user_roles.DEVELOPER, 2),
        (user_roles.SUPERUSER, 2),
    ]
    for role in _roles:
        for _ in range(role[1]):
            auth_user = models.AuthUser()
            auth_user.username = f.user_name()
            auth_user.password = format_password(auth_user.username, '123')
            auth_user.role_flags = role[0]
            auth_user.scopes = {'OPENWASTE': 3, 'OPENLIGHT': 3}
            await auth_user.save()

            user = models.User()
            user.auth_user = auth_user
            user.first_name = f.first_name()
            user.last_name = f.last_name()
            user.email = f.email()
            user.alarm_type = 1
            user.notification_type = 1
            user.phone = f.phone_number()
            await user.save()