forked from tedivm/tedivms-flask
-
Notifications
You must be signed in to change notification settings - Fork 0
/
manage.py
76 lines (57 loc) · 2.51 KB
/
manage.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
"""This file sets up a command line manager.
Use "python manage.py" for a list of available commands.
Use "python manage.py runserver" to start the development web server on localhost:5000.
Use "python manage.py runserver --help" for additional runserver options.
"""
from flask import Flask
#from flask_migrate import MigrateCommand
from flask.cli import FlaskGroup
import click
from app import create_app
from app.commands import user
@click.group(cls=FlaskGroup, create_app=create_app)
@click.pass_context
def cli(ctx):
"""Management script for the Wiki application."""
if ctx.parent:
click.echo(ctx.parent.get_help())
@cli.command(help='Add a User')
@click.argument('username')
@click.argument('email')
@click.argument('password', required=False)
@click.argument('role', required=False, default=None)
@click.option('-f', '--firstname', default='')
@click.option('-l', '--lastname', default='')
@click.option('-s', '--secure', is_flag=True, default=False, help='Set password with prompt without it appearing on screen')
def add_user(username, email, password, role, firstname, lastname, secure):
if not password and secure:
password = click.prompt('Password', hide_input=True, confirmation_prompt=True)
if not password:
raise click.UsageError("Password must be provided for the user")
user_role = None
if role:
user_role = user.find_or_create_role(role, role)
user.find_or_create_user(firstname, lastname, username, email, password, user_role)
@cli.command(help='Add a Role')
@click.argument('name')
@click.argument('label', required=False)
def add_role(name, label):
if not label:
label = name
user.find_or_create_role(name, label)
@cli.command(help='Change the password of a user')
@click.argument('email')
@click.argument('password', required=False)
@click.option('-s', '--secure', is_flag=True, default=False, help='Set password with prompt without it appearing on screen')
def reset_password(email, password, secure):
if not password and secure:
password = click.prompt('Password', hide_input=True, confirmation_prompt=True)
if not password:
raise click.UsageError("Password must be provided for the user")
user = User.query.filter(User.email == email).first()
if not user:
raise click.UsageError("User does not exist")
user.password = current_app.user_manager.hash_password(password)
db.session.commit()
if __name__ == "__main__":
cli()