<commit_before>#!/usr/bin/env python
import os
import sys
import flask_migrate
import click

from zou.app.utils import dbhelpers, auth, commands
from zou.app.services import (
    persons_service,
    tasks_service
)


@click.group()
def cli():
    pass


@cli.command()
def init_db():
    "Creates datababase table (database must be created through PG client)."

    print("Creating database and tables...")
    from zou.app import app
    with app.app_context():
        import zou
        directory = os.path.join(
            os.path.dirname(zou.__file__), "migrations"
        )
        flask_migrate.upgrade(directory=directory)
    print("Database and tables created.")


@cli.command()
def clear_db():
    "Drop all tables from database"

    print("Deleting database and tables...")
    dbhelpers.drop_all()
    print("Database and tables deleted.")


@cli.command()
def reset_db():
    "Drop all tables then recreates them."

    clear_db()
    dbhelpers.create_all()


@cli.command()
def upgrade_db():
    "Upgrade database schema."

    from zou.app import app
    with app.app_context():
        import zou
        directory = os.path.join(
            os.path.dirname(zou.__file__), "migrations"
        )
        flask_migrate.upgrade(directory=directory)


@cli.command()
def stamp_db():
    "Upgrade database schema."

    from zou.app import app
    with app.app_context():
        import zou
        directory = os.path.join(
            os.path.dirname(zou.__file__), "migrations"
        )
        flask_migrate.stamp(directory=directory)


@cli.command()
@click.argument("email")
def create_admin(email):
    "Create an admin user to allow usage of the API when database is empty."
    "Set password is '<PASSWORD>'."

    try:
        # Allow "<EMAIL>" to be invalid.
        if email != "<EMAIL>":
            auth.validate_email(email)
        password = auth.encrypt_password("<PASSWORD>")
        persons_service.create_person(
            email,
            password,
            "<PASSWORD>",
            "<PASSWORD>",
            role="admin"
        )
        print("Admin successfully created.")

    except auth.PasswordsNoMatchException:
        print("Passwords don't match.")
        sys.exit(1)
    except auth.PasswordTooShortException:
        print("Password is too short.")
        sys.exit(1)
    except auth.EmailNotValidException:
        print("Email is not valid.")
        sys.exit(1)


@cli.command('clean_auth_tokens')
def clean_auth_tokens():
    "Remove revoked and expired tokens."
    commands.clean_auth_tokens()


@cli.command('clear_all_auth_tokens')
def clear_all_auth_tokens():
    "Remove all authentication tokens."
    commands.clean_auth_tokens()


@cli.command('init_data')
def init_data():
    commands.init_data()


@cli.command()
def patch_set_done_flag_on_task_status():
    """
    Patch to run after upgrade from 0.4.8 or lower to 0.4.9 or superior.
    """
    for task_status in tasks_service.get_task_statuses():
        tasks_service.update_task_status(task_status["id"], {
            "is_done": task_status["short_name"] == "done"
        })


@cli.command()
def patch_scene_asset_instance():
    """
    Patch to run after upgrade from 0.6.1 or lower to 0.6.2 or superior.
    It concerns only casting based on instances.
    """
    from zou.app.models.asset_instance import AssetInstance
    for asset_instance in AssetInstance.query.all():
        try:
            asset_instance.update({
                "scene_id": asset_instance.entity_id
            })
        except:
            print(asset_instance.serialize())


if __name__ == '__main__':
    cli()<commit_msg>Add patch to set default value to allow timelog flag<commit_after>#!/usr/bin/env python
import os
import sys
import flask_migrate
import click

from zou.app.utils import dbhelpers, auth, commands
from zou.app.services import (
    persons_service,
    tasks_service
)


@click.group()
def cli():
    pass


@cli.command()
def init_db():
    "Creates datababase table (database must be created through PG client)."

    print("Creating database and tables...")
    from zou.app import app
    with app.app_context():
        import zou
        directory = os.path.join(
            os.path.dirname(zou.__file__), "migrations"
        )
        flask_migrate.upgrade(directory=directory)
    print("Database and tables created.")


