Monday 10 December 2012

Python Webapp2 framework and Users Services




Webapp2 is a simple web application framework included in the App Engine.
A webapp2 application has two parts: 

* RequestHandler classes that process requests and build responses.
* WSGIApplication instance that routes incoming requests to handlers based 
   on the URL.

This is a code which defines a simple request handler.
import webapp2

class MainPage(webapp2.RequestHandler):
    def get(self):
        self.response.headers['Content-Type'] = 'text/plain'
        self.response.out.write('Hello, webapp World!')

app = webapp2.WSGIApplication([('/', MainPage)],
                              debug=True)
Here MainPage is mapped to the root URL (). The get method sets properties on self.response to prepare the response, then exits. webapp2 sends a response based on the final state of the MainPage instance.


User Services

import webapp2

from google.appengine.api import users

class MainPage(webapp2.RequestHandler):
    def get(self):
        user = users.get_current_user()

        if user:
            self.response.headers['Content-Type'] = 'text/plain'
            self.response.out.write('Hello, ' + user.nickname())
        else:
            self.redirect(users.create_login_url(self.request.uri))

app = webapp2.WSGIApplication([('/', MainPage)],
                              debug=True)
If the user is already signed in to your application, get_current_user() returns the User object for the user. Otherwise, it returns None. If the user has signed in, display a personalized message using the user.nickname(). If the user has not signed in, tell webapp2 to redirect the user's browser to the Google account sign-in screen.






[Expecting Your Valuable Comments]
Thank You

No comments:

Post a Comment