Esempio n. 1
0
    def get_search_query(self):
        permission = self.get_value('permission')
        if permission == 'add':
            names = ['path']
        else:
            names = ['path', 'format']

        # Query
        query = AndQuery()
        for name in names:
            field_name = 'search_%s' % name
            field = self.get_field(field_name)
            value = field.get_value(self, field_name)
            if not value:
                continue

            if name == 'path':
                depth = self.get_value('search_path_depth')
                depth = None if depth == '*' else int(depth)
                subquery = get_base_path_query(value, 0, depth)
            elif field.multiple:
                err = "access rules don't yet support multiple fields"
                raise NotImplementedError, err
            else:
                subquery = PhraseQuery(name, value)

            query.append(subquery)

        # Ok
        return query
Esempio n. 2
0
    def get_options(self):
        context = get_context()
        resource = context.resource
        view = context.view
        # 1. Build the query of all objects to search
        query = get_base_path_query(resource.abspath)
        if view.search_content_only(resource, context) is True:
            content_query = PhraseQuery('is_content', True)
            query = AndQuery(query, content_query)

        # 2. Compute children_formats
        children_formats = set()
        for child in context.search(query).get_documents():
            children_formats.add(child.format)

        # 3. Do not show two options with the same title
        formats = {}
        for type in children_formats:
            cls = context.database.get_resource_class(type)
            title = cls.class_title.gettext()
            formats.setdefault(title, []).append(type)

        # 4. Build the namespace
        types = []
        for title, type in formats.items():
            type = ','.join(type)
            types.append({'name': type, 'value': title})
        types.sort(key=lambda x: x['value'].lower())

        return types
Esempio n. 3
0
    def get_namespace(self, resource, context):
        # Find out broken links
        base = resource.abspath

        # Search only within the given resource
        query = get_base_path_query(base, min_depth=0)
        results = context.search(query)

        # Find out the broken links
        root = context.root
        broken = {}
        for link in context.database.catalog.get_unique_values('links'):
            if root.get_resource(link, soft=True) is not None:
                continue
            sub_results = results.search(PhraseQuery('links', link))
            link = str(base.get_pathto(Path(link)))
            for brain in sub_results.get_documents():
                broken.setdefault(brain.abspath, []).append(link)

        # Build the namespace
        items = []
        total = 0
        keys = broken.keys()
        keys.sort()
        for path in keys:
            links = broken[path]
            path = str(base.get_pathto(Path(path)))
            n = len(links)
            items.append({'path': path, 'links': links, 'n': n})
            total += n

        return {
            'items': items,
            'total': total}
Esempio n. 4
0
    def get_namespace(self, resource, context):
        # Find out broken links
        base = resource.abspath

        # Search only within the given resource
        query = get_base_path_query(base, min_depth=0)
        results = context.search(query)

        # Find out the broken links
        root = context.root
        broken = {}
        for link in context.database.catalog.get_unique_values('links'):
            if root.get_resource(link, soft=True) is not None:
                continue
            sub_results = results.search(PhraseQuery('links', link))
            link = str(base.get_pathto(Path(link)))
            for brain in sub_results.get_documents():
                broken.setdefault(brain.abspath, []).append(link)

        # Build the namespace
        items = []
        total = 0
        keys = broken.keys()
        keys.sort()
        for path in keys:
            links = broken[path]
            path = str(base.get_pathto(Path(path)))
            n = len(links)
            items.append({'path': path, 'links': links, 'n': n})
            total += n

        return {'items': items, 'total': total}
Esempio n. 5
0
    def get_search_query(self):
        permission = self.get_value('permission')
        if permission == 'add':
            names = ['path']
        else:
            names = ['path', 'format']

        # Query
        query = AndQuery()
        for name in names:
            field_name = 'search_%s' % name
            field = self.get_field(field_name)
            value = field.get_value(self, field_name)
            if not value:
                continue

            if name == 'path':
                depth = self.get_value('search_path_depth')
                depth = None if depth == '*' else int(depth)
                subquery = get_base_path_query(value, 0, depth)
            elif field.multiple:
                err = "access rules don't yet support multiple fields"
                raise NotImplementedError, err
            else:
                subquery = PhraseQuery(name, value)

            query.append(subquery)

        # Ok
        return query
