<s> from django . conf import settings <EOL> from django . contrib import auth <EOL> import facebook <EOL> import datetime <EOL> class DjangoFacebook ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , user ) : <EOL> self . user = user <EOL> self . uid = user [ '<STR_LIT>' ] <EOL> self . graph = facebook . GraphAPI ( user [ '<STR_LIT>' ] ) <EOL> class FacebookDebugCanvasMiddleware ( object ) : <EOL> """<STR_LIT>""" <EOL> def process_request ( self , request ) : <EOL> cp = request . POST . copy ( ) <EOL> request . POST = cp <EOL> request . POST [ '<STR_LIT>' ] = settings . FACEBOOK_DEBUG_SIGNEDREQ <EOL> return None <EOL> class FacebookDebugCookieMiddleware ( object ) : <EOL> """<STR_LIT>""" <EOL> def process_request ( self , request ) : <EOL> cookie_name = "<STR_LIT>" + settings . FACEBOOK_APP_ID <EOL> request . COOKIES [ cookie_name ] = settings . FACEBOOK_DEBUG_COOKIE <EOL> return None <EOL> class FacebookDebugTokenMiddleware ( object ) : <EOL> """<STR_LIT>""" <EOL> def process_request ( self , request ) : <EOL> user = { <EOL> '<STR_LIT>' : settings . FACEBOOK_DEBUG_UID , <EOL> '<STR_LIT>' : settings . FACEBOOK_DEBUG_TOKEN , <EOL> } <EOL> request . facebook = DjangoFacebook ( user ) <EOL> return None <EOL> class FacebookMiddleware ( object ) : <EOL> """<STR_LIT>""" <EOL> def get_fb_user_cookie ( self , request ) : <EOL> """<STR_LIT>""" <EOL> fb_user = facebook . get_user_from_cookie ( <EOL> request . COOKIES , <EOL> settings . FACEBOOK_APP_ID , <EOL> settings . FACEBOOK_SECRET_KEY ) <EOL> if fb_user : <EOL> fb_user [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> return fb_user <EOL> def get_fb_user_canvas ( self , request ) : <EOL> """<STR_LIT>""" <EOL> fb_user = None <EOL> if request . POST . get ( '<STR_LIT>' ) : <EOL> signed_request = request . POST [ "<STR_LIT>" ] <EOL> data = facebook . parse_signed_request ( signed_request , <EOL> settings . FACEBOOK_SECRET_KEY ) <EOL> if data and data . get ( '<STR_LIT>' ) : <EOL> fb_user = data [ '<STR_LIT:user>' ] <EOL> fb_user [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> fb_user [ '<STR_LIT>' ] = data [ '<STR_LIT>' ] <EOL> fb_user [ '<STR_LIT>' ] = data [ '<STR_LIT>' ] <EOL> return fb_user <EOL> def get_fb_user ( self , request ) : <EOL> """<STR_LIT>""" <EOL> fb_user = None <EOL> methods = [ '<STR_LIT>' , '<STR_LIT>' ] <EOL> for method in methods : <EOL> fb_user = getattr ( self , method ) ( request ) <EOL> if ( fb_user ) : <EOL> break <EOL> return fb_user <EOL> def process_request ( self , request ) : <EOL> """<STR_LIT>""" <EOL> fb_user = self . get_fb_user ( request ) <EOL> request . facebook = DjangoFacebook ( fb_user ) if fb_user else None <EOL> if fb_user and request . user . is_anonymous ( ) : <EOL> user = auth . authenticate ( fb_uid = fb_user [ '<STR_LIT>' ] , <EOL> fb_graphtoken = fb_user [ '<STR_LIT>' ] ) <EOL> if user : <EOL> user . last_login = datetime . datetime . now ( ) <EOL> user . save ( ) <EOL> request . user = user <EOL> return None </s>
