Blame view

app/cli/__init__.py 1.22 KB
76ef4fbe   hitier   Click, Sqlalchemy...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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()
59f42c31   hitier   Allow logout
19
    admin = User(email='admin@nowhere.org', name='admin', login='admin', password='admin')
76ef4fbe   hitier   Click, Sqlalchemy...
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
    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():
a49732b5   hitier   Add current user ...
37
38
    print("{:<5} {:<15} {:<15} {:<15} {:<15}".format('id', 'name', 'login', 'passwd', 'email'))
    print("{:<5} {:<15} {:<15} {:<15} {:<15}".format('-' * 5, '-' * 15, '-' * 15, '-' * 15, '-' * 15))
76ef4fbe   hitier   Click, Sqlalchemy...
39
    for user in User.query.all():
a49732b5   hitier   Add current user ...
40
41
        print(user.login)
        print("{:<5} {:<15} {:<15} {:<15} {:<15}".format(
76ef4fbe   hitier   Click, Sqlalchemy...
42
43
            user.id,
            user.name,
a49732b5   hitier   Add current user ...
44
            user.login,
76ef4fbe   hitier   Click, Sqlalchemy...
45
46
47
            user.password,
            user.email
        ))