from django import template
from module_iam.permission import ResourcePermissionRequiredMixin

register = template.Library()


@register.filter(name='has_resource_permission')
def has_resource_permission(user, resource):
    """
    Check if a user has a specific resource and action permission.

    Args:
        user (User): The user to check for permission.
        resource_action (str): The resource and action string (e.g., "resource_name.action_name").

    Returns:
        bool: True if the user has the specified permission, False otherwise.

    Example usage in a template:
    {% if user|has_resource_permission:"article" %}
        <!-- Render content for users with permission -->
    {% else %}
        <!-- Render content for users without permission -->
    {% endif %}
    """
    # resource, action = resource_action.split(".")
    return ResourcePermissionRequiredMixin().has_resource_permission(user, resource)
