Comment out the 'loadtest' backend in the 'counter' backend sample so that it does...
[gae-samples.git] / codepad / main.py
blob33fd057bfa2947640fb0b600a48bdd6c99d929bd
1 #!/usr/bin/env python
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
24 import os
26 TEMPLATE_PATH = os.path.join(os.path.dirname(__file__), 'index.html')
29 class Pad(db.Model):
30 """
31 Store source code and a link to a previous revisions.
32 """
33 code = db.TextProperty(required=True)
34 parent_pad = db.SelfReferenceProperty()
37 class EditHandler(webapp.RequestHandler):
38 """
39 Build the form to edit source code, and handle submission.
40 """
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)
51 pad_key = pad.put()
52 else:
53 pad_key = parent_pad.key()
54 self.redirect('/%s' % pad_key.id())
57 class MainHandler(webapp.RequestHandler):
58 """
59 Evaluate source code in the iframe.
60 Named 'MainHandler' to match the code included in the template.
61 """
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)],
70 debug=True)
73 def main():
74 util.run_wsgi_app(application)
76 if __name__ == '__main__':
77 main()