forked from oppia/oppia
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
360 lines (312 loc) · 14.5 KB
/
main.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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
# Copyright 2014 The Oppia Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS-IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""URL routing definitions, and some basic error/warmup handlers."""
__author__ = 'Sean Lip'
import feconf
import logging
from core.controllers import admin
from core.controllers import base
from core.controllers import collection_viewer
from core.controllers import editor
from core.controllers import feedback
from core.controllers import galleries
from core.controllers import home
from core.controllers import moderator
from core.controllers import pages
from core.controllers import profile
from core.controllers import reader
from core.controllers import recent_commits
from core.controllers import resources
from core.platform import models
transaction_services = models.Registry.import_transaction_services()
from mapreduce import main as mapreduce_main
from mapreduce import parameters as mapreduce_parameters
import webapp2
from webapp2_extras.routes import RedirectRoute
class FrontendErrorHandler(base.BaseHandler):
"""Handles errors arising from the frontend."""
REQUIRE_PAYLOAD_CSRF_CHECK = False
def post(self):
"""Records errors reported by the frontend."""
logging.error('Frontend error: %s' % self.payload.get('error'))
self.render_json(self.values)
class WarmupHandler(base.BaseHandler):
"""Handles warmup requests."""
def get(self):
"""Handles GET warmup requests."""
pass
# Regex for base64 id encoding
r = '[A-Za-z0-9=_-]+'
def get_redirect_route(regex_route, handler, name, defaults=None):
"""Returns a route that redirects /foo/ to /foo.
Warning: this method strips off parameters after the trailing slash. URLs
with parameters should be formulated without the trailing slash.
"""
if defaults is None:
defaults = {}
return RedirectRoute(
regex_route, handler, name, strict_slash=True, defaults=defaults)
def authorization_wrapper(self, *args, **kwargs):
# developers.google.com/appengine/docs/python/taskqueue/overview-push
# promises that this header cannot be set by external callers. If this
# is present, we can be certain that the request is internal and from
# the task queue worker.
if 'X-AppEngine-TaskName' not in self.request.headers:
self.response.out.write('Forbidden')
self.response.set_status(403)
return
self.real_dispatch(*args, **kwargs)
def ui_access_wrapper(self, *args, **kwargs):
self.real_dispatch(*args, **kwargs)
mapreduce_handlers = []
for path, handler_class in mapreduce_main.create_handlers_map():
if path.startswith('.*/pipeline'):
if 'pipeline/rpc/' in path or path == '.*/pipeline(/.+)':
path = path.replace('.*/pipeline', '/mapreduce/ui/pipeline')
else:
path = path.replace('.*/pipeline', '/mapreduce/worker/pipeline')
else:
if '_callback' in path:
path = path.replace('.*', '/mapreduce/worker', 1)
elif '/list_configs' in path:
continue
else:
path = path.replace('.*', '/mapreduce/ui', 1)
if '/ui/' in path or path.endswith('/ui'):
if (hasattr(handler_class, 'dispatch') and
not hasattr(handler_class, 'real_dispatch')):
handler_class.real_dispatch = handler_class.dispatch
handler_class.dispatch = ui_access_wrapper
mapreduce_handlers.append((path, handler_class))
else:
if (hasattr(handler_class, 'dispatch') and
not hasattr(handler_class, 'real_dispatch')):
handler_class.real_dispatch = handler_class.dispatch
handler_class.dispatch = authorization_wrapper
mapreduce_handlers.append((path, handler_class))
# Tell map/reduce internals that this is now the base path to use.
mapreduce_parameters.config.BASE_PATH = '/mapreduce/worker'
# Register the URLs with the classes responsible for handling them.
urls = [
get_redirect_route(r'/_ah/warmup', WarmupHandler, 'warmup_handler'),
get_redirect_route(
r'/notifications_dashboard', home.NotificationsDashboardPage,
'notifications_dashboard_handler'),
get_redirect_route(
r'/notificationsdashboardhandler/data',
home.NotificationsDashboardHandler,
'notifications_dashboard_handler'),
get_redirect_route(
r'/my_explorations', home.MyExplorationsPage, 'my_explorations_page'),
get_redirect_route(
r'/myexplorationshandler/data', home.MyExplorationsHandler,
'my_explorations_handler'),
get_redirect_route(r'/about', pages.AboutPage, 'about_page'),
get_redirect_route(
r'/participate', pages.ParticipatePage, 'participate_page'),
get_redirect_route(
r'/site_guidelines', pages.ParticipatePage,
'redirect_to_participate_page'),
get_redirect_route(
r'/contact', pages.AboutPage, 'redirect_to_about_page'),
get_redirect_route(r'/forum', pages.ForumPage, 'forum_page'),
get_redirect_route(r'/terms', pages.TermsPage, 'terms_page'),
get_redirect_route(r'/privacy', pages.PrivacyPage, 'privacy_page'),
get_redirect_route(r'/admin', admin.AdminPage, 'admin_page'),
get_redirect_route(r'/adminhandler', admin.AdminHandler, 'admin_handler'),
get_redirect_route(
r'/adminjoboutput', admin.AdminJobOutput, 'admin_job_output'),
get_redirect_route(
r'/admintopicscsvdownloadhandler',
admin.AdminTopicsCsvDownloadHandler,
'admin_topics_csv_download_handler'),
get_redirect_route(
r'/imagehandler/<exploration_id>/<encoded_filepath>',
resources.ImageHandler, 'image_handler'),
get_redirect_route(
r'/object_editor_template/<obj_type>',
resources.ObjectEditorTemplateHandler, 'object_editor_template'),
get_redirect_route(
r'/value_generator_handler/<generator_id>',
resources.ValueGeneratorHandler, 'value_generator_handler'),
get_redirect_route(r'/', galleries.GalleryPage, 'gallery_page'),
get_redirect_route(
r'%s' % feconf.GALLERY_URL, galleries.GalleryPage, 'gallery_page'),
get_redirect_route(
r'%s' % feconf.GALLERY_DATA_URL, galleries.GalleryHandler,
'gallery_handler'),
get_redirect_route(
r'%s' % feconf.LEARN_GALLERY_URL, galleries.GalleryRedirectPage,
'learn_gallery_page'),
get_redirect_route(
r'%s' % feconf.PLAYTEST_QUEUE_URL, galleries.GalleryRedirectPage,
'playtest_queue_page'),
get_redirect_route(
r'%s' % feconf.CONTRIBUTE_GALLERY_URL, galleries.GalleryRedirectPage,
'contribute_gallery_page'),
get_redirect_route(
r'%s' % feconf.NEW_EXPLORATION_URL,
galleries.NewExploration, 'new_exploration'),
get_redirect_route(
r'%s' % feconf.UPLOAD_EXPLORATION_URL,
galleries.UploadExploration, 'upload_exploration'),
get_redirect_route(
r'/explorationsummarieshandler/data',
galleries.ExplorationSummariesHandler, 'exploration_summaries_handler'),
get_redirect_route(
r'/profile/<username>', profile.ProfilePage, 'profile_page'),
get_redirect_route(
r'/profilehandler/data/<username>', profile.ProfileHandler,
'profile_handler'),
get_redirect_route(
r'/preferences', profile.PreferencesPage, 'preferences_page'),
get_redirect_route(
r'/preferenceshandler/data', profile.PreferencesHandler,
'preferences_handler'),
get_redirect_route(
r'/preferenceshandler/profile_picture', profile.ProfilePictureHandler,
'profle_picture_handler'),
get_redirect_route(
r'/preferenceshandler/profile_picture_by_username/<username>', profile.ProfilePictureHandlerByUsername,
'profile_picture_handler_by_username'),
get_redirect_route(
r'%s' % feconf.SIGNUP_URL, profile.SignupPage, 'signup_page'),
get_redirect_route(
r'%s' % feconf.SIGNUP_DATA_URL, profile.SignupHandler,
'signup_handler'),
get_redirect_route(
r'%s' % feconf.USERNAME_CHECK_DATA_URL,
profile.UsernameCheckHandler, 'username_check_handler'),
get_redirect_route(
r'/moderator', moderator.ModeratorPage, 'moderator_page'),
get_redirect_route(
r'/moderatorhandler/user_services',
moderator.UserServiceHandler, 'moderator_user_service_handler'),
get_redirect_route(
r'%s/<exploration_id>' % feconf.EXPLORATION_URL_PREFIX,
reader.ExplorationPage, 'exploration_page'),
get_redirect_route(
r'%s/<exploration_id>' % feconf.EXPLORATION_INIT_URL_PREFIX,
reader.ExplorationHandler, 'exploration_handler'),
get_redirect_route(
r'/explorehandler/exploration_start_event/<exploration_id>',
reader.ExplorationStartEventHandler,
'exploration_start_event_handler'),
get_redirect_route(
r'/explorehandler/state_hit_event/<exploration_id>',
reader.StateHitEventHandler, 'state_hit_event_handler'),
get_redirect_route(
r'/explorehandler/answer_submitted_event/<exploration_id>',
reader.AnswerSubmittedEventHandler, 'answer_submitted_event_handler'),
get_redirect_route(
r'/explorehandler/give_feedback/<exploration_id>',
reader.ReaderFeedbackHandler, 'reader_feedback_handler'),
get_redirect_route(
r'/explorehandler/exploration_complete_event/<exploration_id>',
reader.ExplorationCompleteEventHandler, 'reader_complete_handler'),
get_redirect_route(
r'/explorehandler/exploration_maybe_leave_event/<exploration_id>',
reader.ExplorationMaybeLeaveHandler, 'reader_leave_handler'),
get_redirect_route(
r'/explorehandler/classify/<exploration_id>', reader.ClassifyHandler,
'reader_classify_handler'),
get_redirect_route(
r'/explorehandler/rating/<exploration_id>',
reader.RatingHandler, 'rating_handler'),
get_redirect_route(
r'/explorehandler/recommendations/<exploration_id>',
reader.RecommendationsHandler, 'recommendations_handler'),
get_redirect_route(
r'%s/<exploration_id>' % feconf.EDITOR_URL_PREFIX,
editor.ExplorationPage, 'editor_exploration_page'),
get_redirect_route(
r'/createhandler/data/<exploration_id>', editor.ExplorationHandler,
'editor_exploration_handler'),
get_redirect_route(
r'/createhandler/change_list_summary/<exploration_id>',
editor.ChangeListSummaryHandler, 'change_list_summary'),
get_redirect_route(
r'/createhandler/download/<exploration_id>',
editor.ExplorationDownloadHandler, 'exploration_download_handler'),
get_redirect_route(
r'/createhandler/download_state/<exploration_id>',
editor.StateDownloadHandler, 'state_download_handler'),
get_redirect_route(
r'/createhandler/imageupload/<exploration_id>',
editor.ImageUploadHandler, 'image_upload_handler'),
get_redirect_route(
r'/createhandler/resolved_answers/<exploration_id>/<escaped_state_name>',
editor.ResolvedAnswersHandler, 'resolved_answers_handler'),
get_redirect_route(
r'/createhandler/training_data/<exploration_id>/<escaped_state_name>',
editor.UntrainedAnswersHandler, 'training_data_handler'),
get_redirect_route(
r'/createhandler/resource_list/<exploration_id>',
editor.ExplorationResourcesHandler, 'exploration_resources_handler'),
get_redirect_route(
r'/createhandler/revert/<exploration_id>',
editor.ExplorationRevertHandler, 'exploration_revert_handler'),
get_redirect_route(
r'%s/<exploration_id>' % feconf.EXPLORATION_RIGHTS_PREFIX,
editor.ExplorationRightsHandler, 'exploration_rights_handler'),
get_redirect_route(
r'/createhandler/snapshots/<exploration_id>',
editor.ExplorationSnapshotsHandler, 'exploration_snapshots_handler'),
get_redirect_route(
r'/createhandler/statisticsversion/<exploration_id>',
editor.ExplorationStatsVersionsHandler, 'exploration_stats_versions_handler'),
get_redirect_route(
r'/createhandler/statistics/<exploration_id>/<exploration_version>',
editor.ExplorationStatisticsHandler, 'exploration_statistics_handler'),
get_redirect_route(
r'/createhandler/state_rules_stats/<exploration_id>/<escaped_state_name>',
editor.StateRulesStatsHandler, 'state_rules_stats_handler'),
get_redirect_route(
r'/createhandler/started_tutorial_event/<exploration_id>',
editor.StartedTutorialEventHandler, 'started_tutorial_event_handler'),
get_redirect_route(
r'%s' % feconf.RECENT_COMMITS_DATA_URL,
recent_commits.RecentCommitsHandler, 'recent_commits_handler'),
get_redirect_route(
r'%s' % feconf.RECENT_FEEDBACK_MESSAGES_DATA_URL,
feedback.RecentFeedbackMessagesHandler,
'recent_feedback_messages_handler'),
get_redirect_route(
r'%s/<exploration_id>' % feconf.FEEDBACK_LAST_UPDATED_URL_PREFIX,
feedback.FeedbackLastUpdatedHandler, 'feedback_last_updated_handler'),
get_redirect_route(
r'%s/<exploration_id>' % feconf.FEEDBACK_THREADLIST_URL_PREFIX,
feedback.ThreadListHandler, 'feedback_threadlist_handler'),
get_redirect_route(
r'%s/<exploration_id>/<thread_id>' % feconf.FEEDBACK_THREAD_URL_PREFIX,
feedback.ThreadHandler, 'feedback_thread_handler'),
get_redirect_route(
r'%s/<collection_id>' % feconf.COLLECTION_URL_PREFIX,
collection_viewer.CollectionPage, 'collection_page'),
get_redirect_route(
r'%s/<collection_id>' % feconf.COLLECTION_DATA_URL_PREFIX,
collection_viewer.CollectionDataHandler, 'collection_data_handler'),
get_redirect_route(
r'/notificationshandler', home.NotificationsHandler,
'notifications_handler'),
get_redirect_route(
r'/frontend_errors', FrontendErrorHandler, 'frontend_error_handler'),
get_redirect_route(
r'/logout', base.LogoutPage, 'logout_page_handler'),
# 404 error handler.
get_redirect_route(r'/<:.*>', base.Error404Handler, 'error_404_handler'),
]
urls = mapreduce_handlers + urls
app = transaction_services.toplevel_wrapper(
webapp2.WSGIApplication(urls, debug=feconf.DEBUG))