Exemplo n.º 1
0
def get_abspath_links(self, links, resource, field_name, languages):
    if not self.multilingual:
        languages = [None]

    for lang in languages:
        value = resource.get_value(field_name, lang)
        if not value:
            continue
        if self.multiple:
            # Multiple
            for x in value:
                if not x:
                    continue
                if x.startswith('/'):
                    # Get the reference, path and view
                    ref, path, view = split_reference(x)
                    if ref.scheme:
                        continue
                    links.add(str(path))
        else:
            if value.startswith('/'):
                # Get the reference, path and view
                ref, path, view = split_reference(value)
                if ref.scheme:
                    continue
                # Singleton
                links.add(str(path))
    return links
Exemplo n.º 2
0
    def get_links(self, links, resource, field_name, languages):
        base = resource.abspath
        if not self.multilingual:
            languages = [None]

        for lang in languages:
            prop = resource.metadata.get_property(field_name, lang)
            if prop is None:
                continue
            if self.multiple:
                # Multiple
                for x in prop:
                    value = x.value
                    if not value:
                        continue
                    # Get the reference, path and view
                    ref, path, view = split_reference(value)
                    if ref.scheme:
                        continue
                    link = base.resolve2(path)
                    links.add(str(link))
            else:
                value = prop.value
                if not value:
                    continue
                # Get the reference, path and view
                ref, path, view = split_reference(value)
                if ref.scheme:
                    continue
                # Singleton
                link = base.resolve2(path)
                links.add(str(link))

        return links
Exemplo n.º 3
0
    def get_links(self, links, resource, field_name, languages):
        base = resource.abspath
        if not self.multilingual:
            languages = [None]

        for lang in languages:
            value = resource.get_value(field_name, lang)
            if not value:
                continue
            if self.multiple:
                # Multiple
                for x in value:
                    if not x:
                        continue
                    # Get the reference, path and view
                    ref, path, view = split_reference(x)
                    if ref.scheme:
                        continue
                    link = base.resolve2(path)
                    links.add(str(link))
            else:
                # Get the reference, path and view
                ref, path, view = split_reference(value)
                if ref.scheme:
                    continue
                # Singleton
                link = base.resolve2(path)
                links.add(str(link))

        return links
Exemplo n.º 4
0
def get_abspath_links(self, links, resource, field_name, languages):
    if not self.multilingual:
        languages = [None]

    for lang in languages:
        prop = resource.metadata.get_property(field_name, lang)
        if prop is None:
            continue
        if self.multiple:
            # Multiple
            for x in prop:
                value = x.value
                if not value:
                    continue
                if value.startswith('/'):
                    # Get the reference, path and view
                    ref, path, view = split_reference(value)
                    if ref.scheme:
                        continue
                    links.add(str(path))
        else:
            value = prop.value
            if not value:
                continue
            if value.startswith('/'):
                # Get the reference, path and view
                ref, path, view = split_reference(value)
                if ref.scheme:
                    continue
                # Singleton
                links.add(str(path))
    return links
Exemplo n.º 5
0
def update_abspath_links(self, resource, field_name, source, target, languages,
                 old_base, new_base):
    if not self.multilingual:
        languages = [None]

    for lang in languages:
        prop = resource.metadata.get_property(field_name, lang)
        if prop is None:
            continue
        if self.multiple:
            # Multiple
            new_values = []
            for p in prop:
                value = p.value
                if not value:
                    continue
                if not value.startswith('/'):
                    # In Share_Field, "everybody" / "authenticated" are not abspaths
                    new_values.append(value)
                else:
                    # Get the reference, path and view
                    ref, path, view = split_reference(value)
                    if ref.scheme:
                        continue
                    path = old_base.resolve2(path)
                    if path == source:
                        # Explicitly call str because URI.encode does
                        # nothing
                        new_value = str(target) + view
                        new_values.append(new_value)
                    else:
                        new_values.append(p)
            self._set_value(resource, field_name, new_values, lang)
        else:
            # Singleton
            value = prop.value
            if not value:
                continue
            if not value.startswith('/'):
                # In Share_Field, "everybody" / "authenticated" are not abspaths
                continue
            # Get the reference, path and view
            ref, path, view = split_reference(value)
            if ref.scheme:
                continue
            path = old_base.resolve2(path)
            if path == source:
                # Hit the old name
                # Build the new reference with the right path
                # Explicitly call str because URI.encode does nothing
                new_value = str(target) + view
                self._set_value(resource, field_name, new_value, lang)
