forked from langdev/langdev.org-old
-
Notifications
You must be signed in to change notification settings - Fork 0
/
manage_langdev.py
executable file
·79 lines (63 loc) · 2.65 KB
/
manage_langdev.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
#!/usr/bin/env python
""":mod:`manage_langdev` --- LangDev manager script
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
"""
import sys
import os.path
import hashlib
import datetime
import flask
from flaskext.script import *
import langdev.orm
import langdev.web
import langdev.user
model_modules = ['langdev.user', 'langdev.forum', 'langdev.thirdparty']
def create_app(config_filename):
if not os.path.isfile(config_filename):
msg = "{0} doesn't exist yet; would you create it"
creating = prompt_bool(msg.format(config_filename), default=True)
if creating:
create_config_file(config_filename)
if not os.path.isfile(config_filename):
print>>sys.stderr, "{0} doesn't exist".format(config_filename)
raise SystemExit
return langdev.web.create_app(config_filename=config_filename)
manager = Manager(create_app)
manager.add_option('-c', '--config', dest='config_filename', required=True)
def create_config_file(config_filename):
"""Creates a new config file."""
current_path = os.path.abspath('.')
with open(config_filename, 'w') as config:
database_url = 'sqlite:///{0}/db.sqlite'.format(current_path)
database_url = prompt('Database URL', default=database_url)
print>>config, 'DATABASE_URL =', repr(database_url)
secret_key = hashlib.md5(`datetime.datetime.now()`).hexdigest()
secret_key = prompt('Secret key for secure session',
default=secret_key)
print>>config, 'SECRET_KEY =', repr(secret_key)
intersites = []
if prompt_bool('Are there any external sites to link', default=False):
while True:
key = prompt('External site name')
value = prompt('URL of ' + key)
intersites.append((key, value))
if not prompt_bool('Are there remaining external sites',
default=False):
break
print>>config, 'INTERSITES =', repr(intersites)
print '{0} config file has created'.format(config_filename)
@manager.command
def initdb():
"""Creates all tables needed by LangDev."""
for module in model_modules:
__import__(module)
engine = langdev.web.get_database_engine(flask.current_app.config)
langdev.orm.Base.metadata.create_all(engine)
@manager.shell
def make_shell_context():
engine = langdev.web.get_database_engine(flask.current_app.config)
return {'app': flask.current_app, 'g': flask.g,
'engine': engine, 'session': langdev.orm.Session(bind=engine),
'langdev': langdev, 'User': langdev.user.User}
if __name__ == '__main__':
manager.run()