Exemplo n.º 1
0
    def get_context(self, context, menu_slug, from_depth, to_depth, template, **kwargs):
        site = Site.objects.get_current()
        # allow passing through None or "" ...
        if not from_depth:
            from_depth = 0
        # allow passing through None or "" ...
        if not to_depth:
            to_depth = 100
        base = {
            'to_depth': to_depth,
            'from_depth': from_depth,
            'site': site,
            'template': template or self.template,
        }
        if 'request' in context:
            base.update(request=context['request'])

        # try to go by PK, or if there's no invalid characters (eg: /:_ ...)
        # by menu_slug, otherwise falling back to assuming the input is
        # request.path or whatevers.
        fetched_menuitem = self.get_menuitem(context, menu_slug, site)
        menu_root = fetched_menuitem.obj
        base.update(query=fetched_menuitem.query)

        if menu_root is None:
            return base

        menu_root.is_active = True
        depth_filtered_menu = MenuItem.get_published_annotated_list(
            parent=menu_root, from_depth=from_depth, to_depth=to_depth)

        if 'request' in context:
            marked_annotated_menu = marked_annotated_list(
                request=context['request'],
                tree=depth_filtered_menu)
        else:  # pragma: no cover
            logger.info("Cannot calculate position in tree without a request")
            marked_annotated_menu = depth_filtered_menu

        base.update(menu_root=menu_root,
                    menu_nodes=marked_annotated_menu)
        return base