예제 #1
0
 def auth_resource(self,
                   resource,
                   auth=None,
                   notify=False,
                   invalidate=False,
                   flush=True,
                   action='append',
                   **kw):
     if isinstance(resource, basestring):
         uri = resource
     elif isinstance(resource, etree._Element):
         uri = resource.get('uri')
     resource = load_uri(uri)
     log.debug("auth_resourch on %s", str(resource))
     response = etree.Element('resource')
     if resource is not None:
         auth = resource_acl(resource,
                             newauth=auth,
                             notify=notify,
                             invalidate=invalidate,
                             action=action)
         response.append(auth)
     else:
         log.warn('AUTH: could not load resource %s', uri)
     if flush:
         self.flushchanges()
     return response
예제 #2
0
    def update_resource(self,
                        resource,
                        new_resource=None,
                        replace=True,
                        flush=True,
                        **kw):
        """update a resource with a new values
        @param resource: an etree element or uri
        @param new_resource:  an etree element if resource is a uri
        @param replace: replace all members (if false then appends only)
        @param kw: view parameters for return
        """

        uri = None
        log.debug('resource  = %s' % resource)
        if isinstance(resource, etree._Element):
            uri = resource.get('uri')
        elif isinstance(resource, basestring):
            uri = resource
            if new_resource is None:
                raise BadValue(
                    'update_resource uri %s needs new_value to update  ' % uri)
        if uri is not None:
            resource = load_uri(uri)
        log.debug('resource %s = %s' % (uri, resource))
        node = bisquik2db(doc=new_resource, resource=resource, replace=replace)
        #response  = etree.Element ('response')
        #self.cache_invalidate(r.get('uri'))
        if flush:
            self.flushchanges(node)
        self.cache_invalidate_resource(resource)
        r = db2tree(node, baseuri=self.url, **kw)
        return r
예제 #3
0
    def new_resource(self, resource, parent=None, flush=True, **kw):
        '''Create a new resouce in the local database based on the
        resource tree given.
        '''
        view = kw.pop('view', None)
        if isinstance(resource, basestring):
            log.debug('attributes= %s ', kw)
            resource = etree.Element(resource, **kw)
            log.debug('created %s ', resource)

        if parent is None and resource.get('resource_uniq') is None:
            resource.set('resource_uniq', self.resource_uniq())
        else:
            if isinstance(parent, etree._Element):
                parent = parent.get('uri')
            if isinstance(parent, basestring):
                parent = load_uri(parent)

        node = bisquik2db(doc=resource, parent=parent)
        log.debug("new_resource %s", node)
        # Invalidate the top level container i.e. /data_service/<resource_type>
        #self.cache_invalidate(r.get('uri').rsplit('/', 1)[0])
        if flush:
            self.flushchanges(node, parent)
        self.cache_invalidate_resource(node)
        r = db2tree(node, baseuri=self.url, view=view)
        return r
예제 #4
0
 def append_resource(self, resource, tree=None, flush=True, **kw):
     '''Append an element to resource (a node)
     '''
     if isinstance(resource, etree._Element):
         uri = resource.get('uri')
         resource = load_uri(uri)
     dbresource = bisquik2db(doc=tree, parent=resource)
     #self.cache_invalidate(r.get('uri'))
     if flush:
         self.flushchanges(dbresource, resource)
     self.cache_invalidate_resource(dbresource)
     r = db2tree(dbresource, baseuri=self.url, **kw)
     return r
예제 #5
0
 def del_resource(self, resource, **kw):
     uri = None
     if isinstance(resource, etree._Element):
         uri = resource.get('uri')
     elif isinstance(resource, basestring):
         uri = resource
     if uri is not None:
         resource = load_uri(uri)
     if resource is not None:
         resource_delete(resource, **kw)
         #self.cache_invalidate(uri)
     else:
         log.warn("Could not load uri %s ", uri)
     self.flushchanges()
     self.cache_invalidate_resource(resource)
