Skip to content

Manage request-based menu generation #142

Open
@frague59

Description

@frague59

Hi,

I'ld like to bind a sub-menu part to the authenticated user (children).

I use a function to build this sub menu, and use the check trick to show / hide menu items according to the connected user.

def get_subscription_menu_items() -> list[MenuItem]:
    """
    Get the domain subscription menu items.

    :return: A list of MenuItem objects representing subscription items.
    :rtype: list[MenuItem]
    """
    output: list[MenuItem] = []
    for idx, subscription in enumerate(models.DomainSubscription.objects.all(), 2):
        domains = [("domain", domain.pk) for domain in subscription.domains.all()]
        categories = [("category", category.pk) for category in subscription.categories.all()]

        # Combine into a single list

        all_parameters = domains + categories + [("title", subscription.title)]

        # Generate the query string
        query_string = urlencode(all_parameters)
        _url = reverse("publications:notice_list")

        if query_string:
            _url += f"?{query_string}"

        output.append(
            MenuItem(
                title=subscription.title,
                url=_url,
                icon="list",
                weight=idx * 10,
                check=lambda request: request.user == subscription.user,  # noqa: B023
            )
        )
    return output

Menu.add_item(
    "main",
    MenuItem(
        title=_("Publications"),
        url=reverse("publications:manage_subscriptions"),
        icon="book",
        weight=10,
        children=[
            MenuItem(
                title=_("Subscriptions"),
                url=reverse("publications:manage_subscriptions"),
                icon="book",
                weight=10,
                separator=True,
            ),
        ]
        + get_subscription_menu_items(),
    ),
)

The problem with this approach is that I've to query all Foo instances, and filter "on display".

It could be simpler and nicer I could use directly the request object as parameter on a method / function that generates the menus.

Thanks for you great app !

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions