<s> '''<STR_LIT>''' <EOL> import logging <EOL> from xml . dom . minidom import parseString <EOL> from xml . parsers . expat import ExpatError <EOL> from django . http import HttpResponse , Http404 , HttpResponseForbidden <EOL> from django . shortcuts import render_to_response <EOL> from django . conf import settings <EOL> import types <EOL> from rpcdispatcher import RPCDispatcher <EOL> from __init__ import version <EOL> LOG_REQUESTS_RESPONSES = getattr ( settings , <EOL> '<STR_LIT>' , False ) <EOL> RESTRICT_INTROSPECTION = getattr ( settings , <EOL> '<STR_LIT>' , False ) <EOL> RESTRICT_JSON = getattr ( settings , '<STR_LIT>' , False ) <EOL> RESTRICT_XML = getattr ( settings , '<STR_LIT>' , False ) <EOL> RESTRICT_METHOD_SUMMARY = getattr ( settings , <EOL> '<STR_LIT>' , False ) <EOL> RESTRICT_RPCTEST = getattr ( settings , '<STR_LIT>' , False ) <EOL> RESTRICT_RPCTEST = getattr ( settings , '<STR_LIT>' , False ) <EOL> HTTP_ACCESS_CREDENTIALS = getattr ( settings , <EOL> '<STR_LIT>' , False ) <EOL> HTTP_ACCESS_ALLOW_ORIGIN = getattr ( settings , <EOL> '<STR_LIT>' , '<STR_LIT>' ) <EOL> APPS = getattr ( settings , '<STR_LIT>' , [ ] ) <EOL> logger = logging . getLogger ( "<STR_LIT>" ) <EOL> class NonExistingDispatcher ( Exception ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , path , url_name ) : <EOL> super ( NonExistingDispatcher , self ) . __init__ ( <EOL> "<STR_LIT>" % ( url_name , path ) ) <EOL> self . path = path <EOL> self . url_name = url_name <EOL> def get_dispatcher ( path , url_name ) : <EOL> try : <EOL> dispatcher = dispatchers [ url_name ] <EOL> except KeyError : <EOL> raise NonExistingDispatcher ( path , url_name ) <EOL> return dispatcher <EOL> def _check_request_permission ( request , request_format = '<STR_LIT>' , url_name = "<STR_LIT:root>" ) : <EOL> '''<STR_LIT>''' <EOL> user = getattr ( request , '<STR_LIT:user>' , None ) <EOL> dispatcher = get_dispatcher ( request . path , url_name ) <EOL> methods = dispatcher . list_methods ( ) <EOL> method_name = dispatcher . get_method_name ( request . raw_post_data , request_format ) <EOL> response = True <EOL> for method in methods : <EOL> if method . name == method_name : <EOL> if method . permission is not None : <EOL> logger . debug ( '<STR_LIT>' % ( method . name , method . permission ) ) <EOL> if user is None : <EOL> logger . warn ( '<STR_LIT>' ) <EOL> response = False <EOL> elif not user . has_perm ( method . permission ) : <EOL> logger . info ( '<STR_LIT>' % ( str ( user ) ) ) <EOL> response = False <EOL> else : <EOL> logger . debug ( '<STR_LIT>' % ( str ( user ) ) ) <EOL> else : <EOL> logger . debug ( '<STR_LIT>' % ( method . name ) ) <EOL> break <EOL> return response <EOL> def _is_xmlrpc_request ( request ) : <EOL> '''<STR_LIT>''' <EOL> conttype = request . META . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> if conttype == '<STR_LIT>' or conttype == '<STR_LIT>' : <EOL> return True <EOL> elif conttype . find ( '<STR_LIT>' ) >= <NUM_LIT:0> or conttype . find ( '<STR_LIT>' ) >= <NUM_LIT:0> : <EOL> return False <EOL> if LOG_REQUESTS_RESPONSES : <EOL> logger . info ( '<STR_LIT>' % conttype ) <EOL> logger . info ( '<STR_LIT>' ) <EOL> try : <EOL> parseString ( request . raw_post_data ) <EOL> return True <EOL> except ExpatError : <EOL> pass <EOL> return False <EOL> def serve_rpc_request ( request , url_name = "<STR_LIT:root>" , ** kwargs ) : <EOL> '''<STR_LIT>''' <EOL> dispatcher = get_dispatcher ( request . path , url_name ) <EOL> if request . method == "<STR_LIT:POST>" and len ( request . POST ) > <NUM_LIT:0> : <EOL> if LOG_REQUESTS_RESPONSES : <EOL> logger . debug ( '<STR_LIT>' ) <EOL> logger . debug ( '<STR_LIT>' % str ( request . raw_post_data ) ) <EOL> if _is_xmlrpc_request ( request ) : <EOL> if RESTRICT_XML : <EOL> raise Http404 <EOL> if not _check_request_permission ( request , '<STR_LIT>' , url_name = url_name ) : <EOL> return HttpResponseForbidden ( ) <EOL> resp = dispatcher . xmldispatch ( request . raw_post_data , <EOL> request = request , ** kwargs ) <EOL> response_type = '<STR_LIT>' <EOL> else : <EOL> if RESTRICT_JSON : <EOL> raise Http404 <EOL> if not _check_request_permission ( request , '<STR_LIT>' , url_name = url_name ) : <EOL> return HttpResponseForbidden ( ) <EOL> resp = dispatcher . jsondispatch ( request . raw_post_data , <EOL> request = request ) <EOL> response_type = '<STR_LIT:application/json>' <EOL> if LOG_REQUESTS_RESPONSES : <EOL> logger . debug ( '<STR_LIT>' % response_type ) <EOL> logger . debug ( '<STR_LIT>' % resp ) <EOL> return HttpResponse ( resp , response_type ) <EOL> elif request . method == '<STR_LIT>' : <EOL> response = HttpResponse ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> origin = request . META . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> response [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> response [ '<STR_LIT>' ] = <NUM_LIT:0> <EOL> response [ '<STR_LIT>' ] = str ( HTTP_ACCESS_CREDENTIALS ) . lower ( ) <EOL> response [ '<STR_LIT>' ] = HTTP_ACCESS_ALLOW_ORIGIN <EOL> response [ '<STR_LIT>' ] = request . META . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> if LOG_REQUESTS_RESPONSES : <EOL> logger . debug ( '<STR_LIT>' % ( origin ) ) <EOL> return response <EOL> else : <EOL> if RESTRICT_METHOD_SUMMARY : <EOL> raise Http404 <EOL> methods = dispatcher . list_methods ( ) <EOL> template_data = { <EOL> '<STR_LIT>' : methods , <EOL> '<STR_LIT:url>' : request . path , <EOL> '<STR_LIT:version>' : version ( ) , <EOL> '<STR_LIT>' : RESTRICT_RPCTEST , <EOL> } <EOL> return render_to_response ( '<STR_LIT>' , template_data ) <EOL> try : <EOL> from django . views . decorators . csrf import csrf_exempt <EOL> except ImportError : <EOL> try : <EOL> from django . contrib . csrf . middleware import csrf_exempt <EOL> except ImportError : <EOL> csrf_exempt = None <EOL> if csrf_exempt is not None : <EOL> serve_rpc_request = csrf_exempt ( serve_rpc_request ) <EOL> def _register_rpcmethods ( apps , restrict_introspection = False , dispatchers = { } ) : <EOL> '''<STR_LIT>''' <EOL> for appname in apps : <EOL> app = __import__ ( appname , globals ( ) , locals ( ) , [ '<STR_LIT:*>' ] ) <EOL> for obj in dir ( app ) : <EOL> method = getattr ( app , obj ) <EOL> if callable ( method ) and getattr ( method , '<STR_LIT>' , False ) : <EOL> if method . url_name not in dispatchers : <EOL> dispatchers [ method . url_name ] = RPCDispatcher ( <EOL> method . url_name , restrict_introspection ) <EOL> dispatchers [ method . url_name ] . register_method ( <EOL> method , method . external_name ) <EOL> elif isinstance ( method , types . ModuleType ) : <EOL> try : <EOL> _register_rpcmethods ( <EOL> [ "<STR_LIT>" % ( appname , obj ) ] , <EOL> restrict_introspection , dispatchers ) <EOL> except ImportError : <EOL> pass <EOL> return dispatchers <EOL> dispatchers = _register_rpcmethods ( APPS , RESTRICT_INTROSPECTION ) </s>