Esempio n. 6
0
    def get_options(self):
        context = get_context()
        resource = context.resource
        view = context.view
        # 1. Build the query of all objects to search
        query = get_base_path_query(resource.abspath)
        if view.search_content_only(resource, context) is True:
            content_query = PhraseQuery('is_content', True)
            query = AndQuery(query, content_query)

        # 2. Compute children_formats
        children_formats = set()
        for child in context.search(query).get_documents():
            children_formats.add(child.format)

        # 3. Do not show two options with the same title
        formats = {}
        for type in children_formats:
            cls = context.database.get_resource_class(type)
            title = cls.class_title.gettext()
            formats.setdefault(title, []).append(type)

        # 4. Build the namespace
        types = []
        for title, type in formats.items():
            type = ','.join(type)
            types.append({'name': type, 'value': title})
        types.sort(key=lambda x: x['value'].lower())

        return types
Esempio n. 7
0
    def get_items_query(self, resource, context):
        # Search in subtree
        query = get_base_path_query(resource.abspath, max_depth=self.depth)

        # Base classes
        base_classes = self.base_classes
        if base_classes is not None:
            base_classes_query = OrQuery(
                *[PhraseQuery('base_classes', x) for x in base_classes])
            query = AndQuery(query, base_classes_query)
        # Ok
        return query
Esempio n. 8
0
    def del_resource(self, name, soft=False, ref_action='restrict'):
        """ref_action allows to specify which action is done before deleting
        the resource.
        ref_action can take 2 values:
        - 'restrict' (default value): do an integrity check
        - 'force': do nothing
        """
        database = self.database
        resource = self.get_resource(name, soft=soft)
        if soft and resource is None:
            return

        # Referential action
        if ref_action == 'restrict':
            # Check referencial-integrity
            path = str(resource.abspath)
            query = AndQuery(NotQuery(PhraseQuery('abspath', path)),
                             NotQuery(get_base_path_query(path)))
            sub_search = database.search(query)
            for sub_resource in resource.traverse_resources():
                path = str(sub_resource.abspath)
                query = PhraseQuery('links', path)
                results = sub_search.search(query)
                # A resource may have been updated in the same transaction,
                # so not yet reindexed: we need to check that the resource
                # really links.
                for referrer in results.get_resources():
                    if path in referrer.get_links():
                        err = 'cannot delete, resource "{}" is referenced'
                        raise ConsistencyError(err.format(path))
        elif ref_action == 'force':
            # Do not check referencial-integrity
            pass
        else:
            raise ValueError,('Incorrect ref_action "{}"'.format(ref_action))

        # Events, remove
        path = str(resource.abspath)
        database.remove_resource(resource)
        # Remove
        fs = database.fs
        for handler in resource.get_handlers():
            # Skip empty folders and phantoms
            if fs.exists(handler.key):
                database.del_handler(handler.key)
        self.handler.del_handler('%s.metadata' % name)
        # Clear cookie
        context = get_context()
        cut, paths = context.get_cookie('ikaaro_cp', datatype=CopyCookie)
        if path in paths:
            context.del_cookie('ikaaro_cp')
Esempio n. 9
0
    def del_resource(self, name, soft=False, ref_action='restrict'):
        """ref_action allows to specify which action is done before deleting
        the resource.
        ref_action can take 2 values:
        - 'restrict' (default value): do an integrity check
        - 'force': do nothing
        """
        database = self.database
        resource = self.get_resource(name, soft=soft)
        if soft and resource is None:
            return

        # Referential action
        if ref_action == 'restrict':
            # Check referencial-integrity
            path = str(resource.abspath)
            query = AndQuery(NotQuery(PhraseQuery('abspath', path)),
                             NotQuery(get_base_path_query(path)))
            sub_search = database.search(query)
            for sub_resource in resource.traverse_resources():
                path = str(sub_resource.abspath)
                query = PhraseQuery('links', path)
                results = sub_search.search(query)
                # A resource may have been updated in the same transaction,
                # so not yet reindexed: we need to check that the resource
                # really links.
                for referrer in results.get_resources():
                    if path in referrer.get_links():
                        err = 'cannot delete, resource "{}" is referenced'
                        raise ConsistencyError(err.format(path))
        elif ref_action == 'force':
            # Do not check referencial-integrity
            pass
        else:
            raise ValueError, ('Incorrect ref_action "{}"'.format(ref_action))

        # Events, remove
        path = str(resource.abspath)
        database.remove_resource(resource)
        # Remove
        fs = database.fs
        for handler in resource.get_handlers():
            # Skip empty folders and phantoms
            if fs.exists(handler.key):
                database.del_handler(handler.key)
        self.handler.del_handler('%s.metadata' % name)
        # Clear cookie
        context = get_context()
        cut, paths = context.get_cookie('ikaaro_cp', datatype=CopyCookie)
        if path in paths:
            context.del_cookie('ikaaro_cp')
