3 # Copyright 2011 Google Inc.
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
9 # http://www.apache.org/licenses/LICENSE-2.0
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
18 from google
.appengine
.ext
import webapp
19 from google
.appengine
.ext
import db
20 from google
.appengine
.ext
.webapp
import util
21 from google
.appengine
.ext
.webapp
import template
22 from google
.appengine
.api
import namespace_manager
26 TEMPLATE_PATH
= os
.path
.join(os
.path
.dirname(__file__
), 'index.html')
31 Store source code and a link to a previous revisions.
33 code
= db
.TextProperty(required
=True)
34 parent_pad
= db
.SelfReferenceProperty()
37 class EditHandler(webapp
.RequestHandler
):
39 Build the form to edit source code, and handle submission.
42 def get(self
, pad_id
):
43 pad
= Pad
.get_by_id(int(pad_id
)) if pad_id
else None
44 self
.response
.out
.write(template
.render(TEMPLATE_PATH
, {'pad': pad
}))
46 def post(self
, parent_id
):
47 code
= self
.request
.get('code').replace('\r\n', '\n')
48 parent_pad
= Pad
.get_by_id(int(parent_id
)) if parent_id
else None
49 if not parent_pad
or parent_pad
.code
!= code
:
50 pad
= Pad(code
=code
, parent_pad
=parent_pad
)
53 pad_key
= parent_pad
.key()
54 self
.redirect('/%s' % pad_key
.id())
57 class MainHandler(webapp
.RequestHandler
):
59 Evaluate source code in the iframe.
60 Named 'MainHandler' to match the code included in the template.
63 def get(self
, pad_id
):
64 pad
= Pad
.get_by_id(int(pad_id
))
65 namespace_manager
.set_namespace(pad_id
)
66 exec(pad
.code
, {'webapp': webapp
}, {'self': self
})
68 application
= webapp
.WSGIApplication([('/(\d+)/eval', MainHandler
),
69 ('/(\d*)', EditHandler
)],
74 util
.run_wsgi_app(application
)
76 if __name__
== '__main__':