forked from flaskbb/flaskbb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmanage.py
executable file
·283 lines (215 loc) · 8.98 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
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
#!/usr/bin/env python
"""
flaskbb.manage
~~~~~~~~~~~~~~~~~~~~
This script provides some easy to use commands for
creating the database with or without some sample content.
You can also run the development server with it.
Just type `python manage.py` to see the full list of commands.
TODO: When Flask 1.0 is released, get rid of Flask-Script and use click.
Then it's also possible to split the commands in "command groups"
which would make the commands better seperated from each other
and less confusing.
:copyright: (c) 2014 by the FlaskBB Team.
:license: BSD, see LICENSE for more details.
"""
import sys
import os
import subprocess
from flask import current_app
from werkzeug.utils import import_string
from sqlalchemy.exc import IntegrityError, OperationalError
from flask_script import (Manager, Shell, Server, prompt, prompt_pass,
prompt_bool)
from flask_migrate import MigrateCommand, upgrade
from flaskbb import create_app
from flaskbb.extensions import db, plugin_manager
from flaskbb.utils.populate import (create_test_data, create_welcome_forum,
create_admin_user, create_default_groups,
create_default_settings, insert_mass_data,
update_settings_from_fixture)
# Use the development configuration if available
try:
from flaskbb.configs.development import DevelopmentConfig as Config
except ImportError:
from flaskbb.configs.default import DefaultConfig as Config
app = create_app(Config)
manager = Manager(app)
# Used to get the plugin translations
PLUGINS_FOLDER = os.path.join(app.root_path, "plugins")
# Run local server
manager.add_command("runserver", Server("localhost", port=8080))
# Migration commands
manager.add_command('db', MigrateCommand)
# Add interactive project shell
def make_shell_context():
return dict(app=current_app, db=db)
manager.add_command("shell", Shell(make_context=make_shell_context))
@manager.command
def initdb():
"""Creates the database."""
upgrade()
@manager.command
def dropdb():
"""Deletes the database."""
db.drop_all()
@manager.command
def populate(dropdb=False, createdb=False):
"""Creates the database with some default data.
If you do not want to drop or create the db add
'-c' (to not create the db) and '-d' (to not drop the db)
"""
if not dropdb:
app.logger.info("Dropping database...")
db.drop_all()
if not createdb:
app.logger.info("Creating database...")
upgrade()
app.logger.info("Creating test data...")
create_test_data()
@manager.option('-u', '--username', dest='username')
@manager.option('-p', '--password', dest='password')
@manager.option('-e', '--email', dest='email')
def create_admin(username=None, password=None, email=None):
"""Creates the admin user."""
if not (username and password and email):
username = prompt("Username")
email = prompt("A valid email address")
password = prompt_pass("Password")
create_admin_user(username=username, password=password, email=email)
@manager.option('-u', '--username', dest='username')
@manager.option('-p', '--password', dest='password')
@manager.option('-e', '--email', dest='email')
def init(username=None, password=None, email=None):
"""Initializes FlaskBB with all necessary data."""
app.logger.info("Creating default data...")
try:
create_default_groups()
create_default_settings()
except IntegrityError:
app.logger.error("Couldn't create the default data because it already "
"exist!")
if prompt_bool("Found an existing database."
"Do you want to recreate the database? (y/n)"):
db.session.rollback()
db.drop_all()
upgrade()
create_default_groups()
create_default_settings()
else:
sys.exit(0)
except OperationalError:
app.logger.error("No database found.")
if prompt_bool("Do you want to create the database now? (y/n)"):
db.session.rollback()
upgrade()
create_default_groups()
create_default_settings()
else:
sys.exit(0)
app.logger.info("Creating admin user...")
if username and password and email:
create_admin_user(username=username, password=password, email=email)
else:
create_admin()
app.logger.info("Creating welcome forum...")
create_welcome_forum()
app.logger.info("Compiling translations...")
compile_translations()
app.logger.info("Congratulations! FlaskBB has been successfully installed")
@manager.command
def insertmassdata():
"""Warning: This can take a long time!.
Creates 100 topics and each topic contains 100 posts.
"""
insert_mass_data()
@manager.option('-s', '--settings', dest="settings")
@manager.option('-f', '--force', dest="force", default=False)
def update(settings=None, force=False):
"""Updates the settings via a fixture. All fixtures have to be placed
in the `fixture`.
Usage: python manage.py update -s your_fixture
"""
try:
fixture = import_string(
"flaskbb.fixtures.{}".format(settings)
)
fixture = fixture.fixture
except ImportError:
raise "{} fixture is not available".format(settings)
if force:
count = update_settings_from_fixture(fixture, overwrite_group=True,
overwrite_setting=True)
app.logger.info(
"{} groups and {} settings forcefully updated."
.format(count[0], count[1])
)
else:
count = update_settings_from_fixture(fixture)
app.logger.info(
"{} groups and {} settings updated.".format(count[0], count[1])
)
@manager.command
def update_translations():
"""Updates all translations."""
# update flaskbb translations
translations_folder = os.path.join(app.root_path, "translations")
source_file = os.path.join(translations_folder, "messages.pot")
subprocess.call(["pybabel", "extract", "-F", "babel.cfg",
"-k", "lazy_gettext", "-o", source_file, "."])
subprocess.call(["pybabel", "update", "-i", source_file,
"-d", translations_folder])
# updates all plugin translations too
for plugin in plugin_manager.all_plugins:
update_plugin_translations(plugin)
@manager.command
def add_translations(translation):
"""Adds a new language to the translations."""
translations_folder = os.path.join(app.root_path, "translations")
source_file = os.path.join(translations_folder, "messages.pot")
subprocess.call(["pybabel", "extract", "-F", "babel.cfg",
"-k", "lazy_gettext", "-o", source_file, "."])
subprocess.call(["pybabel", "init", "-i", source_file,
"-d", translations_folder, "-l", translation])
@manager.command
def compile_translations():
"""Compiles all translations."""
# compile flaskbb translations
translations_folder = os.path.join(app.root_path, "translations")
subprocess.call(["pybabel", "compile", "-d", translations_folder])
# compile all plugin translations
for plugin in plugin_manager.all_plugins:
compile_plugin_translations(plugin)
# Plugin translation commands
@manager.command
def add_plugin_translations(plugin, translation):
"""Adds a new language to the plugin translations. Expects the name
of the plugin and the translations name like "en".
"""
plugin_folder = os.path.join(PLUGINS_FOLDER, plugin)
translations_folder = os.path.join(plugin_folder, "translations")
source_file = os.path.join(translations_folder, "messages.pot")
subprocess.call(["pybabel", "extract", "-F", "babel.cfg",
"-k", "lazy_gettext", "-o", source_file,
plugin_folder])
subprocess.call(["pybabel", "init", "-i", source_file,
"-d", translations_folder, "-l", translation])
@manager.command
def update_plugin_translations(plugin):
"""Updates the plugin translations. Expects the name of the plugin."""
plugin_folder = os.path.join(PLUGINS_FOLDER, plugin)
translations_folder = os.path.join(plugin_folder, "translations")
source_file = os.path.join(translations_folder, "messages.pot")
subprocess.call(["pybabel", "extract", "-F", "babel.cfg",
"-k", "lazy_gettext", "-o", source_file,
plugin_folder])
subprocess.call(["pybabel", "update", "-i", source_file,
"-d", translations_folder])
@manager.command
def compile_plugin_translations(plugin):
"""Compile the plugin translations. Expects the name of the plugin."""
plugin_folder = os.path.join(PLUGINS_FOLDER, plugin)
translations_folder = os.path.join(plugin_folder, "translations")
subprocess.call(["pybabel", "compile", "-d", translations_folder])
if __name__ == "__main__":
manager.run()