# -*- coding: utf-8 -*-

from app.models import Usuario
from django.core.exceptions import PermissionDenied
from tarea.models import Actividad

def role_required(function=None, redirect_field_name=None,role=None):
    def _dec(view_func):
        def _view(request, *args, **kwargs):
            #if role == None:
            #    raise "ERROR"
            if request.user.is_authenticated() and request.user.usuario.rol == role:
                return view_func(request, *args, **kwargs)
            else:
                raise PermissionDenied()

        _view.__name__ = view_func.__name__
        _view.__dict__ = view_func.__dict__
        _view.__doc__ = view_func.__doc__

        return _view

    if function is None:
        return _dec
    else:
        return _dec(function)

def owner_required(function=None, redirect_field_name=None,model=None):
    def _dec(view_func):
        def _view(request, *args, **kwargs):
            if request.user.is_authenticated() and Actividad.objects.get(id__exact=kwargs['id']).autor==request.user:
                return view_func(request, *args, **kwargs)
            else:
                raise PermissionDenied()

        _view.__name__ = view_func.__name__
        _view.__dict__ = view_func.__dict__
        _view.__doc__ = view_func.__doc__

        return _view

    if function is None:
        return _dec
    else:
        return _dec(function)
