from django.core.exceptions import PermissionDenied

from django.http import Http404
from .constants import Group

from functools import wraps

def user_is_cid_user_manager(function):
    def wrap(request, *args, **kwargs):
        if (
            request.user.groups.filter(name="cid_user_manager").exists()
            or request.user.is_superuser
        ):
            return function(request, *args, **kwargs)
        else:
            raise PermissionDenied

    wrap.__doc__ = function.__doc__
    wrap.__name__ = function.__name__
    return wrap


def check_user_in_group(*groups: Group):
    def decorator(function):
        @wraps(function)
        def wrapper(request, *args, **kwargs):
            if request.user.is_superuser or request.user.groups.filter(
                name__in=[group.name for group in groups]
            ).exists():
                return function(request, *args, **kwargs)
            raise PermissionDenied

        return wrapper
    return decorator