Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Manage request-based menu generation #142

Open
frague59 opened this issue Dec 14, 2023 · 0 comments
Open

Manage request-based menu generation #142

frague59 opened this issue Dec 14, 2023 · 0 comments

Comments

@frague59
Copy link

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 !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant