Source

it-navi-clone / fb.py

Full commit
# -*- coding:utf-8 -*-

from google.appengine.ext import webapp
import facebook.wsgi as facebook

    
apikey = ''
secretkey = ''
try:
    import fbconfig
    apikey = fbconfig.apikey
    secretkey = fbconfig.secretkey
except:
    pass

class FbBaseHandler(webapp.RequestHandler):
    def __init__(self, *args, **kwargs):
        super(FbBaseHandler, self).__init__(*args, **kwargs)
        self.facebook = facebook.Facebook(apikey, secretkey)

    def check_session(self):
        return self.facebook.check_session(self.request)

           

    @property
    def fbuid(self):
        return self.facebook.uid

    def fb_redirect(self, url):
        if self.facebook.in_canvas:
            self.out.write('<fb:redirect url="%s" />' % url)
        else:
            self.redirect(url)


def require_login(method):
    def wrap(self, *args, **kw):
        if not self.check_session():
            self.fb_redirect(self.facebook.get_login_url())
            return
        else:
            return method(self, *args, **kw)
    return wrap

            
        

class FbCanvasHandler(FbBaseHandler):

    @require_login
    def get(self):
        pass

    @require_login
    def post(self):
        self.response.out.write('welcome <fb:name uid="%(uid)s" useyou="false" />' % dict(uid=self.fbuid))