Esempio n. 10
0
    def get_items_query(self, resource, context):
        # Search in subtree
        query = get_base_path_query(resource.abspath, max_depth=self.depth)

        # Base classes
        base_classes = self.base_classes
        if base_classes is not None:
            base_classes_query = OrQuery(
                *[PhraseQuery('base_classes', x) for x in base_classes])
            query = AndQuery(query, base_classes_query)

        # Exclude non-content
        if self.search_content_only(resource, context) is True:
            query = AndQuery(query, PhraseQuery('is_content', True))

        return query
Esempio n. 11
0
    def get_items_query(self, resource, context):
        # Search in subtree
        query = get_base_path_query(resource.abspath, max_depth=self.depth)

        # Base classes
        base_classes = self.base_classes
        if base_classes is not None:
            base_classes_query = OrQuery(*
                [ PhraseQuery('base_classes', x) for x in base_classes ])
            query = AndQuery(query, base_classes_query)

        # Exclude non-content
        if self.search_content_only(resource, context) is True:
            query = AndQuery(query, PhraseQuery('is_content', True))

        return query
Esempio n. 12
0
    def del_resource(self, name, soft=False, ref_action='restrict'):
        """ref_action allows to specify which action is done before deleting
        the resource.
        ref_action can take 2 values:
        - 'restrict' (default value): do an integrity check
        - 'force': do nothing
        """
        database = self.database
        resource = self.get_resource(name, soft=soft)
        if soft and resource is None:
            return

        # Referential action
        if ref_action == 'restrict':
            # Check referencial-integrity
            path = str(resource.abspath)
            query = AndQuery(NotQuery(PhraseQuery('abspath', path)),
                             NotQuery(get_base_path_query(path)))
            sub_search = database.search(query)
            for sub_resource in resource.traverse_resources():
                path = str(sub_resource.abspath)
                query = PhraseQuery('links', path)
                results = sub_search.search(query)
                # A resource may have been updated in the same transaction,
                # so not yet reindexed: we need to check that the resource
                # really links.
                for referrer in results.get_resources():
                    if path in referrer.get_links():
                        err = 'cannot delete, resource "{}" is referenced'
                        raise ConsistencyError(err.format(path))
        elif ref_action == 'force':
            # Do not check referencial-integrity
            pass
        else:
            raise ValueError,('Incorrect ref_action "{}"'.format(ref_action))

        # Events, remove
        path = str(resource.abspath)
        database.remove_resource(resource)
        # Remove handlers
        for r in list(resource.traverse_resources()):
            for handler in [r.metadata] + r.get_fields_handlers():
                if database.has_handler(handler.key):
                    database.del_handler(handler.key)
Esempio n. 13
0
    def GET(self, resource, context):
        # Build the query
        query = get_base_path_query(resource.abspath)
        for key, value in context.uri.query.items():
            if key == 'abspath' and value == 'myself':
                value = str(context.user.abspath)
            query = AndQuery(query, PhraseQuery(key, value))

        # Search
        items = []
        for resource in context.search(query).get_resources():
            item = {'abspath': {'value': str(resource.abspath)}}
            for field_name in resource.fields:
                value = field_to_json(resource, field_name)
                if value is not None:
                    item[field_name] = value

            items.append(item)

        # Ok
        return self.return_json(items)
Esempio n. 14
0
    def GET(self, resource, context):
        # Build the query
        query = get_base_path_query(resource.abspath)
        for key, value in context.uri.query.items():
            if key == 'abspath' and value == 'myself':
                value = str(context.user.abspath)
            query = AndQuery(query, PhraseQuery(key, value))

        # Search
        items = []
        for resource in context.search(query).get_resources():
            item = {'abspath': {'value': str(resource.abspath)}}
            for field_name in resource.fields:
                value = field_to_json(resource, field_name)
                if value is not None:
                    item[field_name] = value

            items.append(item)

        # Ok
        return self.return_json(items)