__init__.py 1.22 KB
import click
from flask import Blueprint
from app.models import db, User

bp = Blueprint('pdc_db', __name__)


@bp.cli.command('delete_user')
@click.argument('user_id')
def delete(user_id):
    user = User.query.get(user_id)
    db.session.delete(user)
    db.session.commit()


@bp.cli.command('create_db')
def create():
    db.create_all()
    admin = User(email='admin@nowhere.org', name='admin', login='admin', password='admin')
    db.session.add(admin)
    db.session.commit()


@bp.cli.command('add_user')
@click.argument('email')
@click.argument('name')
@click.argument('password')
def add(email, name, password):
    user = User(email=email, name=name, password=password)
    db.session.add(user)
    db.session.commit()
    print("added ", name)


@bp.cli.command('show_all')
def show_all():
    print("{:<5} {:<15} {:<15} {:<15} {:<15}".format('id', 'name', 'login', 'passwd', 'email'))
    print("{:<5} {:<15} {:<15} {:<15} {:<15}".format('-' * 5, '-' * 15, '-' * 15, '-' * 15, '-' * 15))
    for user in User.query.all():
        print(user.login)
        print("{:<5} {:<15} {:<15} {:<15} {:<15}".format(
            user.id,
            user.name,
            user.login,
            user.password,
            user.email
        ))