Py | Google App Engine (BaseApp)


/ Published in: Python
Save to your folder(s)



Copy this code and paste it in your HTML
  1. # -------
  2. # main.py
  3. # -------
  4.  
  5. ###
  6. ### imports
  7. import os
  8. import datetime
  9. from google.appengine.ext import webapp
  10. from google.appengine.ext.webapp import template
  11. from google.appengine.ext.webapp import util
  12.  
  13. ###
  14. ### manager
  15. class BaseRequestHandler(webapp.RequestHandler):
  16. def trace(self, value):
  17. self.response.out.write('<pre>%s</pre>' % value)
  18.  
  19. def render(self, tmpl_name, tmpl_value={}):
  20. tmpl_name = self.getTemplate(tmpl_name)
  21. tmpl_value.update(application_metadata)
  22.  
  23. self.response.out.write(
  24. template.render(tmpl_name, tmpl_value, debug=application_debug))
  25.  
  26. def http_error(self):
  27. self.error(404)
  28. return self.getTemplate('error.html')
  29.  
  30. def getTemplate(self, tmpl_name):
  31. if tmpl_name == None or len(tmpl_name) == 0:
  32. return self.http_error()
  33.  
  34. path = os.path.join(
  35. application_directory, os.path.join('templates', tmpl_name))
  36.  
  37. if os.path.exists(path) != True:
  38. return self.http_error()
  39.  
  40. return path
  41.  
  42. ###
  43. ### pages
  44. class ManagerPages(BaseRequestHandler):
  45. def get(self):
  46. self.render(application_templates.get(self.request.path))
  47.  
  48. ###
  49. ### actions
  50. class LoginAction(BaseRequestHandler):
  51. def get(self):
  52. self.render('login.html')
  53.  
  54. def post(self):
  55. values = {
  56. 'username': self.request.get('username'),
  57. 'password': self.request.get('password')
  58. }
  59. self.render('login.html', values)
  60.  
  61. ###
  62. ### config
  63.  
  64. application_metadata = {
  65. 'html_title': 'App Name',
  66. 'html_copyright_date': '0000 - ' + str(datetime.datetime.now().year),
  67. 'html_meta_description': 'Description',
  68. 'html_meta_keywords': 'key, words'
  69. }
  70.  
  71. application_templates = {
  72. '/': 'index.html',
  73. '/about-us': 'about-us.html',
  74. '/services': 'services.html',
  75. '/portfolio': 'portfolio.html',
  76. '/contact-us': 'contact-us.html'
  77. }
  78.  
  79. application_mapping = [
  80. ('/login.do', LoginAction),
  81. ('/.*', ManagerPages)
  82. ]
  83.  
  84. application_debug = True
  85. application_directory = os.path.dirname(__file__)
  86. application = webapp.WSGIApplication(application_mapping, debug=application_debug)
  87.  
  88. ###
  89. ### main
  90. def main():
  91. util.run_wsgi_app(application)
  92.  
  93. ###
  94. ### init
  95. if __name__ == '__main__':
  96. main()
  97.  
  98.  
  99.  
  100. # --------
  101. # app.yaml
  102. # --------
  103.  
  104. application: app_name
  105. version: 1
  106. runtime: python
  107. api_version: 1
  108.  
  109. skip_files: |
  110. ^(.*/)?(
  111. (app\.yaml)|
  112. (app\.yml)|
  113. (index\.yaml)|
  114. (index\.yml)|
  115. (#.*#)|
  116. (.*~)|
  117. (.*\.py[co])|
  118. )$
  119.  
  120. handlers:
  121. - url: /common
  122. static_dir: common
  123.  
  124. - url: /favicon.ico
  125. static_files: common/favicon.ico
  126. upload: common/favicon.ico
  127.  
  128. - url: /.*
  129. script: main.py

URL: http://www.alejandrob.com.ar/

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.