Monday, 26 August 2013

Organizing webapp2 application

Organizing webapp2 application

Im trying to find the right way to organize my webapp2 application. This
is the file structure:
/my-app
app.yaml
main.app
/views
index.html
/handlers
__init__.py
base.py
home.py
handlers/base.py
import webapp2
from webapp2_extras import jinja2
class BaseHandler(webapp2.RequestHandler):
@webapp2.cached_property
def jinja2(self):
return jinja2.get_jinja2(app=self.app)
def render_response(self, _template, **context):
rendered_template = self.jinja2.render_template(_template +
'.html', **context)
return self.response.write(rendered_template)
All the handlers extends the BaseHandler
handlers/home.py
from base import BaseHandler
# Not sure about importing BaseHandler here
class Name(BaseHandler):
def post(self, name=None):
context = {
'name': name,
}
self.render_response('index', **context)
and this is the main.py file
import os
import webapp2
from webapp2_extras import jinja2
CONFIG = {
'debug': True,
'webapp2_extras.jinja2': {
'autoescape': True,
'template_path': os.path.join(os.path.dirname(__file__), 'views'),
'globals': {
'url_for': webapp2.uri_for,
},
},
}
app = webapp2.WSGIApplication([
webapp2.Route('/<name>', handler='handlers.home.Name', name='name'),
], config = CONFIG, debug = CONFIG['debug'])
Should I put the routes in the same file? Because all the handlers extends
the BaseHandler i´m not sure if that is the right way to do that. Also I´m
importing webapp2 and extras twice.

No comments:

Post a Comment