open-law/app/commands/__init__.py

39 lines
1.2 KiB
Python

import click
from flask import Flask
from app import models as m
from app import db, forms
from app import schema as s
def init(app: Flask):
# flask cli context setup
@app.shell_context_processor
def get_context():
"""Objects exposed here will be automatically available from the shell."""
return dict(app=app, db=db, m=m, f=forms, s=s)
if app.config["ENV"] != "production":
@app.cli.command()
@click.option("--count", default=100, type=int)
def db_populate(count: int):
"""Fill DB by dummy data."""
from tests.db import populate
populate(count)
print(f"DB populated by {count} instancies")
@app.cli.command("create-admin")
def create_admin():
"""Create super admin account"""
if m.User.query.filter_by(email=app.config["ADMIN_EMAIL"]).first():
print(f"User with e-mail: [{app.config['ADMIN_EMAIL']}] already exists")
return
m.User(
username=app.config["ADMIN_USERNAME"],
email=app.config["ADMIN_EMAIL"],
password=app.config["ADMIN_PASSWORD"],
).save()
print("admin created")