/
manage.py
97 lines (69 loc) · 2.25 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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# -*- coding: utf-8 -*-
#!/usr/bin/env python
"""
.. module:: manage
:synopsis: Flask manager for perfomining management commands such as
running a local development server.
"""
import re
import sys
from flask.ext.migrate import Migrate, MigrateCommand
from flask.ext.script import Manager, prompt, prompt_pass, Shell, Server
from flask.ext.security import SQLAlchemyUserDatastore
from flask.ext.security.utils import encrypt_password
from pravis.app import create_app
from pravis.auth.models import User, Role
from pravis.ext import db
app = create_app()
manager = Manager(app)
migrate = Migrate(app, db)
EMAIL_REGEX = re.compile(r"[^@]+@[^@]+\.[^@]+")
def _make_context():
"""
Return context dict for a shell session so you can access
app, db, and the User model by default.
"""
return {
'app': app,
'db': db,
'User': User
}
@manager.command
def schema_diagram():
"""
Draw an Entity Relationship Diagram
"""
from sqlalchemy_schemadisplay import create_schema_graph
graph = create_schema_graph(
metadata=db.MetaData(app.config['SQLALCHEMY_DATABASE_URI']),
show_datatypes=True,
show_indexes=True
)
graph.write_png('schema.png')
@manager.command
def createsuperuser():
"""
Create a super user of the system, requiring Email and password.
"""
email = prompt('User E-Mail')
email_confirm = prompt('Confirm E-Mail')
if not email == email_confirm:
sys.exit('\nCould not create user: E-Mail did not match')
if not EMAIL_REGEX.match(email):
sys.exit('\nCould not create user: Invalid E-Mail addresss')
password = prompt_pass('User password')
password_confirm = prompt_pass('Confirmed password')
if not password == password_confirm:
sys.exit('\nCould not create user: Passwords did not match')
datastore = SQLAlchemyUserDatastore(db, User, Role)
datastore.create_user(
email=email,
password=encrypt_password(password),
active=True,
super_user=True)
db.session.commit()
manager.add_command("server", Server())
manager.add_command('db', MigrateCommand)
manager.add_command("shell", Shell(make_context=_make_context))
if __name__ == "__main__":
manager.run()