forked from oppia/oppia
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
157 lines (131 loc) · 5.44 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
# coding: utf-8
#
# Copyright 2012 Google Inc. 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.
"""Main package for URL routing and the index page."""
__author__ = 'Sean Lip'
import base, classifiers, editor, feconf, models, os, reader, utils, widgets
import webapp2
class Error404Handler(base.BaseHandler):
"""Handles 404 errors."""
def get(self): # pylint: disable-msg=C6409
self.error(404)
class AboutPage(base.BaseHandler):
"""Page with information about Oppia."""
def get(self): # pylint: disable-msg=C6409
"""Handles GET requests."""
self.values['js'] = utils.GetJsFilesWithBase([])
self.values['code_contributors'] = [
'Jeremy Emerson',
'Manas Tungare',
'Sean Lip',
'Stephanie Federwisch',
'Wilson Hong',
'Yana Malysheva',
]
self.values['idea_contributors'] = [
'Alex Kauffmann',
'Catherine Colman',
'Neil Fraser',
'Pavel Simakov',
'Peter Norvig',
'Phil Wagner',
'Philip Guo',
'Reinaldo Aguiar',
]
self.response.out.write(
base.JINJA_ENV.get_template('about.html').render(self.values))
class TermsPage(base.BaseHandler):
"""Page with terms and conditions."""
def get(self): # pylint: disable-msg=C6409
"""Handles GET requests."""
self.values['js'] = utils.GetJsFilesWithBase([])
self.response.out.write(
base.JINJA_ENV.get_template('terms.html').render(self.values))
class MainPage(base.BaseHandler):
"""Oppia's main page."""
def InitializeInputViews(self):
"""Loads pre-written input views into the Oppia datastore."""
# TODO(sll): This is temporary code that automatically loads input views
# into the datastore on startup. Remove it once the bulk upload described
# below is implemented.
input_view_list = [utils.input_views.none,
utils.input_views.multiple_choice,
utils.input_views.int,
utils.input_views.set,
utils.input_views.text]
classifier_list = [classifiers.classifiers.none,
classifiers.classifiers.finite,
classifiers.classifiers.numeric,
classifiers.classifiers.set,
classifiers.classifiers.text]
for i in range(len(input_view_list)):
name = input_view_list[i]
if not models.InputView.gql('WHERE name = :name', name=name).get():
input_view = models.InputView(
name=name, classifier=classifier_list[i],
html=utils.GetFileContents('/input_views/%s.html' % name))
input_view.put()
def EnsureDefaultExplorationExists(self):
"""Add the default explorations, if they don't already exist."""
try:
exploration = utils.GetEntity(models.Exploration, '0')
except:
with open('samples/hola.yaml') as f:
yaml = f.read().decode('utf-8')
exploration = utils.CreateExplorationFromYaml(
yaml=yaml, user=None, title='Demo: ¡Hola!', category='Languages', id='0')
try:
exploration = utils.GetEntity(models.Exploration, '1')
except:
# TODO(sll): Populate the data for this sample exploration.
utils.CreateNewExploration(None, title='Demo 2', id='1')
def get(self): # pylint: disable-msg=C6409
"""Handles GET requests."""
self.InitializeInputViews()
self.EnsureDefaultExplorationExists()
self.values['js'] = utils.GetJsFilesWithBase(['index'])
self.response.out.write(
base.JINJA_ENV.get_template('index.html').render(self.values))
# Regex for base64 hash_id encoding
r = '[A-Za-z0-9=_-]+'
# Register the URL with the responsible classes
urls = [
(r'/?', MainPage),
(r'/about/?', AboutPage),
(r'/terms/?', TermsPage),
(r'/learn/?', reader.MainPage),
(r'/learn/(%s)/?' % r, reader.ExplorationPage),
(r'/learn/(%s)/data/?' % r, reader.ExplorationHandler),
# TODO(sll): there is a potential collision here if the state_id is 'data'.
(r'/learn/(%s)/(%s)/?' % (r, r), reader.ExplorationHandler),
(r'/create/?', editor.MainPage),
(r'/create_new/?', editor.NewExploration),
(r'/create/download/(%s)/?' % r, editor.ExplorationDownloadHandler),
(r'/create/(%s)/?' % r, editor.ExplorationPage),
(r'/create/(%s)/data/?' % r, editor.ExplorationHandler),
# TODO(sll): there is a potential collision here if the state_id is 'data'.
(r'/create/(%s)/(%s)/?' % (r, r), editor.StatePage),
(r'/create/(%s)/(%s)/data/?' % (r, r), editor.StateHandler),
(r'/templates/(%s)/?' % r, editor.TemplateHandler),
(r'/imagehandler/?', editor.Image),
(r'/imagehandler/(%s)/?' % r, editor.Image),
(r'/widgets/?', widgets.Widget),
(r'/widgets/(%s)/?' % r, widgets.Widget),
(r'/widgetrepository/?', widgets.WidgetRepositoryPage),
(r'/widgetrepository/data/?', widgets.WidgetRepositoryHandler),
# 404 error handler.
(r'/.*', Error404Handler),
]
app = webapp2.WSGIApplication(urls, debug=feconf.DEBUG)