@cli.command()
def migrate_db():
    "Upgrade database schema."

    from zou.app import app
    with app.app_context():
        import zou
        directory = os.path.join(
            os.path.dirname(zou.__file__), "migrations"
        )
        flask_migrate.migrate(directory=directory)


@cli.command()
def clear_db():
    "Drop all tables from database"

    print("Deleting database and tables...")
    dbhelpers.drop_all()
    print("Database and tables deleted.")


@cli.command()
def reset_db():
    "Drop all tables then recreates them."

    clear_db()
    dbhelpers.create_all()


@cli.command()
def upgrade_db():
    "Upgrade database schema."

    from zou.app import app
    with app.app_context():
        import zou
        directory = os.path.join(
            os.path.dirname(zou.__file__), "migrations"
        )
        flask_migrate.upgrade(directory=directory)


@cli.command()
def stamp_db():
    "Upgrade database schema."

    from zou.app import app
    with app.app_context():
        import zou
        directory = os.path.join(
            os.path.dirname(zou.__file__), "migrations"
        )
        flask_migrate.stamp(directory=directory)


@cli.command()
@click.argument("email")
def create_admin(email):
    "Create an admin user to allow usage of the API when database is empty."
    "Set password is '<PASSWORD>'."

    try:
        # Allow "<EMAIL>" to be invalid.
        if email != "<EMAIL>":
            auth.validate_email(email)
        password = auth.encrypt_password("<PASSWORD>")
        persons_service.create_person(
            email,
            password,
            "<PASSWORD>",
            "Admin",
            role="admin"
        )
        print("Admin successfully created.")

    except auth.PasswordsNoMatchException:
        print("Passwords don't match.")
        sys.exit(1)
    except auth.PasswordTooShortException:
        print("Password is too short.")
        sys.exit(1)
    except auth.EmailNotValidException:
        print("Email is not valid.")
        sys.exit(1)


@cli.command('clean_auth_tokens')
def clean_auth_tokens():
    "Remove revoked and expired tokens."
    commands.clean_auth_tokens()


@cli.command('clear_all_auth_tokens')
def clear_all_auth_tokens():
    "Remove all authentication tokens."
    commands.clean_auth_tokens()


@cli.command('init_data')
def init_data():
    commands.init_data()


@cli.command()
def patch_set_done_flag_on_task_status():
    """
    Patch to run after upgrade from 0.4.8 or lower to 0.4.9 or superior.
    """
    for task_status in tasks_service.get_task_statuses():
        tasks_service.update_task_status(task_status["id"], {
            "is_done": task_status["short_name"] == "done"
        })


@cli.command()
def patch_scene_asset_instance():
    """
    Patch to run after upgrade from 0.6.1 or lower to 0.6.2 or superior.
    It concerns only casting based on instances.
    """
    from zou.app.models.asset_instance import AssetInstance
    for asset_instance in AssetInstance.query.all():
        try:
            asset_instance.update({
                "scene_id": asset_instance.entity_id
            })
        except:
            print(asset_instance.serialize())


@cli.command()
def patch_task_type_allow_timelog():
    """
    Patch to run after upgrade from 0.7.3 or lower to 0.7.4 or superior.
    """
    from zou.app.models.task_type import TaskType
    for task_type in TaskType.query.all():
        try:
            task_type.update({
                "allow_timelog": True
            })
        except:
            print(task_type.serialize())


if __name__ == '__main__':
    cli()
{'fraction_non_alphanumeric': 0.11119430924747285, 'fraction_numerical': 0.004492699363534257, 'mean_word_length': 2.596947935368043, 'pattern_counts': {'":': 7, '<': 14, '<?xml version=': 0, '>': 14, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '42536', 'n_tokens_mistral': 2451, 'n_tokens_neox': 2396, 'n_words': 605}