开发者

Python - Inheritance - Facebook application

开发者 https://www.devze.com 2023-04-05 06:07 出处:网络
I have a question about how the code run in inheritance in Python. It might look like a dummy question somehow, but I a new to Python.

I have a question about how the code run in inheritance in Python. It might look like a dummy question somehow, but I a new to Python.

This a code snippet from some Facebook application I am working on:

class BaseHandler(webapp.RequestHandler):
    facebook = None
    user = None

    def initialize(self, request, response):
        """General initialization for every request"""
        super(BaseHandler, self).initialize(request, response)

        try:
            self.init_facebook()
        except Exception, ex:
            self.log_exception(ex)
            raise
   def init_facebook(self):
    """Sets up the request specific Facebook and user instance"""
    facebook = Facebook()
    user = None

    # Initially Facebook request comes in as a POST with a signed_request
    if u'signed_request' in self.request.POST:
        facebook.load_signed_request(self.request.get('signed_request'))
        # We reset the method to GET because a request from Facebook with a
        # signed_request uses POST for security reasons, despite it
        # actually being a GET. In a web application this causes loss of request.POST data.
        self.request.method = u'GET'
        self.set_cookie(
            'u', facebook.user_cookie, datetime.timedelta(minutes=1440))
    elif 'u' in self.request.cookies:
        facebook.load_signed_request(self.request.cookies.get('u'))

    # Try to load or create a user object
    if facebook.user_id:
        user = User.get_by_key_name(facebook.user_id)
        if user:
            # Update stored access_token
            if facebook.access_token and \
                    facebook.access_token != user.access_token:
                user.access_token = facebook.access_token
                user.put()
            # Refresh data if we failed in doing so after a realtime ping.
            if user.dirty:
                user.refresh_data()
            # Restore stored access_token if necessary
            if not facebook.access_token:
                facebook.access_token = user.access_token

        if not user and facebook.access_token:
            me = facebook.api(u'/me', {u'fields': _USER_FIELDS})
            try:
                friends = [user[u'id'] for user in me[u'friends'][u'data']]
                user = User(key_name=facebook.user_id,
                    user_id=facebook.user_id, friends=friends,
                    access_token=facebook.access_token, name=me[u'name'],
                    email=me.get(u'email'), picture开发者_如何转开发=me[u'picture'])
                user.put()
            except KeyError, ex:
                pass # Ignore if can't get the minimum fields.

    self.facebook = facebook
    self.user = user

This is another class that inherits from BaseHandler

class RecentRunsHandler(BaseHandler):
    """Show recent runs for the user and friends"""
    def get(self):
        if self.user:
            friends = {}
            for friend in select_random(
                    User.get_by_key_name(self.user.friends), 30):
                friends[friend.user_id] = friend

            self.render(u'runs',
                friends=friends,
                user_recent_runs=Run.find_by_user_ids(
                    [self.user.user_id], limit=5),
                friends_runs=Run.find_by_user_ids(friends.keys()),
            )
        else:
            self.render(u'welcome')

Does the initialize function in the BaseHandler get called when the RecentRunsHandler is called?

I am asking this because if after the user "allow" the application (and the user data is saved in the database) ... The application still redirects him to the welcoming page where the Facebook-login button exists.

To make it more clear, the application can't know that the user has authorized him before.


Probably not. Perhaps you should def initialize to def __init__? Python objects get instantiated through the __init__ method, not through any initialize() method. Since you don't seem to have any explicit calls to initialize(), it will probably not be called.

0

精彩评论

暂无评论...
验证码 换一张
取 消