<s> """<STR_LIT>""" <EOL> import json <EOL> import time <EOL> from django . http import HttpResponse , HttpResponseServerError <EOL> from django . contrib . auth import authenticate , login <EOL> from rest_framework . request import Request <EOL> from rest_framework . exceptions import PermissionDenied <EOL> from django . core . handlers . wsgi import WSGIRequest <EOL> from ovs . extensions . storage . volatilefactory import VolatileFactory <EOL> from ovs . extensions . generic . volatilemutex import VolatileMutex <EOL> from ovs . log . logHandler import LogHandler <EOL> logger = LogHandler . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> def _find_request ( args ) : <EOL> """<STR_LIT>""" <EOL> for item in args : <EOL> if isinstance ( item , Request ) or isinstance ( item , WSGIRequest ) : <EOL> return item <EOL> def auto_response ( ) : <EOL> """<STR_LIT>""" <EOL> def wrap ( f ) : <EOL> """<STR_LIT>""" <EOL> def new_function ( * args , ** kw ) : <EOL> """<STR_LIT>""" <EOL> results = f ( * args , ** kw ) <EOL> if isinstance ( results , tuple ) or isinstance ( results , list ) : <EOL> return_type , data = results [ <NUM_LIT:0> ] , results [ <NUM_LIT:1> ] <EOL> if len ( results ) == <NUM_LIT:2> : <EOL> if isinstance ( data , dict ) : <EOL> return return_type ( json . dumps ( data ) , content_type = '<STR_LIT:application/json>' ) <EOL> return return_type ( data ) <EOL> else : <EOL> status_code = results [ <NUM_LIT:2> ] <EOL> if isinstance ( data , dict ) : <EOL> return return_type ( json . dumps ( data ) , content_type = '<STR_LIT:application/json>' , status = status_code ) <EOL> return return_type ( data , status = status_code ) <EOL> elif isinstance ( results , HttpResponse ) : <EOL> return results <EOL> else : <EOL> logger . error ( '<STR_LIT>' ) <EOL> return HttpResponseServerError ( ) <EOL> new_function . __name__ = f . __name__ <EOL> new_function . __module__ = f . __module__ <EOL> return new_function <EOL> return wrap <EOL> def limit ( amount , per , timeout ) : <EOL> """<STR_LIT>""" <EOL> def wrap ( f ) : <EOL> """<STR_LIT>""" <EOL> def new_function ( self , request , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> now = time . time ( ) <EOL> key = '<STR_LIT>' . format ( <EOL> f . __module__ , f . __name__ , <EOL> request . META [ '<STR_LIT>' ] <EOL> ) <EOL> client = VolatileFactory . get_client ( ) <EOL> mutex = VolatileMutex ( key ) <EOL> try : <EOL> mutex . acquire ( ) <EOL> rate_info = client . get ( key , { '<STR_LIT>' : [ ] , <EOL> '<STR_LIT>' : None } ) <EOL> active_timeout = rate_info [ '<STR_LIT>' ] <EOL> if active_timeout is not None : <EOL> if active_timeout > now : <EOL> logger . warning ( '<STR_LIT>' . format ( key , active_timeout - now ) ) <EOL> return HttpResponse , { '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:error>' : '<STR_LIT>' . format ( round ( active_timeout - now , <NUM_LIT:2> ) ) } , <NUM_LIT> <EOL> else : <EOL> rate_info [ '<STR_LIT>' ] = None <EOL> rate_info [ '<STR_LIT>' ] = [ call for call in rate_info [ '<STR_LIT>' ] if call > ( now - per ) ] + [ now ] <EOL> calls = len ( rate_info [ '<STR_LIT>' ] ) <EOL> if calls > amount : <EOL> rate_info [ '<STR_LIT>' ] = now + timeout <EOL> client . set ( key , rate_info ) <EOL> logger . warning ( '<STR_LIT>' . format ( key , timeout ) ) <EOL> return HttpResponse , { '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:error>' : '<STR_LIT>' . format ( calls , per ) } , <NUM_LIT> <EOL> client . set ( key , rate_info ) <EOL> finally : <EOL> mutex . release ( ) <EOL> return f ( self , request , * args , ** kwargs ) <EOL> new_function . __name__ = f . __name__ <EOL> new_function . __module__ = f . __module__ <EOL> return new_function <EOL> return wrap <EOL> def log ( ) : <EOL> """<STR_LIT>""" <EOL> def wrap ( f ) : <EOL> """<STR_LIT>""" <EOL> def new_function ( self , request , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> metadata = { '<STR_LIT>' : dict ( ( str ( key ) , str ( value ) ) for key , value in request . META . iteritems ( ) ) , <EOL> '<STR_LIT>' : dict ( ( str ( key ) , str ( value ) ) for key , value in request . REQUEST . iteritems ( ) ) , <EOL> '<STR_LIT>' : dict ( ( str ( key ) , str ( value ) ) for key , value in request . COOKIES . iteritems ( ) ) } <EOL> _logger = LogHandler . get ( '<STR_LIT>' , name = '<STR_LIT>' ) <EOL> _logger . info ( '<STR_LIT>' . format ( <EOL> f . __module__ , <EOL> f . __name__ , <EOL> getattr ( request , '<STR_LIT>' ) . user_guid if hasattr ( request , '<STR_LIT>' ) else None , <EOL> json . dumps ( list ( args ) ) , <EOL> json . dumps ( kwargs ) , <EOL> json . dumps ( metadata ) <EOL> ) ) <EOL> return f ( self , request , * args , ** kwargs ) <EOL> new_function . __name__ = f . __name__ <EOL> new_function . __module__ = f . __module__ <EOL> return new_function <EOL> return wrap <EOL> def authenticated ( ) : <EOL> """<STR_LIT>""" <EOL> def wrap ( f ) : <EOL> """<STR_LIT>""" <EOL> def new_function ( * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> request = _find_request ( args ) <EOL> user = authenticate ( request = request , native_django = True ) <EOL> if user is None : <EOL> raise PermissionDenied ( '<STR_LIT>' ) <EOL> login ( request , user ) <EOL> return f ( * args , ** kwargs ) <EOL> new_function . __name__ = f . __name__ <EOL> new_function . __module__ = f . __module__ <EOL> return new_function <EOL> return wrap </s>
