canaille-globuzma/canaille/commands.py

73 lines
1.5 KiB
Python
Raw Normal View History

2021-10-13 08:12:44 +00:00
import sys
2021-10-13 07:38:44 +00:00
2021-12-20 22:57:27 +00:00
import click
2021-10-13 07:38:44 +00:00
from canaille import create_app
2022-01-11 18:49:06 +00:00
from canaille.oidc.models import AuthorizationCode
from canaille.oidc.models import Token
2021-10-13 07:38:44 +00:00
from flask import current_app
2021-12-20 22:57:27 +00:00
from flask.cli import FlaskGroup
from flask.cli import with_appcontext
2021-10-13 07:38:44 +00:00
@click.group(cls=FlaskGroup, create_app=create_app)
def cli():
pass
2021-10-13 07:38:44 +00:00
@cli.command()
@with_appcontext
def clean():
"""
Remove expired tokens and authorization codes.
"""
from canaille.ldap_backend.backend import (
setup_backend,
teardown_backend,
)
if not current_app.config["TESTING"]:
setup_backend(current_app)
for t in Token.all():
if t.is_expired():
t.delete()
for a in AuthorizationCode.all():
if a.is_expired():
a.delete()
if not current_app.config["TESTING"]:
teardown_backend(current_app)
2021-10-13 08:12:44 +00:00
@cli.command()
@with_appcontext
def check():
"""
Check the configuration file.
"""
from canaille.configuration import validate, ConfigurationException
2021-10-29 12:20:06 +00:00
2021-10-13 08:12:44 +00:00
try:
validate(current_app.config, validate_remote=True)
except ConfigurationException as exc:
print(exc)
sys.exit(1)
2021-11-08 17:09:05 +00:00
@cli.command()
@with_appcontext
def install():
"""
2021-11-08 17:30:31 +00:00
Installs canaille elements from the configuration.
2021-11-08 17:09:05 +00:00
"""
from canaille.installation import install
2021-11-13 18:11:56 +00:00
from canaille.configuration import ConfigurationException
2021-11-08 17:09:05 +00:00
try:
install(current_app.config)
except ConfigurationException as exc:
print(exc)
sys.exit(1)