def processInput(self, ctx, key, args): namer = self._namer(key) # Get the form field values as a (y,m,d) tuple ymd = [args.get(namer(part), [''])[0].strip() for part in ('year', 'month', 'day')] # Remove parts that were not entered. ymd = [p for p in ymd if p] # Nothing entered means None otherwise we need all three. if not ymd: ymd = None elif len(ymd) != 3: raise validation.FieldValidationError("Invalid date") # So, we have what looks like a good attempt to enter a date. if ymd is not None: # If a 2-char year is allowed then prepend the century. if self.twoCharCutoffYear is not None and len(ymd[0]) == 2: try: if int(ymd[0]) >= self.twoCharCutoffYear: century = '19' else: century = '20' ymd[0] = century + ymd[0] except ValueError: pass # By now, we should have a year of at least 4 characters. if len(ymd[0]) < 4: if self.twoCharCutoffYear is not None: msg = "Please enter a 2 or 4 digit year" else: msg = "Please enter a 4 digit year" raise validation.FieldValidationError(msg) # Map to integers try: ymd = [int(p) for p in ymd] except ValueError, e: raise validation.FieldValidationError("Invalid date")
def toType(self, value): if not (value is None): value = value.strip() if not value: return None result_list = [] for x in re.split(',|;', value): try: x = x.strip() if x.find('/') > 0: result_list.append(datatypes.IPv4Subnet.fromString(x)) else: result_list.append( datatypes.IPv4Subnet.fromString(x + '/32')) except datatypes.InvalidSubnet: raise validation.FieldValidationError('Invalid subnet in list') except: # XXX: Error logs? print "Unknown field error. System exception message: ", str( sys.exc_info()[0]) raise validation.FieldValidationError('Invalid subnet in list') return result_list
def parseDate(self, value): try: y, m, d = [int(p) for p in value.split('-')] except ValueError: raise validation.FieldValidationError('Invalid date') try: value = date(y, m, d) except ValueError, e: raise validation.FieldValidationError('Invalid date: ' + str(e))
def processInput(self, ctx, key, args): charset = util.getPOSTCharset(ctx) pwds = [pwd.decode(charset) for pwd in args.get(key, [])] if len(pwds) == 0: pwd = '' elif len(pwds) == 1: raise validation.FieldValidationError('Please enter the password twice for confirmation.') else: if pwds[0] != pwds[1]: raise validation.FieldValidationError('Passwords do not match.') return self.original.validate(pwds[0])
def toType(self, value): if not (value is None): value = value.strip() if not value: return None try: value = datatypes.IPv4Address.fromString(value) except datatypes.InvalidIPAddress: raise validation.FieldValidationError('Invalid IP address') except: print "Unknown field error. System exception message: ", sys.exc_info( )[0] raise validation.FieldValidationError('Invalid IP address') return value
def toType(self, value): if not (value is None): value = value.strip() if not value: return None try: temp_mask = datatypes.mask_to_cidr(value) value = datatypes.IPv4Address.fromString(value) except datatypes.InvalidSubnet: raise validation.FieldValidationError('Invalid subnet mask') except: print "Unknown field error. System exception message: ", sys.exc_info( )[0] raise validation.FieldValidationError('Invalid subnet mask') return value
def toType(self, value): if value is None: return None try: value = date(*value) except (TypeError, ValueError), e: raise validation.FieldValidationError('Invalid date: ' + str(e))
def processInput(self, ctx, key, args): namer = self._namer(key) # Get the form field values as a (y,m,d) tuple ymd = [args.get(namer(part), [''])[0].strip() for part in ('year', 'month', 'day')] # Remove parts that were not entered. ymd = [p for p in ymd if p] # Nothing entered means None otherwise we need all three. if not ymd: ymd = None elif len(ymd) != 3: raise validation.FieldValidationError("Invalid date") # So, we have what looks like a good attempt to enter a date. if ymd is not None: # Map to integers try: ymd = [int(p) for p in ymd] except ValueError, e: raise validation.FieldValidationError("Invalid date")
def toType(self, value): if value is not None: value = value.strip() if not value: return None if value not in ('True', 'False'): raise validation.FieldValidationError( '%r should be either True or False' % value) return value == 'True'
def toType(self, value): if not (value is None): value = value.strip() if not value: return None try: if (value.find('/') > 0): value = datatypes.IPv4Subnet.fromString(value) else: value = datatypes.IPv4Subnet.fromString(value + '/32') except datatypes.InvalidSubnet: raise validation.FieldValidationError('Invalid subnet') except: # XXX: Error logs? print "Unknown field error. System exception message: ", str( sys.exc_info()[0]) raise validation.FieldValidationError('Invalid subnet') return value
def toType(self, value): if not (value is None): value = value.strip() if not value: return None try: value = datatypes.IPv4AddressRange.fromString(value) except: raise validation.FieldValidationError('Invalid IP address range') return value
def processInput(self, ctx, key, args): namer = self._namer(key) value = [args.get(namer(part), [''])[0].strip() for part in ('year', 'month')] value = [p for p in value if p] if not value: value = None elif len(value) != 2: raise validation.FieldValidationError("Invalid date") if value is not None: try: value = [int(p) for p in value] except ValueError, e: raise validation.FieldValidationError("Invalid date") if value[1] < 0 or value[1] > 99: raise validation.FieldValidationError("Invalid year. Please enter a two-digit year.") if value[0] > self.cutoffYear: value[0] = 1900 + value[0] else: value[0] = 2000 + value[0] value.append(1)
def toType(self, value): if not (value is None): value = value.strip() if not value: return None value = value.replace(',', '.') try: value = float(value) except: raise validation.FieldValidationError('Invalid number') return value
def toType(self, value): if value is not None: value = value.strip() if not value: return None # "Cast" the value to the correct type. For some strange reason, # Python's decimal.Decimal type raises an ArithmeticError when it's # given a dodgy value. try: value = self.cast(value) except (ValueError, ArithmeticError): raise validation.FieldValidationError("Not a valid number") return value
def parseTime(self, value): if '.' in value: value, ms = value.split('.') else: ms = 0 try: parts = value.split(':') if len(parts) < 2 or len(parts) > 3: raise ValueError() if len(parts) == 2: h, m = parts s = 0 else: h, m, s = parts h, m, s, ms = int(h), int(m), int(s), int(ms) except: raise validation.FieldValidationError('Invalid time') try: value = time(h, m, s, ms) except ValueError, e: raise validation.FieldValidationError('Invalid time: ' + str(e))