Exemplo n.º 6
0
    def _is_allowed_to_access(self, context, uri):
        # Get the reference and path
        ref, path, view = split_reference(uri)

        # Broken entry
        if ref is None or path == '':
            return False

        # External link
        if ref.scheme:
            return True

        # Broken link
        resource = self.get_resource(path, soft=True)
        if resource is None:
            return False

        if view:
            # Remove the first '/;' of the view
            view = resource.get_view(view[2:], ref.query)
            if not view:
                return False
            # Check ACL
            return context.is_access_allowed(resource, view)

        # Check if the user can access to resource views
        # get_views checks ACLs with by calling is_access_allowed
        resource_views = list(resource.get_views())
        return len(resource_views) > 0
Exemplo n.º 7
0
    def update_incoming_links(self, resource, field_name, source, languages):
        target = resource.abspath
        resources_old2new = resource.database.resources_old2new
        if not self.multilingual:
            languages = [None]

        for lang in languages:
            prop = resource.metadata.get_property(field_name, lang)
            if prop is None:
                continue
            if self.multiple:
                # Multiple
                new_values = []
                for p in prop:
                    value = p.value
                    if not value:
                        continue
                    # Get the reference, path and view
                    ref, path, view = split_reference(value)
                    if ref.scheme:
                        continue
                    # Calculate the old absolute path
                    old_abs_path = source.resolve2(path)
                    # Check if the target path has not been moved
                    new_abs_path = resources_old2new.get(
                        old_abs_path, old_abs_path)
                    new_value = str(target.get_pathto(new_abs_path)) + view
                    new_values.append(new_value)
                self._set_value(resource, field_name, new_values, lang)
            else:
                # Singleton
                value = prop.value
                if not value:
                    continue
                # Get the reference, path and view
                ref, path, view = split_reference(value)
                if ref.scheme:
                    continue
                # Calculate the old absolute path
                old_abs_path = source.resolve2(path)
                # Check if the target path has not been moved
                new_abs_path = resources_old2new.get(old_abs_path,
                                                     old_abs_path)

                # Explicitly call str because URI.encode does nothing
                new_value = str(target.get_pathto(new_abs_path)) + view
                self._set_value(resource, field_name, new_value, lang)
Exemplo n.º 8
0
    def update_incoming_links(self, resource, field_name, source, languages):
        target = resource.abspath
        resources_old2new = resource.database.resources_old2new
        if not self.multilingual:
            languages = [None]

        for lang in languages:
            prop = resource.metadata.get_property(field_name, lang)
            if prop is None:
                continue
            if self.multiple:
                # Multiple
                new_values = []
                for p in prop:
                    value = p.value
                    if not value:
                        continue
                    # Get the reference, path and view
                    ref, path, view = split_reference(value)
                    if ref.scheme:
                        continue
                    # Calculate the old absolute path
                    old_abs_path = source.resolve2(path)
                    # Check if the target path has not been moved
                    new_abs_path = resources_old2new.get(old_abs_path,
                                                         old_abs_path)
                    new_value = str(target.get_pathto(new_abs_path)) + view
                    new_values.append(new_value)
                self._set_value(resource, field_name, new_values, lang)
            else:
                # Singleton
                value = prop.value
                if not value:
                    continue
                # Get the reference, path and view
                ref, path, view = split_reference(value)
                if ref.scheme:
                    continue
                # Calculate the old absolute path
                old_abs_path = source.resolve2(path)
                # Check if the target path has not been moved
                new_abs_path = resources_old2new.get(old_abs_path,
                                                     old_abs_path)

                # Explicitly call str because URI.encode does nothing
                new_value = str(target.get_pathto(new_abs_path)) + view
                self._set_value(resource, field_name, new_value, lang)
