flex_menu.checks#

Predefined check functions controlling menu item visibility.

All check functions receive a Django request object and optional keyword arguments. They return a boolean value indicating whether the menu item should be visible.

Module Contents#

Functions#

user_is_staff

Check if the user associated with the given request is a staff member.

user_is_authenticated

Check if the user associated with the given request is authenticated.

user_is_anonymous

Check if the user associated with the given request is anonymous.

user_is_superuser

Check if the user associated with the given request is a superuser.

user_in_any_group

Build a check for membership in any of the given groups.

user_has_any_permission

Build a check for at least one of the given permissions.

user_has_object_permission

Build a check for a specific object-level permission.

user_in_all_groups

Build a check for membership in all of the given groups.

user_has_all_permissions

Build a check for all of the given permissions.

user_is_active

Check if the user associated with the given request is active.

user_email_verified

Check if the user’s email is verified.

user_has_profile

Check if the user has an associated profile.

request_is_ajax

Check if the request is an AJAX request.

request_is_secure

Check if the request is secure (HTTPS).

request_method_is

Build a check for the request method matching any of the given methods.

user_attribute_equals

Build a check for a user attribute equalling a specific value.

user_in_group_with_permission

Build a check for group membership combined with a permission.

debug_mode_only

Check if Django is running in debug mode.

combine_checks

Combine multiple check functions with AND or OR logic.

negate_check

Negate a check function (NOT logic).

API#

user_is_staff(request, **kwargs)[source]#

Check if the user associated with the given request is a staff member.

Parameters:
  • request – The HTTP request object.

  • **kwargs – Additional arguments (ignored).

Returns:

True if the user is a staff member, False otherwise.

user_is_authenticated(request, **kwargs)[source]#

Check if the user associated with the given request is authenticated.

Parameters:
  • request – The HTTP request object.

  • **kwargs – Additional arguments (ignored).

Returns:

True if the user is authenticated, False otherwise.

user_is_anonymous(request, **kwargs)[source]#

Check if the user associated with the given request is anonymous.

Parameters:
  • request – The HTTP request object.

  • **kwargs – Additional arguments (ignored).

Returns:

True if the user is anonymous, False otherwise.

user_is_superuser(request, **kwargs)[source]#

Check if the user associated with the given request is a superuser.

Parameters:
  • request – The HTTP request object.

  • **kwargs – Additional arguments (ignored).

Returns:

True if the user is a superuser, False otherwise.

user_in_any_group(*groups)[source]#

Build a check for membership in any of the given groups.

Parameters:

*groups – Variable length argument list of group names to check against.

Returns:

A function that takes a Django request object and optional keyword arguments, and returns True if the user is authenticated and is a member of any of the specified groups, otherwise False.

Example

MenuLink(
    name="Authors only",
    view_name="author-management-page",
    check=user_in_any_group("authors"),
)
user_has_any_permission(*perms: str)[source]#

Build a check for at least one of the given permissions.

Parameters:

*perms – One or more permission strings to check against the user.

Returns:

True if the user has at least one of the specified permissions, False otherwise.

Example

MenuLink(
    name="Authors",
    view_name="book-create",
    check=user_has_any_permission("book.add_book"),
)
user_has_object_permission(perm: str)[source]#

Build a check for a specific object-level permission.

Note: This function is removed as it depended on the instance parameter. For object-level permissions, create custom check functions that get the object from the request context or other means.

Parameters:

perm – The permission codename to check (e.g., ‘blog.change_post’).

Returns:

A function that always returns False with a warning.

user_in_all_groups(*groups)[source]#

Build a check for membership in all of the given groups.

Parameters:

*groups – Variable length argument list of group names to check against.

Returns:

A function that takes a Django request object and optional keyword arguments, and returns True if the user is authenticated and is a member of ALL specified groups, otherwise False.

Example

