from django.core.exceptions import PermissionDenied
from .models import Long, LongSeries

def user_is_author_or_long_series_checker_or_long_marker(function):
    def wrap(request, *args, **kwargs):
        series = LongSeries.objects.get(pk=kwargs['pk'])
        if request.user in series.get_author_objects() or request.user.groups.filter(name='long_checker').exists() or request.user.groups.filter(name='long_marker').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 user_is_author_or_long_series_checker(function):
    def wrap(request, *args, **kwargs):
        series = LongSeries.objects.get(pk=kwargs['pk'])
        if request.user in series.get_author_objects() or request.user.groups.filter(name='long_checker').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 user_is_author_or_long_checker_or_long_marker(function):
    def wrap(request, *args, **kwargs):
        long = Long.objects.get(pk=kwargs['pk'])
        if request.user in long.author.all() or request.user.groups.filter(name='long_checker').exists()  or request.user.groups.filter(name='long_marker').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 user_is_author_or_long_checker(function):
    def wrap(request, *args, **kwargs):
        long = Long.objects.get(pk=kwargs['pk'])
        if request.user in long.author.all() or request.user.groups.filter(name='long_checker').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 user_is_long_checker(function):
    def wrap(request, *args, **kwargs):
        if request.user.groups.filter(name='long_checker').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 user_is_long_marker(function):
    def wrap(request, *args, **kwargs):
        if request.user.groups.filter(name='long_marker').exists() or request.user.is_superuser:
            return function(request, *args, **kwargs)
        else:
            raise PermissionDenied
    wrap.__doc__ = function.__doc__
    wrap.__name__ = function.__name__
    return wrap