Example #1
0
    def isRange(cls, value, minrange, maxrange):
        """
        Range testing is inclusive on the margins
        minrange <= value <= maxrange
        So minrange == 10 and value == 10 is true
        Float values can be defined with comma's or periods.
        
        Minrange and maxrange are dates or numbers.
        Note that minrange is the relative number of days from today, if defined as number
        The maxrange is the relative number of days if defined as number.
        
            >>> v = Validator()
            >>> v.isRange('today', -2, 2)
            True
            >>> v.isRange('today', -100, 200)
            True
            >>> v.isRange('today', 100, 200)
            False
            >>> v.isRange('today', '3/3/2002', '3/12/2030')
            True
            >>> v.isRange('today', 'today', 'today')
            False
            >>> v.isRange('5/11/1988', '4/11/1988', '6/11/1988')
            True
            >>> v.isRange('5/11/1988', '4/11/2004', '6/11/2004')
            False
            >>> v.isRange('10/10/2015', 'today', '7200')
            True
            >>> v.isRange('10/10/2015', 'today', '720')
            False
            >>> v.isRange('50', '0', '100')
            True
            >>> v.isRange(50, 0, 100)
            True
            >>> v.isRange(-100, 50, 100)
            False
            >>> v.isRange(10,30,100)
            False
            >>> v.isRange('0.4',0,1)
            True
            >>> v.isRange('0,4','0,3','0,8')
            True
            >>> v.isRange('','0,3','0,8')
            False
            >>> v.isRange(None, None, None)
            False
        """
        if not value:
            return False
        value = cls.arg2Arg(value)
        minrange = cls.arg2Arg(minrange)                            # Allow conversion of 'today' or 'infinite'
        maxrange = cls.arg2Arg(maxrange)        
        
        if cls.isDate(value):                                        # Format is date string?

            d = DateTime(date=value)
            thisyear, thismonth, thisday = d.s2date(cls.arg2Arg('today'))    # Get totay for relative calculation
            year, month, day = d.s2date(value)
            
            if cls.isDate(minrange):                                # Minrange is defined as date?
                minyear, minmonth, minday = d.s2date(minrange)
            else:                                                    # Else it be relative number of days instead
                minyear, minmonth, minday = d.futureday(thisyear, thismonth, thisday, number=int(minrange))        

            if cls.isDate(maxrange):
                maxyear, maxmonth, maxday = d.s2date(maxrange)
            else:
                maxyear, maxmonth, maxday = d.futureday(thisyear, thismonth, thisday, number=int(maxrange))        

            if minyear < year < maxyear:                            # Precheck, to avoid the limit in 1970 < mktime < 2040
                return True
            if not (1970 < year < 2040):                            # Can't check other than this interval when using mktime in dating
                return False                                        # when on of the limits is same as range years. False to be sure
                
            # Now we have to check the dates, but still one of the ranges can be outside the 1970 < mktime < 2040 range
            # so we'll crop them on both limits, which is safe because we know that thisyear is already within the limits.
            
            return d.dates2difference((min(2040, max(1970, minyear)), minmonth, minday), (year, month, day)) > 0 and\
                   d.dates2difference((min(2040, max(1970, maxyear)), maxmonth, maxday), (year, month, day)) < 0
        else:
            try:
                if isinstance(minrange, basestring):
                    minrange = float(minrange.replace(',','.'))
                if isinstance(maxrange, basestring):
                    maxrange = float(maxrange.replace(',','.'))
                if isinstance(value, basestring):
                    value = float(value.replace(',','.'))
            except:
                return False  # Any error as with None or string, aswer false

        result = minrange <= value <= maxrange
        return result
Example #2
0
    def isRange(cls, value, minrange, maxrange):
        """
        Range testing is inclusive on the margins
        minrange <= value <= maxrange
        So minrange == 10 and value == 10 is true
        Float values can be defined with comma's or periods.
        
        Minrange and maxrange are dates or numbers.
        Note that minrange is the relative number of days from today, if defined as number
        The maxrange is the relative number of days if defined as number.
        
            >>> v = Validator()
            >>> v.isRange('today', -2, 2)
            True
            >>> v.isRange('today', -100, 200)
            True
            >>> v.isRange('today', 100, 200)
            False
            >>> v.isRange('today', '3/3/2002', '3/12/2030')
            True
            >>> v.isRange('today', 'today', 'today')
            False
            >>> v.isRange('5/11/1988', '4/11/1988', '6/11/1988')
            True
            >>> v.isRange('5/11/1988', '4/11/2004', '6/11/2004')
            False
            >>> v.isRange('10/10/2015', 'today', '7200')
            True
            >>> v.isRange('10/10/2015', 'today', '720')
            False
            >>> v.isRange('50', '0', '100')
            True
            >>> v.isRange(50, 0, 100)
            True
            >>> v.isRange(-100, 50, 100)
            False
            >>> v.isRange(10,30,100)
            False
            >>> v.isRange('0.4',0,1)
            True
            >>> v.isRange('0,4','0,3','0,8')
            True
            >>> v.isRange('','0,3','0,8')
            False
            >>> v.isRange(None, None, None)
            False
        """
        if not value:
            return False
        value = cls.arg2Arg(value)
        minrange = cls.arg2Arg(
            minrange)  # Allow conversion of 'today' or 'infinite'
        maxrange = cls.arg2Arg(maxrange)

        if cls.isDate(value):  # Format is date string?

            d = DateTime(date=value)
            thisyear, thismonth, thisday = d.s2date(
                cls.arg2Arg('today'))  # Get totay for relative calculation
            year, month, day = d.s2date(value)

            if cls.isDate(minrange):  # Minrange is defined as date?
                minyear, minmonth, minday = d.s2date(minrange)
            else:  # Else it be relative number of days instead
                minyear, minmonth, minday = d.futureday(thisyear,
                                                        thismonth,
                                                        thisday,
                                                        number=int(minrange))

            if cls.isDate(maxrange):
                maxyear, maxmonth, maxday = d.s2date(maxrange)
            else:
                maxyear, maxmonth, maxday = d.futureday(thisyear,
                                                        thismonth,
                                                        thisday,
                                                        number=int(maxrange))

            if minyear < year < maxyear:  # Precheck, to avoid the limit in 1970 < mktime < 2040
                return True
            if not (
                    1970 < year < 2040
            ):  # Can't check other than this interval when using mktime in dating
                return False  # when on of the limits is same as range years. False to be sure

            # Now we have to check the dates, but still one of the ranges can be outside the 1970 < mktime < 2040 range
            # so we'll crop them on both limits, which is safe because we know that thisyear is already within the limits.

            return d.dates2difference((min(2040, max(1970, minyear)), minmonth, minday), (year, month, day)) > 0 and\
                   d.dates2difference((min(2040, max(1970, maxyear)), maxmonth, maxday), (year, month, day)) < 0
        else:
            try:
                if isinstance(minrange, basestring):
                    minrange = float(minrange.replace(',', '.'))
                if isinstance(maxrange, basestring):
                    maxrange = float(maxrange.replace(',', '.'))
                if isinstance(value, basestring):
                    value = float(value.replace(',', '.'))
            except:
                return False  # Any error as with None or string, aswer false

        result = minrange <= value <= maxrange
        return result