~jnaous/rpc4django/urlgroups

« back to all changes in this revision

Viewing changes to rpc4django/rpcdispatcher.py

  • Committer: Jad Naous
  • Date: 2010-11-20 07:43:51 UTC
  • Revision ID: jnaous@jadsm-20101120074351-u40j1micda55hzqd
Implement isolation across URLs

Show diffs side-by-side

added added

removed removed

Lines of Context:
8
8
import inspect
9
9
import platform
10
10
import pydoc
11
 
import types
12
11
import xmlrpclib
13
12
from xmlrpclib import Fault
14
13
from django.contrib.auth import authenticate, login, logout
43
42
    
44
43
        @rpcmethod()
45
44
        @rpcmethod(name='myns.myFuncName', signature=['int','int'])
46
 
        @rpcmethod(permission='add_group')
 
45
        @rpcmethod(permission='add_group', url_name="my_url_name")
47
46
        
48
47
    '''
49
48
    
50
49
    def set_rpcmethod_info(method):
51
50
        method.is_rpcmethod = True
52
 
        method.signature = []
53
 
        method.permission = None
54
 
        method.external_name = getattr(method, '__name__')
55
 
 
56
 
        if 'name' in kwargs:
57
 
            method.external_name = kwargs['name']
58
 
 
59
 
        if 'signature' in kwargs:
60
 
            method.signature = kwargs['signature']
61
 
            
62
 
        if 'permission' in kwargs:
63
 
            method.permission = kwargs['permission']
64
 
 
 
51
        method.external_name = kwargs.get("name", getattr(method, '__name__'))
 
52
        method.signature = kwargs.get('signature', [])
 
53
        method.permission = kwargs.get('permission', None)
 
54
        method.url_name = kwargs.get("url_name", "root")
65
55
        return method
66
56
    return set_rpcmethod_info
67
57
 
210
200
      
211
201
    '''
212
202
    
213
 
    def __init__(self, url='', apps=[], restrict_introspection=False, restrict_ootb_auth=True):
 
203
    def __init__(self, url_name='root', restrict_introspection=False, restrict_ootb_auth=True):
214
204
        version = platform.python_version_tuple()
215
 
        self.url = url
 
205
        self.url_name = url_name
216
206
        self.rpcmethods = []        # a list of RPCMethod objects
217
207
        self.jsonrpcdispatcher = JSONRPCDispatcher()
218
208
        self.xmlrpcdispatcher = XMLRPCDispatcher()
226
216
        if not restrict_ootb_auth:
227
217
            self.register_method(self.system_login)
228
218
            self.register_method(self.system_logout)
229
 
            
230
 
        self.register_rpcmethods(apps)
231
219
        
232
220
    @rpcmethod(name='system.describe', signature=['struct'])
233
221
    def system_describe(self):
237
225
        
238
226
        description = {}
239
227
        description['serviceType'] = 'RPC4Django JSONRPC+XMLRPC'
240
 
        description['serviceURL'] = self.url,
241
228
        description['methods'] = [{'name': method.name, 
242
229
                                   'summary': method.help, 
243
230
                                   'params': method.get_params(),
314
301
            
315
302
        return False
316
303
               
317
 
    def register_rpcmethods(self, apps):
318
 
        '''
319
 
        Scans the installed apps for methods with the rpcmethod decorator
320
 
        Adds these methods to the list of methods callable via RPC
321
 
        '''    
322
 
        
323
 
        for appname in apps:
324
 
            # check each app for any rpcmethods
325
 
            app = __import__(appname, globals(), locals(), ['*'])
326
 
            for obj in dir(app):
327
 
                method = getattr(app, obj)
328
 
                if callable(method) and \
329
 
                   hasattr(method, 'is_rpcmethod') and \
330
 
                   method.is_rpcmethod == True:
331
 
                    # if this method is callable and it has the rpcmethod
332
 
                    # decorator, add it to the dispatcher
333
 
                    self.register_method(method, method.external_name)
334
 
                elif isinstance(method, types.ModuleType):
335
 
                    # if this is not a method and instead a sub-module,
336
 
                    # scan the module for methods with @rpcmethod
337
 
                    try:
338
 
                        self.register_rpcmethods(["%s.%s" % (appname, obj)])
339
 
                    except ImportError:
340
 
                        pass
341
 
 
342
 
    
343
304
    def jsondispatch(self, raw_post_data, **kwargs):
344
305
        '''
345
306
        Sends the post data to :meth:`rpc4django.jsonrpcdispatcher.JSONRPCDispatcher.dispatch`