def __init__(
     self, format='DHMSX', min_value=None, max_value=None, *args, **kw):
     Field.__init__(self, *args, **kw)
     self.format = format
     self.min_value = min_value
     self.max_value = max_value
     self.widget = IntervalWidget(format)
Beispiel #2
0
 def __init__(
     self, format='ymDHMSX', min_value=None, max_value=None, *args, **kw):
     Field.__init__(self, *args, **kw)
     self.format = format
     self.min_value = min_value
     self.max_value = max_value
     self.widget = IntervalWidget(format)
    def clean(self, value):

        kw = dict(days=0, hours=0, minutes=0, seconds=0, microseconds=0)

        if HAVE_RELATIVEDELTA:
            kw['years'] = 0
            kw['months'] = 0

        for letter, desc in format_desc.items():

            if letter not in self.format:
                continue

            def raiseError(fieldName):
                raise ValidationError(
                    _("Bad value in ''%s'' subfield.") % _(fieldName))

            if 'BAD' in value:
                raiseError(value['BAD'])

            try:
                kw[desc] = int(value[desc])
            except ValueError:
                raiseError(desc)

        try:
            if HAVE_RELATIVEDELTA:
                cleaned_value = relativedelta(**kw)
            else:
                cleaned_value = timedelta(**kw)

        except OverflowError:
            raise ValidationError(_("This value is too large"))

        if cleaned_value is not None:
            if self.min_value is not None:
                if cleaned_value < self.min_value:
                    raise ValidationError(
                        _("This interval must be at least: %s") %
                        self.min_value)

            if self.max_value is not None:
                if cleaned_value > self.max_value:
                    raise ValidationError(
                        _("This interval must be no more than: %s") %
                        self.max_value)

        if self.required:
            if (cleaned_value == timedelta(0)) or (
                    HAVE_RELATIVEDELTA and cleaned_value == relativedelta(0)):
                raise ValidationError(self.default_error_messages['required'])

        return Field.clean(self, cleaned_value)
Beispiel #4
0
    def clean(self, value):

        kw = dict(years=0, months=0, days=0, hours=0, minutes=0, seconds=0, microseconds=0)

        for letter, desc in format_desc.items():

            if letter not in self.format:
                continue

            def raiseError(fieldName):
                raise ValidationError(
                    _("Bad value in ''%s'' subfield.") % _(fieldName))

            if 'BAD' in value:
                raiseError(value['BAD'])

            try:
                kw[desc] = int(value[desc])
            except ValueError:
                raiseError(desc)

        try:
            cleaned_value = relativedelta(
                years=kw['years'],
                months=kw['months'],
                days=kw['days'],
                seconds=kw['seconds'],
                minutes=kw['minutes'],
                hours=kw['hours'],
                microseconds=kw['microseconds'])
        except OverflowError:
            raise ValidationError(_("This value is too large"))

        if cleaned_value is not None:
            if self.min_value is not None:
                if cmp_relativedeltas(cleaned_value, self.min_value) == -1:
                    raise ValidationError(
                    _("This interval must be at least: %s") % self.min_value)

            if self.max_value is not None:
                if cmp_relativedeltas(cleaned_value, self.max_value) == 1:
                    raise ValidationError(
                        _("This interval must be no more than: %s"
                          ) % self.max_value)

        if self.required:
            if cleaned_value == timedelta(0):
                raise ValidationError(self.default_error_messages['required'])

        return Field.clean(self, cleaned_value)