Exemplo n.º 9
0
    def update_links(self, resource, field_name, source, target, languages,
                     old_base, new_base):
        if not self.multilingual:
            languages = [None]

        for lang in languages:
            value = resource.get_value(field_name, lang)
            if not value:
                continue
            if self.multiple:
                # Multiple
                new_values = []
                for x in value:
                    if not x:
                        continue
                    # Get the reference, path and view
                    ref, path, view = split_reference(x)
                    if ref.scheme:
                        continue
                    path = old_base.resolve2(path)
                    if path == source:
                        # Explicitly call str because URI.encode does
                        # nothing
                        new_value = str(new_base.get_pathto(target)) + view
                        new_values.append(new_value)
                    else:
                        new_values.append(x)
                self._set_value(resource, field_name, new_values, lang)
            else:
                # Singleton
                # Get the reference, path and view
                ref, path, view = split_reference(value)
                if ref.scheme:
                    continue
                path = old_base.resolve2(path)
                if path == source:
                    # Hit the old name
                    # Build the new reference with the right path
                    # Explicitly call str because URI.encode does nothing
                    new_value = str(new_base.get_pathto(target)) + view
                    self._set_value(resource, field_name, new_value, lang)
Exemplo n.º 10
0
    def update_links(self, resource, field_name, source, target, languages,
                     old_base, new_base):
        if not self.multilingual:
            languages = [None]

        for lang in languages:
            value = resource.get_value(field_name, lang)
            if not value:
                continue
            if self.multiple:
                # Multiple
                new_values = []
                for x in value:
                    if not x:
                        continue
                    # Get the reference, path and view
                    ref, path, view = split_reference(x)
                    if ref.scheme:
                        continue
                    path = old_base.resolve2(path)
                    if path == source:
                        # Explicitly call str because URI.encode does
                        # nothing
                        new_value = str(new_base.get_pathto(target)) + view
                        new_values.append(new_value)
                    else:
                        new_values.append(x)
                self._set_value(resource, field_name, new_values, lang)
            else:
                # Singleton
                # Get the reference, path and view
                ref, path, view = split_reference(value)
                if ref.scheme:
                    continue
                path = old_base.resolve2(path)
                if path == source:
                    # Hit the old name
                    # Build the new reference with the right path
                    # Explicitly call str because URI.encode does nothing
                    new_value = str(new_base.get_pathto(target)) + view
                    self._set_value(resource, field_name, new_value, lang)
Exemplo n.º 11
0
    def get_menu_namespace_level(self, context, url, use_first_child=False):
        menu_abspath = self.abspath
        here = context.resource
        here_abspath = here.abspath
        here_view_name = url[-1]
        here_abspath_and_view = '%s/%s' % (here_abspath, here_view_name)
        items = []

        for resource in self.get_resources_in_order():
            uri = resource.get_value('path')
            if not self._is_allowed_to_access(context, uri):
                continue
            ref, path, view = split_reference(uri)
            title = resource.get_value('title')
            target = resource.get_value('target')

            # Case 1: External link
            if ref.scheme:
                items.append({
                    'id': 'menu_%s' % resource.name,
                    'path': str(ref),
                    'real_path': None,
                    'title': title,
                    'description': None,
                    'in_path': False,
                    'active': False,
                    'class': None,
                    'target': target,
                    'items': []})
                continue

            # Case 2: Internal link
            # Sub level
            subtabs = resource.get_menu_namespace_level(context, url,
                                                        use_first_child)
            resource = self.get_resource(path, soft=True)
            item_id = 'menu_%s' % resource.name

            # Use first child by default we use the resource itself
            resource_path = path
            # Keep the real path to avoid highlight problems
            resource_original_path = path
            # use first child
            if use_first_child and subtabs:
                sub_path = subtabs[0]['real_path']
                # if the first child is an external link => skip it
                if sub_path is not None:
                    resource_path = sub_path

            # Set active, in_path
            active = in_path = False
            # add default view
            if view:
                resource_method = view[2:]
                item_id += '_%s' % resource_method
            else:
                resource_method = resource.get_default_view_name()
            resource_abspath_and_view = '%s/;%s' % (resource.abspath,
                                                    resource_method)
            if here_abspath_and_view == resource_abspath_and_view:
                active = True
            else:
                # Use the original path for the highlight
                res_abspath = menu_abspath.resolve2(resource_original_path)
                common_prefix = here_abspath.get_prefix(res_abspath)
                # Avoid to always set the root entree 'in_path'
                # If common prefix equals root abspath set in_path to False
                # otherwise compare common_prefix and res_abspath
                if common_prefix != Path('/'):
                    in_path = (common_prefix == res_abspath)

            # Build the new reference with the right path
            ref2 = deepcopy(ref)
            resource = self.get_resource(resource_path)
            ref2.path = context.get_link(resource)
            if view:
                ref2.path += view

            items.append({
                'id': item_id,
                'path': str(ref2),
                'real_path': resource.abspath,
                'title': title,
                'description': None, # FIXME
                'in_path': active or in_path,
                'active': active,
                'class': None,
                'target': target,
                'items': subtabs})

        # Set class
        x = None
        for i, item in enumerate(items):
            if item['active']:
                x = i
                break
            if item['in_path'] and x is None:
                x = i
                break
        if x is not None:
            items[x]['class'] = 'in-path'

        if len(items) > 0:
            # Add class "first" to the first item
            css = items[0]['class'] or ''
            items[0]['class'] = css + ' first'
            # Add class "last" to the last item
            css = items[-1]['class'] or ''
            items[-1]['class'] = css + ' last'

        return items
