MonScoDocEssai/scodoc.py

76 lines
1.6 KiB
Python
Raw Normal View History

2021-05-29 18:22:51 +02:00
# -*- coding: UTF-8 -*
"""Application Flask: ScoDoc
"""
from __future__ import print_function
from pprint import pprint as pp
import click
import flask
from app import create_app, cli, db
from app.auth.models import User, Role, UserRole
from config import Config
app = create_app()
cli.register(app)
@app.shell_context_processor
def make_shell_context():
return {
"db": db,
"User": User,
"Role": Role,
"UserRole": UserRole,
"pp": pp,
"flask": flask,
"current_app": flask.current_app,
"cleardb": _cleardb,
}
@app.cli.command()
def inituserdb():
"""Initialize the users database."""
click.echo("Init the db")
# Create roles:
Role.insert_roles()
click.echo("created initial roles")
# Ensure that admin exists
if Config.SCODOC_ADMIN_MAIL:
admin_username = Config.SCODOC_ADMIN_LOGIN
user = User.query.filter_by(username=admin_username).first()
if not user:
user = User(username=admin_username, email=Config.SCODOC_ADMIN_MAIL)
db.session.add(user)
db.session.commit()
click.echo(
"created initial admin user, login: {u.username}, email: {u.email}".format(
u=user
)
)
@app.cli.command()
def clearuserdb():
"""Erase (drop) all tables of users database !"""
click.echo("Erasing the db !")
_cleardb()
def _cleardb():
"""Erase (drop) all tables of users database !"""
db.reflect()
db.drop_all()
db.session.commit()