forked from honmaple/maple-bbs
/
manager.py
132 lines (106 loc) · 3.64 KB
/
manager.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# *************************************************************************
# Copyright © 2015 JiangLin. All rights reserved.
# File Name: db_create.py
# Author:JiangLin
# Mail:xiyang0807@gmail.com
# Created Time: 2016-02-11 13:34:38
# *************************************************************************
# !/usr/bin/env python
# -*- coding=UTF-8 -*-
from flask import url_for
from flask_script import Manager
from flask_migrate import Migrate, MigrateCommand
from maple import app, db
from maple.user.models import User, UserInfor, UserSetting, Role
from getpass import getpass
from werkzeug.security import generate_password_hash
import os
migrate = Migrate(app, db)
manager = Manager(app)
@manager.command
def runserver():
return app.run()
@manager.command
def init_db():
"""
Drops and re-creates the SQL schema
"""
db.drop_all()
db.configure_mappers()
db.create_all()
db.session.commit()
@manager.command
def babel_init():
pybabel = 'pybabel'
os.system(pybabel +
' extract -F babel.cfg -k lazy_gettext -o messages.pot maple')
os.system(pybabel + ' init -i messages.pot -d maple/translations -l zh')
os.unlink('messages.pot')
@manager.command
def babel_update():
pybabel = 'pybabel'
os.system(pybabel +
' extract -F babel.cfg -k lazy_gettext -o messages.pot maple templates')
os.system(pybabel + ' update -i messages.pot -d maple/translations')
os.unlink('messages.pot')
@manager.command
def babel_compile():
pybabel = 'pybabel'
os.system(pybabel + ' compile -d maple/translations')
@manager.option('-u', '--username', dest='username', default='admin')
@manager.option('-e', '--email', dest='email')
@manager.option('-w', '--password', dest='password')
def create_user(username, email, password):
if username == 'admin':
username = input('Username(default admin):')
if email is None:
email = input('Email:')
if password is None:
password = getpass('Password:')
user = User()
user.username = username
user.password = generate_password_hash(password)
user.email = email
user.is_superuser = True
userinfor = UserInfor()
user.infor = userinfor
usersetting = UserSetting()
user.setting = usersetting
role = Role.query.filter_by(name='super').first()
if role is None:
role = Role()
role.rolename = 'super'
user.roles.append(role)
db.session.add(user)
db.session.commit()
@manager.command
def list_routes():
import urllib
output = []
for rule in app.url_map.iter_rules():
options = {}
for arg in rule.arguments:
options[arg] = "<{0}>".format(arg)
methods = ','.join(rule.methods)
url = url_for(rule.endpoint, **options)
line = urllib.parse.unquote("{:50s} {:20s} {}".format(rule.endpoint,
methods, url))
output.append(line)
for line in sorted(output):
print(line)
@manager.option('-h', '--host', dest='host', default='127.0.0.1')
@manager.option('-p', '--port', dest='port', type=int, default=8000)
@manager.option('-w', '--workers', dest='workers', type=int, default=2)
def gunicorn(host, port, workers):
"""use gunicorn"""
from gunicorn.app.base import Application
class FlaskApplication(Application):
def init(self, parser, opts, args):
return {'bind': '{0}:{1}'.format(host, port), 'workers': workers}
def load(self):
return app
application = FlaskApplication()
return application.run()
manager.add_command('db', MigrateCommand)
if __name__ == '__main__':
manager.run()