Exemplo n.º 12
0
    def get_menu_namespace_level(self, context, url, use_first_child=False):
        menu_abspath = self.abspath
        here = context.resource
        here_abspath = here.abspath
        here_view_name = url[-1]
        here_abspath_and_view = "%s/%s" % (here_abspath, here_view_name)
        items = []

        for resource in self.get_resources_in_order():
            uri = resource.get_value("path")
            if not self._is_allowed_to_access(context, uri):
                continue
            ref, path, view = split_reference(uri)
            title = resource.get_value("title")
            target = resource.get_value("target")

            # Case 1: External link
            if ref.scheme:
                items.append(
                    {
                        "id": "menu_%s" % resource.name,
                        "path": str(ref),
                        "real_path": None,
                        "title": title,
                        "description": None,
                        "in_path": False,
                        "active": False,
                        "class": None,
                        "target": target,
                        "items": [],
                    }
                )
                continue

            # Case 2: Internal link
            # Sub level
            subtabs = resource.get_menu_namespace_level(context, url, use_first_child)
            resource = self.get_resource(path, soft=True)
            item_id = "menu_%s" % resource.name

            # Use first child by default we use the resource itself
            resource_path = path
            # Keep the real path to avoid highlight problems
            resource_original_path = path
            # use first child
            if use_first_child and subtabs:
                sub_path = subtabs[0]["real_path"]
                # if the first child is an external link => skip it
                if sub_path is not None:
                    resource_path = sub_path

            # Set active, in_path
            active = in_path = False
            # add default view
            if view:
                resource_method = view[2:]
                item_id += "_%s" % resource_method
            else:
                resource_method = resource.get_default_view_name()
            resource_abspath_and_view = "%s/;%s" % (resource.abspath, resource_method)
            if here_abspath_and_view == resource_abspath_and_view:
                active = True
            else:
                # Use the original path for the highlight
                res_abspath = menu_abspath.resolve2(resource_original_path)
                common_prefix = here_abspath.get_prefix(res_abspath)
                # Avoid to always set the root entree 'in_path'
                # If common prefix equals root abspath set in_path to False
                # otherwise compare common_prefix and res_abspath
                if common_prefix != Path("/"):
                    in_path = common_prefix == res_abspath

            # Build the new reference with the right path
            ref2 = deepcopy(ref)
            resource = self.get_resource(resource_path)
            ref2.path = context.get_link(resource)
            if view:
                ref2.path += view

            items.append(
                {
                    "id": item_id,
                    "path": str(ref2),
                    "real_path": resource.abspath,
                    "title": title,
                    "description": None,  # FIXME
                    "in_path": active or in_path,
                    "active": active,
                    "class": None,
                    "target": target,
                    "items": subtabs,
                }
            )

        # Set class
        x = None
        for i, item in enumerate(items):
            if item["active"]:
                x = i
                break
            if item["in_path"] and x is None:
                x = i
                break
        if x is not None:
            items[x]["class"] = "in-path"

        if len(items) > 0:
            # Add class "first" to the first item
            css = items[0]["class"] or ""
            items[0]["class"] = css + " first"
            # Add class "last" to the last item
            css = items[-1]["class"] or ""
            items[-1]["class"] = css + " last"

        return items