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#
Check if the user associated with the given request is a staff member. |
|
Check if the user associated with the given request is authenticated. |
|
Check if the user associated with the given request is anonymous. |
|
Check if the user associated with the given request is a superuser. |
|
Build a check for membership in any of the given groups. |
|
Build a check for at least one of the given permissions. |
|
Build a check for a specific object-level permission. |
|
Build a check for membership in all of the given groups. |
|
Build a check for all of the given permissions. |
|
Check if the user associated with the given request is active. |
|
Check if the user’s email is verified. |
|
Check if the user has an associated profile. |
|
Check if the request is an AJAX request. |
|
Check if the request is secure (HTTPS). |
|
Build a check for the request method matching any of the given methods. |
|
Build a check for a user attribute equalling a specific value. |
|
Build a check for group membership combined with a permission. |
|
Check if Django is running in debug mode. |
|
Combine multiple check functions with AND or OR logic. |
|
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_verifiedfield.- 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"), )