예제 #6
0
    def get_resource(self, resource, **kw):
        uri = None
        if isinstance(resource, etree._Element):
            uri = resource.get('uri')
        elif isinstance(resource, basestring):
            uri, params = strip_url_params(resource)
            params.update(kw)
            kw = params
        #log.debug ("get_resource %s %s",  uri, str(kw))
        if uri is not None:
            log.debug("get_resource %s %s", uri, str(kw))
            response = self.cache_check(uri, **kw)
            if response:
                log.debug("get_resource:CACHE response")
                try:
                    xml = etree.XML(response)
                    return xml
                except etree.XMLSyntaxError:
                    log.exception(' while reading cached resourced %s got %s',
                                  uri, response)

            service, clname, ida, rest = parse_bisque_uri(uri)
            resource = load_uri(uri, query=True)
            if rest:  # Fetch resource is really a query
                log.debug("get_resource:QUERY %s %s %s ->", service, ida, rest)
                if resource is not None:
                    resource = resource.first()
                resource = self.query(resource_tag=rest[-1],
                                      parent=resource,
                                      **kw)
                #self.cache_save (uri, response=etree.tostring(resource), **kw)
                #resource.set('uri', uri)
                return resource
            resource = prepare_permissions(resource,
                                           user_id=None,
                                           with_public=True)
            log.debug("got resource %s", str(resource))
            resource = resource.first()

        log.debug("get_resource: uri %s -> %s", uri, str(resource))
        if resource is None:
            return resource
        xtree = db2tree(resource, baseuri=self.url, **kw)
        uri = uri or xtree.get('uri')
        self.cache_save(uri, response=etree.tostring(xtree), **kw)
        return xtree
예제 #7
0
    def query(self, resource_tag=None, parent=None, cache=True, **kw):
        '''Query the local database with expr'''
        #resource_type = dbtype_from_tag(resource_tag)
        parent_uri = getattr(parent, 'uri', None)
        if isinstance(parent, etree._Element):
            parent = parent.get('uri')
        if isinstance(parent, basestring):
            parent_uri = parent
            parent = load_uri(parent)

        log.debug('query: %s %s %s', resource_tag, parent_uri, kw)

        uri = "%s/%s" % (parent_uri or self.url, resource_tag or '')

        if cache:
            response = self.cache_check(uri, **kw)
            if response:
                xml = etree.XML(response)
                return xml

        params = kw.copy()
        view = params.pop('view', None)
        if view == 'count':
            count = resource_count(resource_tag, parent=parent, **params)
            response = etree.Element('resource')
            etree.SubElement(response,
                             resource_tag or 'resource',
                             count=str(count))
        else:
            nodelist = resource_query(resource_tag, parent=parent, **params)
            if view == 'query':
                return nodelist
            full_url = "%s?%s" % (uri, "&".join("%s=%s" % (k, v)
                                                for k, v in kw.items()))
            response = etree.Element('resource', uri=full_url)
            db2tree(nodelist,
                    parent=response,
                    view=view,
                    baseuri=self.url,
                    **params)

        if cache:
            self.cache_save(uri, response=etree.tostring(response), **kw)
        #log.debug ("DS: " + etree.tostring(response))
        return response
예제 #8
0
    def _find_store(self, resource):
        """return a store(mount)  by the resource

        case 1: Anonymous user:  resource must be published
        case 2: User == resource owner : should be one of the users stores
        case 3: User != resource owner : user has read permission

        We has N store prefix and we want to find the longest match for the target string (searchurl)
        """
        #  This should have been checked before we ever get here:
        #  Currently all service check accessibility before arriving here.
        #  so we can simply find the store using the resource's owner.

        # identity functions need to either accept URI we should move to using UNIQ codes for all embedded URI
        # i.e. <image uniq="00-AAA" owner="00-CCC" ../>
        owner_uri = resource.get('owner')
        owner = load_uri(owner_uri)

        # Is this enough context? Should the whole operation be carried out as the user or just the store lookup?
        with identity.as_user(owner):
            store, driver = self.valid_store_ref(resource)
        return store, driver