/
page_filters.py
42 lines (31 loc) · 1.24 KB
/
page_filters.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
"""
This is a collection of tags and filters for :class:`~integreat_cms.cms.models.pages.page.Page` objects.
"""
from __future__ import annotations
from typing import TYPE_CHECKING
from django import template
if TYPE_CHECKING:
from ..models import Page
from ..models.pages.page import PageQuerySet
register = template.Library()
@register.simple_tag
def get_depth_in(node: Page, pageset: PageQuerySet) -> int:
"""
This tag returns the depth of node within the tree/pages in pageset.
:param node : the page
:param pageset: The pages (all pages or pages chosen by filter)
:return: the depth of node within the tree/pages in pageset
"""
if node.parent not in pageset:
return 0
return node.depth - get_highest_anscentor_in(node, pageset).depth
def get_highest_anscentor_in(node: Page, pageset: PageQuerySet) -> Page:
"""
This tag returns the highest (farthest) ancestor of node within the tree/pages in pageset.
:param node : the page
:param pageset: The pages (all pages or pages chosen by filter)
:return: the highest (farthest) ancestor of node within the tree/pages in pageset
"""
if node.parent in pageset:
return get_highest_anscentor_in(node.parent, pageset)
return node