forked from JackStouffer/Flask-Foundation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
manage.py
executable file
·69 lines (52 loc) · 1.69 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
#!/usr/bin/env python
import os
from flask.ext.script import Manager, Server
from flask.ext.script.commands import ShowUrls, Clean
from appname import create_app
from appname.models import db, User
# default to dev config because no one should use this in
# production anyway
env = os.environ.get('APPNAME_ENV', 'dev')
app = create_app('appname.settings.%sConfig' % env.capitalize(), env=env)
manager = Manager(app)
manager.add_command("server", Server())
manager.add_command("show-urls", ShowUrls())
manager.add_command("clean", Clean())
@manager.shell
def make_shell_context():
""" Creates a python REPL with several default imports
in the context of the app
"""
return dict(app=app, db=db, User=User)
@manager.command
def create_tables():
db.create_all()
@manager.command
def drop_tables():
db.drop_all()
@manager.command
def create_superuser():
if User.query.count() == 1:
if not Role.query.count():
superuser = Role()
superuser.name = 'superuser'
superuser.description = 'superuser'
db.session.add(superuser)
db.session.commit()
else:
superuser = Role.query.filter(Role.name == 'superuser').one()
admin = User.query.first()
admin.roles.append(superuser)
db.session.commit()
@manager.option('-e', '--email', dest='email')
@manager.option('-p', '--password', dest='password')
def create_admin(email, password):
admin = User()
admin.email = email
admin.password = encrypt_password(password)
admin.active = True
admin.confirmed_at = datetime.now(tzlocal())
db.session.add(admin)
db.session.commit()
if __name__ == "__main__":
manager.run()