Skip to content

context_processor

utilities.frontend_menu.context_processor ¤

Functions¤

get_menu(user) ¤

Generate the user menu in HTML, depending on its permissions.

Parameters:

Name Type Description Default
user User

The user to generate the menu for.

required

Returns:

Name Type Description
str str

An HTML string with the menu items.

Source code in utilities\functions.py
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
def get_menu(user: User) -> str:
    """Generate the user menu in HTML, depending on its permissions.

    Args:
        user (User): The user to generate the menu for.

    Returns:
        str: An HTML string with the menu items.
    """
    if user.is_anonymous:  # TODO check this new logic if this function is ever used
        perms = Permission.objects.filter(Q(user=user) | Q(group__in=user.groups.all()))
        is_superuser = False
    else:
        perms = Permission.objects.filter(Q(user=user) | Q(group__in=user.groups.all()))
        is_superuser = user.is_superuser

    menu = ""
    for tab in menu_struct():
        items = ""
        last_is_divider = True
        i = 0

        for elements in tab.items:
            if elements.name == "":
                if last_is_divider:
                    continue
                items += menu_item_divider_html
                last_is_divider = True
                continue

            app, codename = elements.permission.split(".")
            if (
                perms.filter(content_type__app_label=app, codename=codename).exists()
                or is_superuser
            ):
                try:
                    url = reverse(elements.url_name)
                except Exception:
                    msg = f"URL '{elements.url_name}' not found when creating menu."
                    getLogger().debug(msg)
                    continue

                items += menu_item_html.format(url=url, name=elements.name)
                last_is_divider = False
                i += 1

        if i > 0:
            menu += menu_tab_html.format(tab=tab.name, items=items)

    return menu

menu(request) ¤

Context processor for creating the menus.

Using this processor is indicated in the TEMPLATES section of settings.py.

Parameters:

Name Type Description Default
request HttpRequest

The request to process, including the 'user'.

required

Returns:

Type Description
dict[str, str]

Dict[str, str]: A dictionary with the menu items.

Source code in utilities\frontend_menu\context_processor.py
19
20
21
22
23
24
25
26
27
28
29
30
def menu(request: HttpRequest) -> dict[str, str]:
    """Context processor for creating the menus.

    Using this processor is indicated in the TEMPLATES section of settings.py.

    Args:
        request (HttpRequest): The request to process, including the 'user'.

    Returns:
        Dict[str, str]: A dictionary with the menu items.
    """
    return {"menu": get_menu(request.user)}