<s> from django . core . urlresolvers import get_callable <EOL> from django . http import HttpResponseBadRequest , HttpResponseRedirect <EOL> from django . core . cache import cache <EOL> import time <EOL> from . settings import THROTTLING , THROTTLING_CACHE_PREFIX , THROTTLING_CACHE_EXPIRE , THROTTLING_CACHE_KEY_PATTERNS , THROTTLING_ENABLED , THROTTLING_IGNORE_ADMINS <EOL> def ip ( request ) : <EOL> x_forwarded_for = request . META . get ( '<STR_LIT>' ) <EOL> if x_forwarded_for : <EOL> ip = x_forwarded_for . split ( '<STR_LIT:U+002C>' ) [ - <NUM_LIT:1> ] . strip ( ) <EOL> else : <EOL> ip = request . META . get ( '<STR_LIT>' ) <EOL> return ip <EOL> class Throttle ( object ) : <EOL> def __init__ ( self , request , view_func , view_args , view_kwargs ) : <EOL> self . request = request <EOL> self . view_func = view_func <EOL> self . view_args = view_args , <EOL> self . view_kwargs = view_kwargs <EOL> self . view_name = '<STR_LIT>' % ( view_func . __module__ , view_func . __name__ ) <EOL> self . view_throttling = THROTTLING . get ( self . view_name , { } ) <EOL> self . method = request . method . lower ( ) <EOL> def get_cache_key ( self ) : <EOL> if '<STR_LIT>' in self . view_throttling : <EOL> if not self . request . path . startswith ( self . view_throttling [ '<STR_LIT>' ] ) : <EOL> return None , None <EOL> timeouts = ( <EOL> ( '<STR_LIT>' , self . view_throttling . get ( self . method ) ) , <EOL> ( '<STR_LIT>' , self . view_throttling . get ( '<STR_LIT:all>' ) ) , <EOL> ( '<STR_LIT>' , THROTTLING . get ( self . method ) ) , <EOL> ( '<STR_LIT>' , THROTTLING . get ( '<STR_LIT:all>' ) ) , <EOL> ) <EOL> format_args = { <EOL> '<STR_LIT>' : THROTTLING_CACHE_PREFIX , <EOL> '<STR_LIT>' : self . view_name , <EOL> '<STR_LIT>' : self . request . user . id , <EOL> '<STR_LIT>' : ip ( self . request ) , <EOL> '<STR_LIT>' : self . request . method <EOL> } <EOL> if hasattr ( self . request , '<STR_LIT>' ) and self . request . session . session_key : <EOL> format_args [ '<STR_LIT>' ] = self . request . session . session_key <EOL> for pattern_key , timeout in timeouts : <EOL> if timeout is not None : <EOL> if isinstance ( timeout , basestring ) and timeout . startswith ( '<STR_LIT>' ) : <EOL> callback_name = timeout . split ( '<STR_LIT>' ) [ <NUM_LIT:1> ] <EOL> return get_callable ( callback_name ) ( self . request , self . view_func , self . view_args , self . view_kwargs ) <EOL> return THROTTLING_CACHE_KEY_PATTERNS [ pattern_key ] % format_args , timeout <EOL> return None , None <EOL> def check ( self ) : <EOL> if not THROTTLING_ENABLED : <EOL> return <EOL> if THROTTLING_IGNORE_ADMINS and self . request . user . is_superuser : <EOL> return <EOL> cache_key , timeout = self . get_cache_key ( ) <EOL> if not cache_key : <EOL> return <EOL> if timeout is False : <EOL> return HttpResponseBadRequest ( ) <EOL> if ( timeout is None ) or ( timeout is <NUM_LIT:0> ) : <EOL> return <EOL> if isinstance ( timeout , basestring ) : <EOL> if timeout . startswith ( '<STR_LIT:/>' ) : <EOL> return HttpResponseRedirect ( timeout ) <EOL> else : <EOL> callback = get_callable ( timeout ) <EOL> maintenance_bundle = { <EOL> '<STR_LIT>' : self . view_func , <EOL> '<STR_LIT>' : self . view_args , <EOL> '<STR_LIT>' : self . view_kwargs <EOL> } <EOL> return callback ( self . request , maintenance_bundle ) <EOL> now = int ( time . time ( ) * <NUM_LIT:1000> ) <EOL> last_access = cache . get ( cache_key , <NUM_LIT:0> ) <EOL> delta = now - last_access <EOL> if delta >= timeout : <EOL> cache . set ( cache_key , now , THROTTLING_CACHE_EXPIRE ) <EOL> return <EOL> congestion_view = self . view_throttling . get ( '<STR_LIT>' ) or THROTTLING . get ( '<STR_LIT>' ) <EOL> if congestion_view : <EOL> if congestion_view . startswith ( '<STR_LIT:/>' ) : <EOL> return HttpResponseRedirect ( congestion_view ) <EOL> else : <EOL> callback = get_callable ( congestion_view ) <EOL> congestion_bundle = { <EOL> '<STR_LIT>' : self . view_func , <EOL> '<STR_LIT>' : self . view_args , <EOL> '<STR_LIT>' : self . view_kwargs , <EOL> '<STR_LIT>' : timeout , <EOL> '<STR_LIT>' : delta , <EOL> } <EOL> return callback ( self . request , congestion_bundle ) <EOL> return HttpResponseBadRequest ( ) </s>
