-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
195 lines (161 loc) · 6.96 KB
/
app.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
import os, sys, traceback
from flask import Flask
from flask.ext.cors import CORS
from flask.ext.mongoengine import MongoEngine
from flask.ext.restful import Api
from flask.ext.security import Security, MongoEngineUserDatastore, login_required, login_user, current_user, http_auth_required
from flask.ext.security.registerable import register_user
from flask.ext.mail import Mail
import redis
from rq import Queue
import uuid
from celery import Celery
app = Flask(__name__)
app.config.from_object(os.environ['APP_SETTINGS'])
print "Loading redis, redis queue, mongo"
#from worker import conn
#app.rq = Queue(connection = conn)
app.redis = redis.StrictRedis.from_url(app.config['REDIS_URL'])
db = MongoEngine(app)
api = Api(app)
app.config['CORS_HEADERS'] = 'Content-Type'
cors = CORS(app, resources={"/assign_next_question": {"origins": "*"},
"/answers": {"origins": "*"},
"/task_data": {"origins" : "*"}})
import schema.requester
import schema.question
import schema.task
import schema.role
print "Loading mail extension"
sys.stdout.flush()
mail = Mail(app)
print "Loading security datastore"
sys.stdout.flush()
user_datastore = MongoEngineUserDatastore(db, schema.requester.Requester,
schema.role.Role)
security = Security(app, user_datastore)
print "Done loading security datastore. Ready to serve pages."
sys.stdout.flush()
print "Loading Celery"
def make_celery(app):
celery = Celery(app.import_name, backend=app.config['REDIS_URL'],
broker=app.config['REDIS_URL'])
celery.conf.update(app.config)
TaskBase = celery.Task
class ContextTask(TaskBase):
abstract = True
def __call__(self, *args, **kwargs):
with app.app_context():
return TaskBase.__call__(self, *args, **kwargs)
celery.Task = ContextTask
return celery
celery = make_celery(app)
app.celery = celery
@app.before_first_request
def setup_logging():
if not app.debug:
import logging
app.logger.addHandler(logging.StreamHandler())
app.logger.setLevel(logging.ERROR)
@app.before_first_request
def add_test_users():
try:
test_user = schema.requester.Requester.objects.get(
email='[email protected]')
except:
test_user = register_user(email='[email protected]',
password='chrisisawesome')
@app.route('/')
@login_required
def hello():
# Print
# print "Firing the missiles..."
# try:
# test_user = schema.requester.Requester.objects.get(
# email='[email protected]')
# except:
# test_user = register_user(email='[email protected]',
# password='chrisisawesome')
# return 'Hello World! Dan Weld has been added to the DB!'
if current_user.is_authenticated():
requester = schema.requester.Requester.objects.get_or_404(
email=current_user.email)
return 'Hello World! Your username is %s.<br/> Your authentication token is %s. <br/> Your requester_id is %s. <br/>' % (current_user.email,
current_user.get_auth_token(),
requester.id)
else:
return "Hello World! You're not logged in, must be testing"
@app.route('/token')
@http_auth_required
def give_me_my_token():
"""
Requester needs to save this auth token in order to use the API.
"""
requester = schema.requester.Requester.objects.get_or_404(email=current_user.email)
print requester.id
print current_user.get_auth_token()
return flask.jsonify(requester_id=str(requester.id),
auth_token=str(current_user.get_auth_token()))
@app.route('/logout')
@login_required
def logout():
logout_user()
return "You have been logged out."
@app.route('/add_test_questions_and_task')
@login_required
def add_test_questions_and_task():
test_requester = schema.requester.Requester.objects.get_or_404(
email='[email protected]')
test_question1_name = uuid.uuid1().hex
test_question1 = schema.question.Question(name=test_question1_name,
description='test question 1',
requester = test_requester)
test_question1.save()
test_question2_name = uuid.uuid1().hex
test_question2 = schema.question.Question(name=test_question2_name,
description='test question 2',
requester = test_requester)
test_question2.save()
test_task_name = uuid.uuid1().hex
test_task = schema.task.Task(name = test_task_name,
description = 'test task with 2 questions',
requester = test_requester,
questions = [test_question1, test_question2])
test_task.save()
return 'Test questions and task added to DB'
# API routes go here.
from api.question_api import *
api.add_resource(QuestionApi, '/questions/<question_id>') #UNSECURED
api.add_resource(QuestionListApi, '/questions')
api.add_resource(QuestionAnswersApi, '/questions/answers') #UNSECURED
api.add_resource(QuestionRequeueApi, '/requeue')
# next question
from api.assignment_api import *
api.add_resource(NextQuestionApi, '/assign_next_question') #DOES NOT REQUIRE SECURITY
#TODO not implemented yet
from api.aggregation_api import *
api.add_resource(AnswerAggregationApi, '/aggregated_answer') #UNSECURED
api.add_resource(TaskAggregationApi, '/tasks/<task_id>/aggregate',
'/tasks/<task_id>/aggregate/<job_id>')
from api.answer_api import *
api.add_resource(AnswerApi, '/answers/<answer_id>') #UNSECURED
api.add_resource(AnswerListApi, '/answers') #DOES NOT REQUIRE SECURITY
api.add_resource(AnswerListApi2, '/answers2') #DOES NOT REQUIRE SECURITY
from api.task_api import *
api.add_resource(TaskApi, '/task_data') #UNSECURED
api.add_resource(TaskListApi, '/tasks')
api.add_resource(TaskQuestionsApi, '/tasks/<task_id>/questions') #UNSECURED
api.add_resource(TaskSetBudget, '/tasks/set_budget')
api.add_resource(TaskDelete, '/tasks/delete')
api.add_resource(TaskClearRedis, '/tasks/clearredis') #SHOULD ONLY BE ALLOWED FOR ADMIN
from api.worker_api import *
api.add_resource(WorkerListApi, '/workers') #UNSECURED
api.add_resource(WorkerApi, '/workers/<worker_id>') #UNSECURED
api.add_resource(WorkerAnswersApi, '/workers/<worker_id>/answers') #UNSECURED
#TODO not implemented yet
#api.add_resource(WorkerSkillApi, '/workers/<worker_id>/skill')
#api.add_resource(WorkerPerTaskSkillApi, '/workers/<worker_id>/skill/<task_id>')
from api.requester_api import *
api.add_resource(RequesterListApi, '/requesters') #UNSECURED
api.add_resource(RequesterApi, '/requesters/<requester_id>') #UNSECURED
api.add_resource(RequesterTasksApi, '/requesters/<requester_id>/tasks') #UNSECURED