<s> import logging <EOL> logger = logging . getLogger ( __name__ ) <EOL> from datetime import datetime , timedelta <EOL> class SessionBase ( object ) : <EOL> def __init__ ( self ) : <EOL> self . app_is_authenticated , self . access_token , self . signed_request = True , None , None <EOL> self . token_expires , self . user_id , self . me = None , None , None <EOL> self . app_requests = [ ] <EOL> def store_token ( self , * args , ** kwargs ) : <EOL> raise AttributeError ( '<STR_LIT>' ) <EOL> def modified ( self ) : <EOL> raise AttributeError ( '<STR_LIT>' ) <EOL> class Meta : <EOL> abstract = True <EOL> class FBSession ( SessionBase ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , request ) : <EOL> if request == None : <EOL> raise AttributeError ( '<STR_LIT>' ) <EOL> if hasattr ( request , '<STR_LIT>' ) : <EOL> raise AttributeError ( '<STR_LIT>' ) <EOL> self . fb_session = self . get_fb_session ( request ) <EOL> self . request = request <EOL> def get_fb_session ( self , request ) : <EOL> fb = request . session . get ( '<STR_LIT>' , None ) <EOL> logger . debug ( '<STR_LIT>' ) <EOL> if not fb : <EOL> logger . debug ( '<STR_LIT>' ) <EOL> request . session . update ( { '<STR_LIT>' : { '<STR_LIT>' : True } } ) <EOL> request . session . modified = True <EOL> fb = request . session [ '<STR_LIT>' ] <EOL> return fb <EOL> def modified ( self , who = '<STR_LIT>' ) : <EOL> self . request . session . modified = True <EOL> logger . debug ( '<STR_LIT>' % ( who , self . fb_session ) ) <EOL> @ property <EOL> def app_is_authenticated ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . fb_session [ '<STR_LIT>' ] if self . fb_session . get ( '<STR_LIT>' , False ) else True <EOL> @ app_is_authenticated . setter <EOL> def app_is_authenticated ( self , status ) : <EOL> if status in [ True , False ] : <EOL> self . fb_session [ '<STR_LIT>' ] = status <EOL> else : <EOL> raise TypeError ( '<STR_LIT>' , '<STR_LIT>' % status ) <EOL> @ property <EOL> def access_token ( self ) : <EOL> """<STR_LIT>""" <EOL> logger . debug ( '<STR_LIT>' % ( self . token_expires , type ( self . token_expires ) ) ) <EOL> if self . token_expires and self . token_expires < datetime . now ( ) : <EOL> logger . debug ( '<STR_LIT>' % self . fb_session . get ( '<STR_LIT>' ) ) <EOL> return None <EOL> else : <EOL> return self . fb_session . get ( '<STR_LIT>' , None ) <EOL> @ access_token . setter <EOL> def access_token ( self , token ) : <EOL> self . fb_session [ '<STR_LIT>' ] = token <EOL> self . modified ( '<STR_LIT>' ) <EOL> @ property <EOL> def token_expires ( self ) : <EOL> return self . fb_session . get ( '<STR_LIT>' , None ) <EOL> @ token_expires . setter <EOL> def token_expires ( self , expires ) : <EOL> if isinstance ( expires , datetime ) or isinstance ( expires , type ( None ) ) : <EOL> logger . debug ( '<STR_LIT>' % expires ) <EOL> elif isinstance ( expires , basestring ) : <EOL> seconds = int ( expires ) <EOL> logger . debug ( '<STR_LIT>' % seconds ) <EOL> expires = datetime . now ( ) + timedelta ( seconds = seconds ) <EOL> else : <EOL> raise TypeError ( '<STR_LIT>' % type ( expires ) ) <EOL> self . fb_session [ '<STR_LIT>' ] = expires <EOL> self . modified ( '<STR_LIT>' ) <EOL> def store_token ( self , token = None , expires = None ) : <EOL> if token == None : <EOL> self . _clear_token ( ) <EOL> else : <EOL> self . access_token = token <EOL> if not expires : <EOL> expires = datetime . now ( ) + timedelta ( hours = <NUM_LIT:1> ) <EOL> if isinstance ( self . token_expires , datetime ) and self . token_expires > expires : <EOL> pass <EOL> else : <EOL> self . token_expires = expires <EOL> def _clear_token ( self ) : <EOL> self . access_token = None <EOL> self . user_id = None <EOL> self . app_is_authenticated = False <EOL> @ property <EOL> def user_id ( self ) : <EOL> user_id = self . fb_session . get ( '<STR_LIT>' ) <EOL> return int ( user_id ) if user_id else None <EOL> @ user_id . setter <EOL> def user_id ( self , id ) : <EOL> if id == None : <EOL> self . fb_session [ '<STR_LIT>' ] = None <EOL> self . fb_session [ '<STR_LIT>' ] = None <EOL> self . app_is_authenticated = False <EOL> else : <EOL> self . fb_session [ '<STR_LIT>' ] = id <EOL> self . app_is_authenticated = True <EOL> self . modified ( '<STR_LIT>' ) <EOL> @ property <EOL> def me ( self ) : <EOL> return self . fb_session . get ( '<STR_LIT>' , None ) <EOL> @ me . setter <EOL> def me ( self , value ) : <EOL> self . fb_session [ '<STR_LIT>' ] = value <EOL> self . app_is_authenticated = True <EOL> self . modified ( '<STR_LIT>' ) <EOL> @ property <EOL> def signed_request ( self ) : <EOL> return self . fb_session . get ( '<STR_LIT>' , None ) <EOL> @ signed_request . setter <EOL> def signed_request ( self , parsed_request ) : <EOL> self . fb_session [ '<STR_LIT>' ] = parsed_request <EOL> self . app_is_authenticated = True if getattr ( parsed_request , '<STR_LIT>' , False ) else False <EOL> self . modified ( '<STR_LIT>' ) <EOL> @ property <EOL> def user ( self ) : <EOL> raise AttributeError ( '<STR_LIT>' ) <EOL> @ user . setter <EOL> def user ( self , user ) : <EOL> raise AttributeError ( '<STR_LIT>' ) <EOL> @ property <EOL> def app_requests ( self ) : <EOL> ar = self . fb_session . get ( '<STR_LIT>' , None ) <EOL> return ar . split ( '<STR_LIT:U+002C>' ) if ar else [ ] <EOL> @ app_requests . setter <EOL> def app_requests ( self , item ) : <EOL> from facebook . modules . profile . application . models import Request <EOL> if isinstance ( item , list ) : <EOL> self . fb_session [ '<STR_LIT>' ] = '<STR_LIT:U+002C>' . join ( str ( i ) for i in item ) <EOL> elif isinstance ( item , Request ) : <EOL> ar = self . fb_session . get ( '<STR_LIT>' , [ ] ) . split ( '<STR_LIT:U+002C>' ) <EOL> ar . append ( str ( item . id ) ) <EOL> self . fb_session [ '<STR_LIT>' ] = '<STR_LIT:U+002C>' . join ( ar ) <EOL> elif isinstance ( item , ( basestring , int ) ) : <EOL> ar = self . fb_session . get ( '<STR_LIT>' , [ ] ) . split ( '<STR_LIT:U+002C>' ) <EOL> ar . append ( str ( item ) ) <EOL> self . fb_session [ '<STR_LIT>' ] = '<STR_LIT:U+002C>' . join ( ar ) <EOL> else : <EOL> raise AttributeError , '<STR_LIT>' % type ( item ) <EOL> self . modified ( '<STR_LIT>' ) <EOL> class FBSessionNoOp ( SessionBase ) : <EOL> def __init__ ( self ) : <EOL> super ( FBSessionNoOp , self ) . __init__ ( ) <EOL> logger . debug ( '<STR_LIT>' ) <EOL> self . _modified = False <EOL> def store_token ( self , token = None , expires = None ) : <EOL> self . access_token = token <EOL> self . token_expires = expires <EOL> def modified ( self , who = '<STR_LIT>' , * args , ** kwargs ) : <EOL> self . _modified = True <EOL> logger . debug ( '<STR_LIT>' % who ) <EOL> return False <EOL> def get_session ( request = None ) : <EOL> if not request : <EOL> return FBSessionNoOp ( ) <EOL> else : <EOL> if hasattr ( request , '<STR_LIT>' ) : <EOL> return request . fb_session <EOL> else : <EOL> return FBSession ( request ) </s>
