"""Generic validator extensions to the base set in django
"""
from django.core.validators import *
from django.utils.translation import gettext_lazy as _

class IllegalIfOtherFieldEquals(object):
    def __init__(self, other_field, other_value, error_message=None):
        self.other_field = other_field
        self.other_value = other_value
        self.error_message = (error_message or
            lazy_inter(_(
                "This field must not be given if %(field)s is %(value)s"), {
            'field': other_field, 'value': other_value}))
        self.always_test = True

    def __call__(self, field_data, all_data):
        if (all_data.has_key(self.other_field) and
            all_data[self.other_field] == self.other_value and field_data):
            raise ValidationError(self.error_message)

class IllegalIfOtherFieldDoesNotEqual(IllegalIfOtherFieldEquals):
    def __init__(self, other_field, other_value, error_message=None):
        super(IllegalIfOtherFieldDoesNotEqual, self).__init__(
            other_field, other_value, (error_message or
                lazy_inter(_(
                "This field must not be given if %(field)s is not %(value)s"), {
                'field': other_field, 'value': other_value})))

    def __call__(self, field_data, all_data):
        if (all_data.has_key(self.other_field) and
            all_data[self.other_field] != self.other_value and field_data):
            raise ValidationError(self.error_message)
    
class IllegalIfOtherFieldsGiven(object):
    def __init__(self, other_field_names, error_message=_("Please enter one field or the others.")):
        self.other, self.error_message = other_field_names, error_message
        self.always_test = True

    def __call__(self, field_data, all_data):
        if not field_data: return
        for field in self.other:
            if all_data.get(field, False):
                raise ValidationError, self.error_message

class IllegalIfOtherFieldGiven(IllegalIfOtherFieldsGiven):
    "Like RequiredIfOtherFieldsGiven, but takes a single field name instead of a list."
    def __init__(self, other_field_name, error_message=_("Please enter one field or the other.")):
        super(IllegalIfOtherFieldGiven, self).__init__([other_field_name], error_message)

