forked from honmaple/maple-bbs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmanager.py
More file actions
executable file
·159 lines (125 loc) · 4.12 KB
/
manager.py
File metadata and controls
executable file
·159 lines (125 loc) · 4.12 KB
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# **************************************************************************
# Copyright © 2016 jianglin
# File Name: manager.py
# Author: jianglin
# Email: xiyang0807@gmail.com
# Created: 2016-10-25 22:08:39 (CST)
# Last Update:星期五 2017-7-28 10:43:51 (CST)
# By:
# Description:
# **************************************************************************
from flask_script import Manager
from flask_migrate import Migrate, MigrateCommand
from forums import create_app
from forums.extension import db,cache
from forums.api.user.models import User, UserInfo, UserSetting
from getpass import getpass
from werkzeug.security import generate_password_hash
from datetime import datetime
import os
app = create_app('config')
migrate = Migrate(app, db)
manager = Manager(app)
@manager.command
def create_index():
from forums.extension import search
return search.create_index()
@manager.command
def update_index():
from forums.extension import search
return search.create_index(update=True)
@manager.command
def delete_index():
from forums.extension import search
return search.create_index(delete=True)
@manager.command
def clear_cache():
with app.app_context():
cache.clear()
@manager.command
def test_index():
from forums.extension import search
from forums.api.topic.models import Topic
results = search.whoosh_search(Topic, '河海', ['title'], 1)
print('results:')
print(results)
for i in results:
print(i['title'])
print(i.highlights("title")) # 高亮标题中的检索词
@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 forums')
os.system(pybabel + ' init -i messages.pot -d 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 forums templates'
)
os.system(pybabel + ' update -i messages.pot -d translations')
os.unlink('messages.pot')
@manager.command
def babel_compile():
pybabel = 'pybabel'
os.system(pybabel + ' compile -d translations')
@manager.option('-u', '--username', dest='username')
def delete_user(username):
user = User.query.filter_by(username=username).first()
user.delete()
@manager.option('-u', '--username', dest='username')
def password_user(username):
password = getpass('Password:')
user = User.query.filter_by(username=username).first()
user.set_password(password)
user.save()
@manager.option('-u', '--username', dest='username')
@manager.option('-e', '--email', dest='email')
@manager.option('-w', '--password', dest='password')
def create_user(username, email, password):
if username is None:
username = input('Username(default admin):') or 'admin'
if email is None:
email = input('Email:')
if password is None:
password = getpass('Password:')
user = User(username=username, email=email)
user.set_password(password)
user.is_superuser = True
user.is_confirmed = True
# user.roles = 'Super'
# user.confirmed_time = datetime.utcnow()
user.save()
@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()