<s> import re <EOL> import facebook <EOL> from django . http import HttpResponse , HttpResponseRedirect <EOL> from django . core . exceptions import ImproperlyConfigured <EOL> from django . conf import settings <EOL> try : <EOL> from threading import local <EOL> except ImportError : <EOL> from django . utils . _threading_local import local <EOL> __all__ = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> _thread_locals = local ( ) <EOL> class Facebook ( facebook . Facebook ) : <EOL> def redirect ( self , url ) : <EOL> """<STR_LIT>""" <EOL> if self . in_canvas : <EOL> return HttpResponse ( '<STR_LIT>' % ( url , ) ) <EOL> elif re . search ( "<STR_LIT>" , url . lower ( ) ) : <EOL> return HttpResponse ( '<STR_LIT>' % url ) <EOL> else : <EOL> return HttpResponseRedirect ( url ) <EOL> def get_facebook_client ( ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> return _thread_locals . facebook <EOL> except AttributeError : <EOL> raise ImproperlyConfigured ( '<STR_LIT>' ) <EOL> def require_login ( next = None , internal = None ) : <EOL> """<STR_LIT>""" <EOL> def decorator ( view ) : <EOL> def newview ( request , * args , ** kwargs ) : <EOL> next = newview . next <EOL> internal = newview . internal <EOL> try : <EOL> fb = request . facebook <EOL> except : <EOL> raise ImproperlyConfigured ( '<STR_LIT>' ) <EOL> if internal is None : <EOL> internal = request . facebook . internal <EOL> if callable ( next ) : <EOL> next = next ( request . path ) <EOL> elif isinstance ( next , int ) : <EOL> next = '<STR_LIT:/>' . join ( request . path . split ( '<STR_LIT:/>' ) [ next + <NUM_LIT:1> : ] ) <EOL> elif next is None and fb . callback_path and request . path . startswith ( fb . callback_path ) : <EOL> next = request . path [ len ( fb . callback_path ) : ] <EOL> elif not isinstance ( next , str ) : <EOL> next = '<STR_LIT>' <EOL> if not fb . check_session ( request ) : <EOL> return fb . redirect ( fb . get_login_url ( next = next ) ) <EOL> if internal and request . method == '<STR_LIT:GET>' and fb . app_name : <EOL> return fb . redirect ( '<STR_LIT>' % ( fb . get_app_url ( ) , next ) ) <EOL> return view ( request , * args , ** kwargs ) <EOL> newview . next = next <EOL> newview . internal = internal <EOL> return newview <EOL> return decorator <EOL> def require_add ( next = None , internal = None , on_install = None ) : <EOL> """<STR_LIT>""" <EOL> def decorator ( view ) : <EOL> def newview ( request , * args , ** kwargs ) : <EOL> next = newview . next <EOL> internal = newview . internal <EOL> try : <EOL> fb = request . facebook <EOL> except : <EOL> raise ImproperlyConfigured ( '<STR_LIT>' ) <EOL> if internal is None : <EOL> internal = request . facebook . internal <EOL> if callable ( next ) : <EOL> next = next ( request . path ) <EOL> elif isinstance ( next , int ) : <EOL> next = '<STR_LIT:/>' . join ( request . path . split ( '<STR_LIT:/>' ) [ next + <NUM_LIT:1> : ] ) <EOL> elif next is None and fb . callback_path and request . path . startswith ( fb . callback_path ) : <EOL> next = request . path [ len ( fb . callback_path ) : ] <EOL> else : <EOL> next = '<STR_LIT>' <EOL> if not fb . check_session ( request ) : <EOL> if fb . added : <EOL> if request . method == '<STR_LIT:GET>' and fb . app_name : <EOL> return fb . redirect ( '<STR_LIT>' % ( fb . get_app_url ( ) , next ) ) <EOL> return fb . redirect ( fb . get_login_url ( next = next ) ) <EOL> else : <EOL> return fb . redirect ( fb . get_add_url ( next = next ) ) <EOL> if not fb . added : <EOL> return fb . redirect ( fb . get_add_url ( next = next ) ) <EOL> if '<STR_LIT>' in request . GET and callable ( on_install ) : <EOL> on_install ( request ) <EOL> if internal and request . method == '<STR_LIT:GET>' and fb . app_name : <EOL> return fb . redirect ( '<STR_LIT>' % ( fb . get_app_url ( ) , next ) ) <EOL> return view ( request , * args , ** kwargs ) <EOL> newview . next = next <EOL> newview . internal = internal <EOL> return newview <EOL> return decorator <EOL> try : <EOL> import decorator <EOL> except ImportError : <EOL> pass <EOL> else : <EOL> def updater ( f ) : <EOL> def updated ( * args , ** kwargs ) : <EOL> original = f ( * args , ** kwargs ) <EOL> def newdecorator ( view ) : <EOL> return decorator . new_wrapper ( original ( view ) , view ) <EOL> return decorator . new_wrapper ( newdecorator , original ) <EOL> return decorator . new_wrapper ( updated , f ) <EOL> require_login = updater ( require_login ) <EOL> require_add = updater ( require_add ) <EOL> class FacebookMiddleware ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , api_key = None , secret_key = None , app_name = None , callback_path = None , internal = None ) : <EOL> self . api_key = api_key or settings . FACEBOOK_API_KEY <EOL> self . secret_key = secret_key or settings . FACEBOOK_SECRET_KEY <EOL> self . app_name = app_name or getattr ( settings , '<STR_LIT>' , None ) <EOL> self . callback_path = callback_path or getattr ( settings , '<STR_LIT>' , None ) <EOL> self . internal = internal or getattr ( settings , '<STR_LIT>' , True ) <EOL> self . proxy = None <EOL> if getattr ( settings , '<STR_LIT>' , False ) : <EOL> self . proxy = settings . HTTP_PROXY <EOL> def process_request ( self , request ) : <EOL> _thread_locals . facebook = request . facebook = Facebook ( self . api_key , self . secret_key , app_name = self . app_name , callback_path = self . callback_path , internal = self . internal , proxy = self . proxy ) <EOL> if not self . internal and '<STR_LIT>' in request . session and '<STR_LIT>' in request . session : <EOL> request . facebook . session_key = request . session [ '<STR_LIT>' ] <EOL> request . facebook . uid = request . session [ '<STR_LIT>' ] <EOL> def process_response ( self , request , response ) : <EOL> if not self . internal and request . facebook . session_key and request . facebook . uid : <EOL> request . session [ '<STR_LIT>' ] = request . facebook . session_key <EOL> request . session [ '<STR_LIT>' ] = request . facebook . uid <EOL> return response </s>
