Sure. Most of my user-facing pages subclass "templateHandler." I have
simplified it slightly for the sake of brevity, but I think you'll get the
idea.
Robert
class templateHandler(webapp.RequestHandler):
template_vals = {}
def handleTemplate(self,templatename, **kwargs):
""" Find the proper template and render it, any arguments
the template expects must passed to this method.
Check if the user is accessing the site via a blackberry.
If so check the "mobile" templates directory first.
"""
directories = directories=['templates/default']
if "blackberry" in self.request.headers.get('user_agent').lower():
directories.insert(0,'templates/mobile')
lookup = TemplateLookup(directories=directories)
template = lookup.get_template(templatename)
self.response.out.write( template.render(**kwargs) )
def handleContinue(self,continueTo = None,defaultTo = None,
permanent=False):
""" Handle redirecting the user, this is used to construct
work-flows.
Return: True if redirected, False if not.
Usage:
class MyHandler(templateHandler):
def get(self):
nextpage = self.request.get('continue')
if self.handleContinue(nextpage,'/dashboard'):
return
"""
if continueTo:
self.redirect(continueTo, permanent=permanent)
return True
elif defaultTo:
self.redirect(defaultTo, permanent=permanent)
return True
return False
def handleError(self,code=500,message="Error 500: A server error was
encountered."):
self.error(500)
self.response.out.write(message)
return
def get(self):
if users.get_current_user():
self.template_vals['logout_url'] =
users.create_logout_url(self.request.uri)
else:
self.redirect(users.create_login_url(self.request.uri))
return
self.handleTemplate("base.html",**self.template_vals)
class MainHandler(templateHandler):
""" This is a very simple handler. It will simply call the
default template.
"""
def __init__(self, **kwargs):
templateHandler.__init__(self, **kwargs)
self.template_vals['title'] = "Welcome"
self.template_vals['page_id'] = "mainpage"
self.template_vals['body_tmplt'] = "default"
return
class MainHandler(templateHandler):
""" This is a simple handler. It will call the default template.
It will also set some template parameters based on the
user name.
"""
def get(self):
if users.get_current_user():
self.template_vals['logout_url'] =
users.create_logout_url(self.request.uri)
self.template_vals['title'] = "Welcome %s " %
str(users.get_current_user())
else:
self.template_vals['title'] = "You must log in."
self.handleTemplate("simple.html",**self.template_vals)
On Thu, Dec 10, 2009 at 4:37 PM, PatHaugen <[email protected]> wrote:
> Robert, very nice! I didn't know you could do that..
>
> For others that may view this thread, here is a solution I used while
> waiting for something 'cleaner'
>
> mainpage_get.py
> def MainPage_Get():
> return xxx,'/somewhere'
>
> main.py
> from mainpage_get import MainPage_Get
> class MainPage(webapp.RequestHandler):
> def get(self):
> xxx,redirect = MainPage_Get()
> if redirect: self.redirect(redirect)
>
> Perhaps that helps someone else, but I like Robert's solution for this
> specific purpose.
>
> Robert, could you provide a very simple example of what you meant in
> your last paragraph? I follow, but can't visualize 'how' to accomplish
> what you mean..
>
> Thanks!
>
> On Dec 9, 8:37 pm, Robert Kluin <[email protected]> wrote:
> > In your MailPage_Get code it does not look like "self" will reference the
> > request handler. Try something like:
> >
> > main.py
> > from mainpage_get import MainPage_Get
> > class MainPage(webapp.RequestHandler):
> > def get(self):
> > MainPage_Get(self)
> >
> > mainpage_get.py
> > def MainPage_Get(handler):
> > handler.redirect('/')
> >
> > Just something you may want to consider for organizing your code, I have
> a
> > couple "standard" base class that inherit from RequestHandler and define
> > things like error handling, template setup, "continue" handlers
> (redirecting
> > to the next page in a workflow), and so on. I usually subclass one of my
> > "standard" handler classes to build an end user facing class.
> >
> > Robert
> >
> >
> >
> > On Wed, Dec 9, 2009 at 10:29 PM, PatHaugen <[email protected]> wrote:
> > > I've been playing around with modularizing my code, and so far just
> > > one question re: self.redirect
> >
> > > For example, changing this:
> >
> > > main.py
> > > from mainpage_get import MainPage_Get
> > > class MainPage(webapp.RequestHandler):
> > > def get(self):
> > > MainPage_Get()
> >
> > > mainpage_get.py
> > > def MainPage_Get():
> > > self.redirect('/')
> >
> > > So the self.redirect is not going to work in any instance within
> > > another file, is there another option? This is a simple example, my
> > > MainPage_Get is of course very large with various checks via if/else
> > > redirecting the user to various locations..
> >
> > > Wondering what others do in cases such as these.
> >
> > > On Dec 9, 1:10 am, G <[email protected]> wrote:
> > > > Use templates to modularize your output, research {% extends %} {%
> > > > block %} and {{ block.super }}. The block.super trick will enable
> > > > DRYness.
> >
> > >http://code.google.com/appengine/docs/python/gettingstarted/templates.
> ..
> >
> > > > Use import to modularize your code, research 'from foo import bar as
> > > > _baz'. The leading underscore trick will keep library namespaces
> > > > clean (not a typical use case but when namespace polution is an issue
> > > > a leading underscore is the solution).
> >
> > >http://docs.python.org/tutorial/modules.htmlhttp://docs.python.org/re.
> ..
> >
> > > > --
> > > > G
> >
> > > > On Dec 8, 11:25 pm, PatHaugen <[email protected]> wrote:
> >
> > > > > Sorry if this seems simple, but all Google searches for 'Google App
> > > > > Engine include require PHP' or any of the variants went to websites
> > > > > complaining about lack of PHP support in GAE.
> >
> > > > > Here is my question:
> >
> > > > > In PHP, I use include/require to break apart code into separate
> files
> > > > > for simplicity and multiple developer situations. I'm at that point
> in
> > > > > GAE, and was wondering if there is something I could use.
> >
> > > > > As an example, if GAE were PHP I'd do something like:
> >
> > > > > ====
> >
> > > > > main.py
> > > > > include header.py
> > > > > include datastore.py
> > > > > class MainPage(webapp.RequestHandler):
> > > > > def get(self):
> > > > > include mainpage.py
> > > > > def post(self):
> > > > > include post.py
> > > > > include footer.py
> >
> > > > > footer.py
> > > > > application = webapp.WSGIApplication(...
> > > > > etc..
> > > > > def main(): run_wsgi_app(application)
> > > > > if __name__ == "__main__": main()
> >
> > > > > header.py
> > > > > import xxx
> > > > > from xxx
> > > > > etc..
> >
> > > > > datastore.py
> > > > > class xxx(db.Model)..
> > > > > etc..
> >
> > > > > ====
> >
> > > > > Just a quick example.
> >
> > > > > Thoughts?
> >
> > > --
> >
> > > You received this message because you are subscribed to the Google
> Groups
> > > "Google App Engine" group.
> > > To post to this group, send email to [email protected]
> .
> > > To unsubscribe from this group, send email to
> > > [email protected]<google-appengine%[email protected]><google-appengine%2Bunsubscrib
> [email protected]>
> > > .
> > > For more options, visit this group at
> > >http://groups.google.com/group/google-appengine?hl=en.
>
> --
>
> You received this message because you are subscribed to the Google Groups
> "Google App Engine" group.
> To post to this group, send email to [email protected].
> To unsubscribe from this group, send email to
> [email protected]<google-appengine%[email protected]>
> .
> For more options, visit this group at
> http://groups.google.com/group/google-appengine?hl=en.
>
>
>
--
You received this message because you are subscribed to the Google Groups
"Google App Engine" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/google-appengine?hl=en.