Return to Snippet

Revision: 45866
at May 10, 2011 03:06 by alejandrombernardis


Updated Code
# -------
# main.py
# -------

###
### imports
import os
import datetime
from google.appengine.ext import webapp
from google.appengine.ext.webapp import template
from google.appengine.ext.webapp import util

###
### manager
class BaseRequestHandler(webapp.RequestHandler):
    def trace(self, value):
        self.response.out.write('<pre>%s</pre>' % value)
        
    def render(self, tmpl_name, tmpl_value={}):
        tmpl_name = self.getTemplate(tmpl_name)
        tmpl_value.update(application_metadata)
        
        self.response.out.write(
            template.render(tmpl_name, tmpl_value, debug=application_debug))
        
    def http_error(self):
        self.error(404)
        return self.getTemplate('error.html')
        
    def getTemplate(self, tmpl_name):
        if tmpl_name == None or len(tmpl_name) == 0:
            return self.http_error()
            
        path = os.path.join(
            application_directory, os.path.join('templates', tmpl_name))
        
        if os.path.exists(path) != True:
            return self.http_error()
            
        return path

###
### pages
class ManagerPages(BaseRequestHandler):
    def get(self):
        self.render(application_templates.get(self.request.path))
        
###
### actions
class LoginAction(BaseRequestHandler):
    def get(self):
        self.render('login.html')
        
    def post(self):
        values = {
            'username': self.request.get('username'),
            'password': self.request.get('password')
        }
        self.render('login.html', values)
        
###
### config

application_metadata = {
    'html_title': 'App Name',
    'html_copyright_date': '0000 - ' + str(datetime.datetime.now().year),
    'html_meta_description': 'Description',
    'html_meta_keywords': 'key, words'
}

application_templates = {
    '/': 'index.html',
    '/about-us': 'about-us.html',
    '/services': 'services.html',
    '/portfolio': 'portfolio.html',
    '/contact-us': 'contact-us.html'
}

application_mapping = [
    ('/login.do', LoginAction),
    ('/.*', ManagerPages)
]

application_debug = True
application_directory = os.path.dirname(__file__)
application = webapp.WSGIApplication(application_mapping, debug=application_debug)

###
### main
def main():
    util.run_wsgi_app(application)

###
### init
if __name__ == '__main__':
    main()



# --------
# app.yaml
# --------

application: app_name
version: 1
runtime: python
api_version: 1

skip_files: |
  ^(.*/)?(
  (app\.yaml)|
  (app\.yml)|
  (index\.yaml)|
  (index\.yml)|
  (#.*#)|
  (.*~)|
  (.*\.py[co])|
  )$

handlers:
- url: /common
  static_dir: common
  
- url: /favicon.ico
  static_files: common/favicon.ico
  upload: common/favicon.ico
  
- url: /.*
  script: main.py

Revision: 45865
at May 10, 2011 03:05 by alejandrombernardis


Initial Code
# -------
# main.py
# -------

###
### imports
import os
import datetime
from google.appengine.ext import webapp
from google.appengine.ext.webapp import template
from google.appengine.ext.webapp import util

###
### manager
class BaseRequestHandler(webapp.RequestHandler):
    def trace(self, value):
        self.response.out.write('<pre>%s</pre>' % value)
        
    def render(self, tmpl_name, tmpl_value={}):
        tmpl_name = self.getTemplate(tmpl_name)
        tmpl_value.update(application_metadata)
        
        self.response.out.write(
            template.render(tmpl_name, tmpl_value, debug=application_debug))
        
    def http_error(self):
        self.error(404)
        return self.getTemplate('error.html')
        
    def getTemplate(self, tmpl_name):
        if tmpl_name == None or len(tmpl_name) == 0:
            return self.http_error()
            
        path = os.path.join(
            application_directory, os.path.join('templates', tmpl_name))
        
        if os.path.exists(path) != True:
            return self.http_error()
            
        return path

###
### pages
class ManagerPages(BaseRequestHandler):
    def get(self):
        self.render(application_templates.get(self.request.path))
        
###
### actions
class LoginAction(BaseRequestHandler):
    def get(self):
        self.render('login.html')
        
    def post(self):
        values = {
            'username': self.request.get('username'),
            'password': self.request.get('password')
        }
        self.render('login.html', values)
        
###
### config

application_metadata = {
    'html_title': 'KC | KirikaCode',
    'html_copyright_date': '2008 - ' + str(datetime.datetime.now().year),
    'html_meta_description': 'Kirika Code is a software and interaction design company',
    'html_meta_keywords': 'kirika, code, as3, python, java, web, w3c, css, javascript, software, interactive, sem, seo'
}

application_templates = {
    '/': 'index.html',
    '/about-us': 'about-us.html',
    '/services': 'services.html',
    '/portfolio': 'portfolio.html',
    '/contact-us': 'contact-us.html'
}

application_mapping = [
    ('/login.do', LoginAction),
    ('/.*', ManagerPages)
]

application_debug = True
application_directory = os.path.dirname(__file__)
application = webapp.WSGIApplication(application_mapping, debug=application_debug)

###
### main
def main():
    util.run_wsgi_app(application)

###
### init
if __name__ == '__main__':
    main()



# --------
# app.yaml
# --------

application: app_name
version: 1
runtime: python
api_version: 1

skip_files: |
  ^(.*/)?(
  (app\.yaml)|
  (app\.yml)|
  (index\.yaml)|
  (index\.yml)|
  (#.*#)|
  (.*~)|
  (.*\.py[co])|
  )$

handlers:
- url: /common
  static_dir: common
  
- url: /favicon.ico
  static_files: common/favicon.ico
  upload: common/favicon.ico
  
- url: /.*
  script: main.py

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

Initial Description

                                

Initial Title
Py | Google App Engine (BaseApp)

Initial Tags

                                

Initial Language
Python