diff --git a/canaille/app/commands.py b/canaille/app/commands.py index 7d9abf66..1b36516b 100644 --- a/canaille/app/commands.py +++ b/canaille/app/commands.py @@ -2,18 +2,9 @@ import functools import sys import click -from canaille import create_app -from canaille.oidc.models import AuthorizationCode -from canaille.oidc.models import Token from flask import current_app -from flask.cli import FlaskGroup from flask.cli import with_appcontext -try: - HAS_FAKER = True -except ImportError: # pragma: no cover - HAS_FAKER = False - def with_backendcontext(func): @functools.wraps(func) @@ -36,34 +27,14 @@ def with_backendcontext(func): return _func -@click.group(cls=FlaskGroup, create_app=create_app) -def cli(): - pass - - -@cli.command() -@with_appcontext -@with_backendcontext -def clean(): - """ - Remove expired tokens and authorization codes. - """ - for t in Token.query(): - if t.is_expired(): - t.delete() - - for a in AuthorizationCode.query(): - if a.is_expired(): - a.delete() - - -@cli.command() +@click.command() @with_appcontext def check(): """ Check the configuration file. """ - from canaille.app.configuration import validate, ConfigurationException + from canaille.app.configuration import ConfigurationException + from canaille.app.configuration import validate try: validate(current_app.config, validate_remote=True) @@ -72,7 +43,7 @@ def check(): sys.exit(1) -@cli.command() +@click.command() @with_appcontext def install(): """ @@ -89,47 +60,6 @@ def install(): sys.exit(1) -if HAS_FAKER: # pragma: no branch - - @cli.group() - @click.option("--nb", default=1, help="Number of items to create") - @click.pass_context - @with_appcontext - def populate(ctx, nb): - """ - Populate the database with generated random data. - """ - ctx.ensure_object(dict) - - ctx.obj["number"] = nb - - @populate.command() - @click.pass_context - @with_appcontext - @with_backendcontext - def users(ctx): - """ - Populate the database with generated random users. - """ - - from canaille.core.populate import fake_users - - fake_users(ctx.obj["number"]) - - @populate.command() - @click.pass_context - @click.option( - "--nb-users-max", - default=1, - help="The maximum number of users that will randomly be affected in the group", - ) - @with_appcontext - @with_backendcontext - def groups(ctx, nb_users_max): - """ - Populate the database with generated random groups. - """ - - from canaille.core.populate import fake_groups - - fake_groups(ctx.obj["number"], nb_users_max) +def register(cli): + cli.add_command(check) + cli.add_command(install) diff --git a/canaille/commands.py b/canaille/commands.py new file mode 100644 index 00000000..c7aeff5f --- /dev/null +++ b/canaille/commands.py @@ -0,0 +1,16 @@ +import canaille.app.commands +import canaille.core.commands +import canaille.oidc.commands +import click +from canaille import create_app +from flask.cli import FlaskGroup + + +@click.group(cls=FlaskGroup, create_app=create_app) +def cli(): + pass # pragma: no cover + + +canaille.app.commands.register(cli) +canaille.core.commands.register(cli) +canaille.oidc.commands.register(cli) diff --git a/canaille/core/commands.py b/canaille/core/commands.py new file mode 100644 index 00000000..6ef1b39f --- /dev/null +++ b/canaille/core/commands.py @@ -0,0 +1,59 @@ +import click +from canaille.app.commands import with_backendcontext +from flask.cli import with_appcontext + +try: + HAS_FAKER = True +except ImportError: # pragma: no cover + HAS_FAKER = False + + +@click.group() +@click.option("--nb", default=1, help="Number of items to create") +@click.pass_context +@with_appcontext +def populate(ctx, nb): + """ + Populate the database with generated random data. + """ + ctx.ensure_object(dict) + + ctx.obj["number"] = nb + + +@populate.command() +@click.pass_context +@with_appcontext +@with_backendcontext +def users(ctx): + """ + Populate the database with generated random users. + """ + + from canaille.core.populate import fake_users + + fake_users(ctx.obj["number"]) + + +@populate.command() +@click.pass_context +@click.option( + "--nb-users-max", + default=1, + help="The maximum number of users that will randomly be affected in the group", +) +@with_appcontext +@with_backendcontext +def groups(ctx, nb_users_max): + """ + Populate the database with generated random groups. + """ + + from canaille.core.populate import fake_groups + + fake_groups(ctx.obj["number"], nb_users_max) + + +def register(cli): + if HAS_FAKER: # pragma: no branch + cli.add_command(populate) diff --git a/canaille/oidc/commands.py b/canaille/oidc/commands.py new file mode 100644 index 00000000..31f8d496 --- /dev/null +++ b/canaille/oidc/commands.py @@ -0,0 +1,25 @@ +import click +from canaille.app.commands import with_backendcontext +from canaille.oidc.models import AuthorizationCode +from canaille.oidc.models import Token +from flask.cli import with_appcontext + + +@click.command() +@with_appcontext +@with_backendcontext +def clean(): + """ + Remove expired tokens and authorization codes. + """ + for t in Token.query(): + if t.is_expired(): + t.delete() + + for a in AuthorizationCode.query(): + if a.is_expired(): + a.delete() + + +def register(cli): + cli.add_command(clean) diff --git a/tests/app/commands/test_check.py b/tests/app/commands/test_check.py index 84cf2d0d..d9abf194 100644 --- a/tests/app/commands/test_check.py +++ b/tests/app/commands/test_check.py @@ -1,4 +1,4 @@ -from canaille.app.commands import cli +from canaille.commands import cli def test_check_command(testclient): diff --git a/tests/core/commands/test_populate.py b/tests/core/commands/test_populate.py index a4b3878b..49ac69d8 100644 --- a/tests/core/commands/test_populate.py +++ b/tests/core/commands/test_populate.py @@ -1,4 +1,4 @@ -from canaille.app.commands import cli +from canaille.commands import cli from canaille.core.models import Group from canaille.core.models import User from canaille.core.populate import fake_users diff --git a/tests/oidc/commands/test_clean.py b/tests/oidc/commands/test_clean.py index 3ca2ad6a..e9c34524 100644 --- a/tests/oidc/commands/test_clean.py +++ b/tests/oidc/commands/test_clean.py @@ -1,6 +1,6 @@ import datetime -from canaille.app.commands import cli +from canaille.commands import cli from canaille.oidc.models import AuthorizationCode from canaille.oidc.models import Token from werkzeug.security import gen_salt diff --git a/tests/oidc/commands/test_install.py b/tests/oidc/commands/test_install.py index f439cb74..e864864f 100644 --- a/tests/oidc/commands/test_install.py +++ b/tests/oidc/commands/test_install.py @@ -3,8 +3,8 @@ import os import ldap import pytest from canaille import create_app -from canaille.app.commands import cli from canaille.app.installation import InstallationException +from canaille.commands import cli from canaille.ldap_backend.ldapobject import LDAPObject from canaille.oidc.installation import setup_schemas from flask_webtest import TestApp