示例#1
0
文件: api.py 项目: gnomix/drest
 def add_resource(self, name, resource_handler=None, path=None):
     if not path:
         path = '%s' % name
     else:
         path = path.lstrip('/')
         
     if not resource_handler:
         handler = self._meta.resource
     else:
         handler = resource_handler
     
     
     handler = handler(baseurl=self._meta.baseurl, path=path, 
                       name=name, 
                       request=self._request
                       )
     resource.validate(handler)
     if hasattr(self, name):
         raise exc.dRestResourceError(
             "The object '%s' already exist on '%s'" % (name, self))
     setattr(self, name, handler)
     self._resources.append(name)
示例#2
0
文件: api.py 项目: yrik/drest
    def add_resource(self, name, resource_handler=None, path=None):
        """
        Add a resource handler to the api object.
        
        Required Arguments:
        
            name
                The name of the resource.  This is generally the basic name 
                of the resource on the API.  For example '/api/v0/users/' 
                would likely be called 'users' and will be accessible as
                'api.users' from which additional calls can be made.  For 
                example 'api.users.get()'.  
        
        Optional Arguments:
        
            resource_handler
                The resource handler class to use.  Defaults to 
                self._meta.resource_handler.
            
            path
                The path to the resource on the API (after the base url).
                Defaults to '/<name>/'.
        
        
        Nested Resources:
            
        It is possible to attach resources in a 'nested' fashion.  For example
        passing a name of 'my.nested.users' would be accessible as 
        api.my.nested.users.get().
        
        Usage:
        
        .. code-block:: python
        
            api.add_resource('users')
            response = api.users.get()
            
            # Or for nested resources
            api.add_resource('my.nested.users', path='/users/')
            response = api.my.nested.users.get()
            
        """
        safe_list = ['.', '_']
        for char in name:
            if char in safe_list:
                continue
            if not char.isalnum():
                raise exc.dRestResourceError(
                    "resource name must be alpha-numeric.")

        if not path:
            path = '%s' % name
        else:
            path = path.strip('/')

        if not resource_handler:
            resource_handler = self._meta.resource_handler

        resource.validate(resource_handler)
        handler = resource_handler(self, name, path)
        if hasattr(self, name):
            raise exc.dRestResourceError(
                "The object '%s' already exist on '%s'" % (name, self))

        # break up if nested
        parts = name.split('.')
        if len(parts) == 1:
            setattr(self, name, handler)
        elif len(parts) > 1:
            first = parts.pop(0)
            last = parts.pop()

            # add the first object to self
            setattr(self, first, resource.NestedResource())
            first_obj = getattr(self, first)
            current_obj = first_obj

            # everything in between
            for part in parts:
                setattr(current_obj, part, resource.NestedResource())
                current_obj = getattr(current_obj, part)

            # add the actual resource to the chain of nested objects
            setattr(current_obj, last, handler)

        self._resources.append(name)
示例#3
0
文件: api.py 项目: eirmag/drest
 def add_resource(self, name, resource_handler=None, path=None):
     """
     Add a resource handler to the api object.
     
     Required Arguments:
     
         name
             The name of the resource.  This is generally the basic name 
             of the resource on the API.  For example '/api/v0/users/' 
             would likely be called 'users' and will be accessible as
             'api.users' from which additional calls can be made.  For 
             example 'api.users.get()'.  
     
     Optional Arguments:
     
         resource_handler
             The resource handler class to use.  Defaults to 
             self._meta.resource_handler.
         
         path
             The path to the resource on the API (after the base url).
             Defaults to '/<name>/'.
     
     
     Nested Resources:
         
     It is possible to attach resources in a 'nested' fashion.  For example
     passing a name of 'my.nested.users' would be accessible as 
     api.my.nested.users.get().
     
     Usage:
     
     .. code-block:: python
     
         api.add_resource('users')
         response = api.users.get()
         
         # Or for nested resources
         api.add_resource('my.nested.users', path='/users/')
         response = api.my.nested.users.get()
         
     """
     safe_list = ['.', '_']
     for char in name:
         if char in safe_list:
             continue
         if not char.isalnum():
             raise exc.dRestResourceError(
                 "resource name must be alpha-numeric."
                 )
     
     if not path:
         path = '%s' % name
     else:
         path = path.strip('/')
         
     if not resource_handler:
         resource_handler = self._meta.resource_handler
         
     resource.validate(resource_handler)
     handler = resource_handler(self, name, path)
     if hasattr(self, name):
         raise exc.dRestResourceError(
             "The object '%s' already exist on '%s'" % (name, self))
                 
     
     # break up if nested
     parts = name.split('.')
     if len(parts) == 1:
         setattr(self, name, handler)
     elif len(parts) > 1:
         first = parts.pop(0)
         last = parts.pop()
         
         # add the first object to self
         setattr(self, first, resource.NestedResource())
         first_obj = getattr(self, first)
         current_obj = first_obj
         
         # everything in between
         for part in parts:
             setattr(current_obj, part, resource.NestedResource())
             current_obj = getattr(current_obj, part)
         
         # add the actual resource to the chain of nested objects
         setattr(current_obj, last, handler)        
         
     self._resources.append(name)