from django.core.exceptions import ValidationError as DjangoValidationError
from modularodm.exceptions import ValidationError as MODMValidationError

class TokenError(Exception):
    pass


class TokenHandlerNotFound(TokenError):
    def __init__(self, action, *args, **kwargs):
        super(TokenHandlerNotFound, self).__init__(*args, **kwargs)

        self.action = action


class UnsupportedSanctionHandlerKind(Exception):
    pass


class OSFError(Exception):
    """Base class for exceptions raised by the Osf application"""
    pass


class NodeError(OSFError):
    """Raised when an action cannot be performed on a Node model"""
    pass


class NodeStateError(NodeError):
    """Raised when the Node's state is not suitable for the requested action

    Example: Node.remove_node() is called, but the node has non-deleted children
    """
    pass


class SanctionTokenError(TokenError):
    """Base class for errors arising from the user of a sanction token."""
    pass


class InvalidSanctionRejectionToken(TokenError):
    """Raised if a Sanction subclass disapproval token submitted is invalid
     or associated with another admin authorizer
    """
    message_short = 'Invalid Token'
    message_long = 'This disapproval link is invalid. Are you logged into the correct account?'


class InvalidSanctionApprovalToken(TokenError):
    """Raised if a Sanction subclass approval token submitted is invalid
     or associated with another admin authorizer
    """
    message_short = 'Invalid Token'
    message_long = 'This approval link is invalid. Are you logged into the correct account?'


class InvalidTagError(OSFError):
    """Raised when attempting to perform an invalid operation on a tag"""
    pass


class TagNotFoundError(OSFError):
    """Raised when attempting to perform an operation on an absent tag"""
    pass


class UserNotAffiliatedError(OSFError):
    """Raised if a user attempts to add an institution that is not currently
    one of its affiliations.
    """
    message_short = 'User not affiliated'
    message_long = 'This user is not affiliated with this institution.'


class ValidationError(MODMValidationError, DjangoValidationError):
    """Raised on database validation failure. This exists for compatibility with both modular-odm and Django."""
    pass

import contextlib

from django.core.exceptions import ValidationError as DjangoValidationError
from modularodm.exceptions import ValidationError as MODMValidationError

class TokenError(Exception):
    pass


class TokenHandlerNotFound(TokenError):
    def __init__(self, action, *args, **kwargs):
        super(TokenHandlerNotFound, self).__init__(*args, **kwargs)

        self.action = action


class UnsupportedSanctionHandlerKind(Exception):
    pass


class OSFError(Exception):
    """Base class for exceptions raised by the Osf application"""
    pass


class NodeError(OSFError):
    """Raised when an action cannot be performed on a Node model"""
    pass


class NodeStateError(NodeError):
    """Raised when the Node's state is not suitable for the requested action

    Example: Node.remove_node() is called, but the node has non-deleted children
    """
    pass


class SanctionTokenError(TokenError):
    """Base class for errors arising from the user of a sanction token."""
    pass


class InvalidSanctionRejectionToken(TokenError):
    """Raised if a Sanction subclass disapproval token submitted is invalid
     or associated with another admin authorizer
    """
    message_short = 'Invalid Token'
    message_long = 'This disapproval link is invalid. Are you logged into the correct account?'


class InvalidSanctionApprovalToken(TokenError):
    """Raised if a Sanction subclass approval token submitted is invalid
     or associated with another admin authorizer
    """
    message_short = 'Invalid Token'
    message_long = 'This approval link is invalid. Are you logged into the correct account?'


class InvalidTagError(OSFError):
    """Raised when attempting to perform an invalid operation on a tag"""
    pass


class TagNotFoundError(OSFError):
    """Raised when attempting to perform an operation on an absent tag"""
    pass


class UserNotAffiliatedError(OSFError):
    """Raised if a user attempts to add an institution that is not currently
    one of its affiliations.
    """
    message_short = 'User not affiliated'
    message_long = 'This user is not affiliated with this institution.'


class ValidationError(MODMValidationError, DjangoValidationError):
    """Raised on database validation failure. This exists for compatibility with both modular-odm and Django."""
    pass


@contextlib.contextmanager
def reraise_django_validation_error():
    try:
        yield
    except DjangoValidationError as err:
        raise ValidationError(*err.args)


Add reraise_django_validation_error helper