-
Notifications
You must be signed in to change notification settings - Fork 2
/
manage.py.bak
226 lines (210 loc) · 7 KB
/
manage.py.bak
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
import os, sys
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), 'teaching_site')))
sys.path.append(os.path.abspath(os.path.dirname(__file__)))
#from flask.ext.script import Manager, Server
from flask_script import Manager, Server, Command, Option
from flask_migrate import MigrateCommand
from teaching_site import app, db
from teaching_site.user.models import User
from teaching_site.question.models import UnitCategory, Unit
from teaching_site.question.models import Variable, Question
from teaching_site.question.models import QuestionCategory
from teaching_site.exercise.models import Exercise
from datetime import datetime
from dateutil.relativedelta import relativedelta
class GunicornServer(Command):
description = 'Run the app within Gunicorn'
#def __init__(self, host='0.0.0.0', port=int(os.getenv('PORT', 8000)), workers=4):
def __init__(self, host='127.0.0.1', port=80, workers=4):
self.port = port
self.host = host
self.workers = workers
def get_options(self):
return (
Option('-H', '--host',
dest='host',
default=self.host),
Option('-p', '--port',
dest='port',
type=int,
default=self.port),
Option('-w', '--workers',
dest='workers',
type=int,
default=self.workers),
)
def __call__(self, app, host, port, workers):
from gunicorn import version_info
if version_info < (0, 9, 0):
from gunicorn.arbiter import Arbiter
from gunicorn.config import Config
arbiter = Arbiter(Config({'bind': "%s:%d" % (host, int(port)),'workers': workers}), app)
arbiter.run()
else:
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
FlaskApplication().run()
manager = Manager(app)
manager.add_command(
'db',
MigrateCommand
)
manager.add_command(
"runserver",
Server(
use_debugger = True,
use_reloader = True,
host = os.getenv('IP', '0.0.0.0'),
port = int(os.getenv('PORT', 5000))
)
)
manager.add_command(
"gunicorn", GunicornServer()
)
@manager.command
def init():
db.create_all()
email = app.config['ROOT']
admin = User(
username = email,
email = email,
password_tmp = 'admin',
validated = True,
is_admin = True
)
db.session.add(admin)
try:
db.session.commit()
except:
print "admin can not be added"
@manager.command
def create_unit():
for name, base in app.config['UNIT_CATAGORIES'].items():
unit = UnitCategory(name, *base)
db.session.add(unit)
try:
db.session.commit()
except:
pass
for unit_config in app.config['UNITS']:
db.session.add(Unit(*unit_config))
try:
db.session.commit()
except:
print unit[0],
print 'failed'
@manager.command
def create_variable():
for variable in app.config['VARIABLES']:
if 'units' in variable:
unit_list = variable.pop('units')
units = []
for unit_str in unit_list:
unit = Unit.query.filter_by(
name = unit_str
).first()
if unit is not None:
units.append(unit)
else:
print "%s not found" % unit_str
variable['units'] = units
db.session.add(Variable(
**variable
))
db.session.commit()
@manager.command
def create_question():
obj_list_dict = {
'text_variables': Variable,
'answer_units': Unit,
}
obj_dict = {
'correct_variable': Variable,
'wrong_variable': Variable
}
qc_dict = {}
for name in app.config['QUESTIONCATAGORIES']:
qc = QuestionCategory(name = name)
qc_dict[name] = qc
db.session.add(qc)
db.session.commit()
for question_config in app.config['QUESTIONS']:
config = {}
config['name'] = question_config['name']
config['body'] = question_config['body']
qc = qc_dict[question_config['category']]
config['category_id'] = qc.id
if 'answer_command' in question_config:
config['answer_command'] = question_config['answer_command']
for key, value in obj_list_dict.items():
if key in question_config:
obj_list = []
config[key] = obj_list
for obj_str in question_config[key]:
obj = value.query.filter_by(
name = obj_str
).first()
if obj:
obj_list.append(obj)
else:
print "failed, %s not found" % obj_str
for key, value in obj_dict.items():
if key in question_config:
obj_str = question_config[key]
obj = value.query.filter_by(
name = obj_str
).first()
if obj:
config[key] = obj
else:
print "failed, %s not found" % obj_str
db.session.add(Question(
**config
))
db.session.commit()
@manager.command
def create_exercise():
for exe_config in app.config['EXERCISE']:
config = {}
config['name'] = exe_config['name']
config['open_date'] = datetime.today() - relativedelta(years=1)
config['close_date'] = datetime.today() + relativedelta(years=1)
questions = []
for q in exe_config['questions']:
question = Question.query.filter_by(
name = q
).first()
if question:
questions.append(question)
else:
print "failed, %s not found" % q
config['questions'] = questions
exercise = Exercise(**config)
db.session.add(exercise)
for exe_config in app.config['EXERCISE']:
config = {}
config['name'] = exe_config['name'] + ' (expired)'
config['open_date'] = datetime.today() - relativedelta(years=2)
config['close_date'] = datetime.today() - relativedelta(years=1)
questions = []
for q in exe_config['questions']:
question = Question.query.filter_by(
name = q
).first()
if question:
questions.append(question)
else:
print "failed, %s not found" % q
config['questions'] = questions
exercise = Exercise(**config)
db.session.add(exercise)
db.session.commit()
if __name__ == '__main__':
manager.run()