Split commands in their dedicated modules

This commit is contained in:
Éloi Rivard 2023-04-09 16:14:38 +02:00
parent 79f12b1d0a
commit 83f67331d3
8 changed files with 111 additions and 81 deletions

View file

@ -2,18 +2,9 @@ import functools
import sys import sys
import click 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 import current_app
from flask.cli import FlaskGroup
from flask.cli import with_appcontext from flask.cli import with_appcontext
try:
HAS_FAKER = True
except ImportError: # pragma: no cover
HAS_FAKER = False
def with_backendcontext(func): def with_backendcontext(func):
@functools.wraps(func) @functools.wraps(func)
@ -36,34 +27,14 @@ def with_backendcontext(func):
return _func return _func
@click.group(cls=FlaskGroup, create_app=create_app) @click.command()
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()
@with_appcontext @with_appcontext
def check(): def check():
""" """
Check the configuration file. Check the configuration file.
""" """
from canaille.app.configuration import validate, ConfigurationException from canaille.app.configuration import ConfigurationException
from canaille.app.configuration import validate
try: try:
validate(current_app.config, validate_remote=True) validate(current_app.config, validate_remote=True)
@ -72,7 +43,7 @@ def check():
sys.exit(1) sys.exit(1)
@cli.command() @click.command()
@with_appcontext @with_appcontext
def install(): def install():
""" """
@ -89,47 +60,6 @@ def install():
sys.exit(1) sys.exit(1)
if HAS_FAKER: # pragma: no branch def register(cli):
cli.add_command(check)
@cli.group() cli.add_command(install)
@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)

16
canaille/commands.py Normal file
View file

@ -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)

59
canaille/core/commands.py Normal file
View file

@ -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)

25
canaille/oidc/commands.py Normal file
View file

@ -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)

View file

@ -1,4 +1,4 @@
from canaille.app.commands import cli from canaille.commands import cli
def test_check_command(testclient): def test_check_command(testclient):

View file

@ -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 Group
from canaille.core.models import User from canaille.core.models import User
from canaille.core.populate import fake_users from canaille.core.populate import fake_users

View file

@ -1,6 +1,6 @@
import datetime import datetime
from canaille.app.commands import cli from canaille.commands import cli
from canaille.oidc.models import AuthorizationCode from canaille.oidc.models import AuthorizationCode
from canaille.oidc.models import Token from canaille.oidc.models import Token
from werkzeug.security import gen_salt from werkzeug.security import gen_salt

View file

@ -3,8 +3,8 @@ import os
import ldap import ldap
import pytest import pytest
from canaille import create_app from canaille import create_app
from canaille.app.commands import cli
from canaille.app.installation import InstallationException from canaille.app.installation import InstallationException
from canaille.commands import cli
from canaille.ldap_backend.ldapobject import LDAPObject from canaille.ldap_backend.ldapobject import LDAPObject
from canaille.oidc.installation import setup_schemas from canaille.oidc.installation import setup_schemas
from flask_webtest import TestApp from flask_webtest import TestApp