MenuLink(
    name="Authors and Editors only",
    view_name="special-page",
    check=user_in_all_groups("authors", "editors"),
)
user_has_all_permissions(*perms: str)[source]#

Build a check for all of the given permissions.

Parameters:

*perms – One or more permission strings to check against the user.

Returns:

A function that returns True if the user has ALL specified permissions, False otherwise.

Example

MenuLink(
    name="Full Admin",
    view_name="admin-panel",
    check=user_has_all_permissions(
        "auth.add_user", "auth.change_user", "auth.delete_user"
    ),
)
user_is_active(request, **kwargs)[source]#

Check if the user associated with the given request is active.

Parameters:
  • request – The HTTP request object.

  • **kwargs – Additional arguments (ignored).

Returns:

True if the user is active, False otherwise.

user_email_verified(request, **kwargs)[source]#

Check if the user’s email is verified.

Assumes verified for authenticated users if the user model has no email_verified field.

Parameters:
  • request – The HTTP request object.

  • **kwargs – Additional arguments (ignored).

Returns:

True if the user’s email is verified, False otherwise.

user_has_profile(request, **kwargs)[source]#

Check if the user has an associated profile.

Parameters:
  • request – The HTTP request object.

  • **kwargs – Additional arguments (ignored).

Returns:

True if the user has a profile, False otherwise.

request_is_ajax(request, **kwargs)[source]#

Check if the request is an AJAX request.

Parameters:
  • request – The HTTP request object.

  • **kwargs – Additional arguments (ignored).

Returns:

True if the request is AJAX, False otherwise.

request_is_secure(request, **kwargs)[source]#

Check if the request is secure (HTTPS).

Parameters:
  • request – The HTTP request object.

  • **kwargs – Additional arguments (ignored).

Returns:

True if the request is secure, False otherwise.

request_method_is(*methods)[source]#

Build a check for the request method matching any of the given methods.

Parameters:

*methods – Variable length argument list of HTTP methods to check against.

Returns:

A function that takes a Django request object and returns True if the request method matches any of the specified methods.

Example

MenuLink(
    name="POST only action",
    view_name="post-handler",
    check=request_method_is("POST"),
)
user_attribute_equals(attribute_name: str, expected_value)[source]#

Build a check for a user attribute equalling a specific value.

Parameters:
  • attribute_name – The name of the user attribute to check.

  • expected_value – The expected value to compare against.

Returns:

A function that returns True if the user attribute equals the expected value.

Example

MenuLink(
    name="Premium users only",
    view_name="premium-content",
    check=user_attribute_equals("subscription_type", "premium"),
)
user_in_group_with_permission(group_name: str, permission: str)[source]#

Build a check for group membership combined with a permission.

Parameters:
  • group_name – The name of the group to check.

  • permission – The permission to check.

Returns:

A function that returns True if the user is in the group and has the permission.

Example

MenuLink(
    name="Editor with publish rights",
    view_name="publish-content",
    check=user_in_group_with_permission("editors", "blog.publish_post"),
)
debug_mode_only(request, **kwargs)[source]#

Check if Django is running in debug mode.

Parameters:
  • request – The HTTP request object.

  • **kwargs – Additional arguments (ignored).

Returns:

True if DEBUG=True, False otherwise.

combine_checks(*check_functions, operator='and')[source]#

Combine multiple check functions with AND or OR logic.

Parameters:
  • *check_functions – Variable length argument list of check functions.

  • operator – Either ‘and’ or ‘or’ to specify the combination logic.

Returns:

A combined check function.

Example

MenuLink(
    name="Staff or superuser",
    view_name="admin-area",
    check=combine_checks(user_is_staff, user_is_superuser, operator="or"),
)
negate_check(check_function)[source]#

Negate a check function (NOT logic).

Parameters:

check_function – The check function to negate.

Returns:

A negated check function.

Example

MenuLink(
    name="Non-staff only",
    view_name="public-area",
    check=negate_check(user_is_staff),
)