Code Duplication    Length = 21-26 lines in 2 locations

src/bika/lims/validators.py 2 locations

@@ 1021-1046 (lines=26) @@
1018
validation.register(DurationValidator())
1019
1020
1021
class PercentValidator:
1022
    """ Floatable, >=0, <=100. """
1023
1024
    implements(IValidator)
1025
    name = "percentvalidator"
1026
1027
    def __call__(self, value, *args, **kwargs):
1028
        instance = kwargs['instance']
1029
        # fieldname = kwargs['field'].getName()
1030
        # request = kwargs.get('REQUEST', {})
1031
        # form = request.get('form', {})
1032
1033
        translate = getToolByName(instance, 'translation_service').translate
1034
1035
        try:
1036
            value = float(value)
1037
        except Exception:
1038
            msg = _("Validation failed: percent values must be numbers")
1039
            return to_utf8(translate(msg))
1040
1041
        if value < 0 or value > 100:
1042
            msg = _(
1043
                "Validation failed: percent values must be between 0 and 100")
1044
            return to_utf8(translate(msg))
1045
1046
        return True
1047
1048
1049
validation.register(PercentValidator())
@@ 1251-1271 (lines=21) @@
1248
}
1249
1250
1251
class SortKeyValidator:
1252
    """ Check for out of range values.
1253
    """
1254
1255
    implements(IValidator)
1256
    name = "SortKeyValidator"
1257
1258
    def __call__(self, value, *args, **kwargs):
1259
        instance = kwargs['instance']
1260
        translate = getToolByName(instance, 'translation_service').translate
1261
        try:
1262
            value = float(value)
1263
        except Exception:
1264
            msg = _("Validation failed: value must be float")
1265
            return to_utf8(translate(msg))
1266
1267
        if value < 0 or value > 1000:
1268
            msg = _("Validation failed: value must be between 0 and 1000")
1269
            return to_utf8(translate(msg))
1270
1271
        return True
1272
1273
1274
validation.register(SortKeyValidator())