def get_context_data(self, parent_context, *tag_args, **tag_kwargs): # Get page objects request = _get_request(parent_context) user = request.user try: current_page = _get_current_page(parent_context) except UrlNode.DoesNotExist: current_page = None if 'parent' in tag_kwargs: # if we've been provided a parent kwarg then we want to filter parent_value = tag_kwargs['parent'] if isinstance(parent_value, basestring): # if we've been provided a string then we lookup based on the # path/url try: parent = UrlNode.objects.filter( pk__in=backend.pages_for_user(user)).get_for_path( parent_value) except UrlNode.DoesNotExist: return {'menu_items': []} # Can't do parent___cached_key due to polymorphic queryset code. top_pages = parent.children.in_navigation().filter( pk__in=backend.pages_for_user(user)) elif isinstance(parent_value, (int, long)): # If we've been provided an int then we lookup based on the id # of the page top_pages = UrlNode.objects.in_navigation().filter( pk__in=backend.pages_for_user(user), parent_id=parent_value) elif isinstance(parent_value, UrlNode): # If we've been given a Page or UrlNode then there's no lookup # necessary top_pages = parent_value.children.in_navigation().filter( pk__in=backend.pages_for_user(user)) else: raise TemplateSyntaxError("The 'render_menu' tag only allows an URL path, page id or page object for the 'parent' keyword") else: # otherwise get the top level nav for the current page top_pages = UrlNode.objects.toplevel_navigation( current_page=current_page).filter( pk__in=backend.pages_for_user(user)) # Construct a PageNavigationNode for every page, that allows simple iteration of the tree. node_kwargs = get_node_kwargs(tag_kwargs) return { 'parent': parent_context, 'request': request, 'menu_items': [ PageNavigationNode(page, current_page=current_page, user=user, **node_kwargs) for page in top_pages ] }
def get_context_data(self, parent_context, *tag_args, **tag_kwargs): # Get page objects request = _get_request(parent_context) user = request.user try: current_page = _get_current_page(parent_context) except UrlNode.DoesNotExist: current_page = None if "parent" in tag_kwargs: # if we've been provided a parent kwarg then we want to filter parent_value = tag_kwargs["parent"] if isinstance(parent_value, basestring): # if we've been provided a string then we lookup based on the # path/url try: parent = UrlNode.objects.filter(pk__in=backend.pages_for_user(user)).get_for_path(parent_value) except UrlNode.DoesNotExist: return {"menu_items": []} # Can't do parent___cached_key due to polymorphic queryset code. top_pages = parent.children.in_navigation().filter(pk__in=backend.pages_for_user(user)) elif isinstance(parent_value, (int, long)): # If we've been provided an int then we lookup based on the id # of the page top_pages = UrlNode.objects.in_navigation().filter( pk__in=backend.pages_for_user(user), parent_id=parent_value ) elif isinstance(parent_value, UrlNode): # If we've been given a Page or UrlNode then there's no lookup # necessary top_pages = parent_value.children.in_navigation().filter(pk__in=backend.pages_for_user(user)) else: raise TemplateSyntaxError( "The 'render_menu' tag only allows an URL path, page id or page object for the 'parent' keyword" ) else: # otherwise get the top level nav for the current page top_pages = UrlNode.objects.toplevel_navigation(current_page=current_page).filter( pk__in=backend.pages_for_user(user) ) # Construct a PageNavigationNode for every page, that allows simple iteration of the tree. node_kwargs = get_node_kwargs(tag_kwargs) return { "parent": parent_context, "request": request, "menu_items": [ PageNavigationNode(page, current_page=current_page, user=user, **node_kwargs) for page in top_pages ], }
def get_queryset(self): """ Return the QuerySet used to find the pages. """ # This can be limited or expanded in the future user = self.request.user return self.model.objects.filter(pk__in=backend.pages_for_user(user))
def _read_children(self): if not self._children and (self._page.get_level() + 1) < self._max_depth: # level 0 = toplevel. #children = self._page.get_children() # Via MPTT children = self._page.children.in_navigation() children = children._mark_current(self._current_page) if self._user is not None: children = children.filter(pk__in=backend.pages_for_user(self._user)) self._children = children
def _read_children(self): if not self._children and (self._page.get_level() + 1) < self._max_depth: # level 0 = toplevel. #children = self._page.get_children() # Via MPTT children = self._page.children.in_navigation() children = children._mark_current(self._current_page) if self._user is not None: children = children.filter( pk__in=backend.pages_for_user(self._user)) self._children = children