def _to_python(self, value, state): """ Will check just the type here and return value to be validated in validate_python """ #will add more beautiful validation here after #integrate the complex widgets for lists and dicts #print "Im in hash validator the value i recieved is ",value if self.not_empty: if len(value) == 0: raise validators.Invalid( 'Empty hash passed when not_empty is set', value, state) #concert the data to proper format final_hash = {} for hash_data in value: final_hash[hash_data['keyfield']] = hash_data['valuefield'] #check the type firstly tmp = {} if type(tmp) != type(final_hash): raise validators.Invalid( 'The value passed to MinionHashValidator should be a dict object', final_hash, state) #print value return final_hash
def validate_python(self, value, state): # pylint: disable-msg=C0111 if not self.username_regex.match(value): raise validators.Invalid( self.message('invalid_username', state, username=value), value, state) if value in self.username_blacklist: raise validators.Invalid( self.message('blacklist', state, username=value), value, state)
def validate_python(self, value, state): # pylint: disable-msg=C0111 try: # just prove that we *cannot* retrieve a person for the username # pylint: disable-msg=W0612 people = People.by_username(value) except InvalidRequestError: return except: raise validators.Invalid( self.message('create_error', state, user=value), value, state) raise validators.Invalid(self.message('exists', state, user=value), value, state)
def validate_python(self, value, state): #print value import re if self.regex_string: try: compiled_regex = re.compile(self.regex_string) except Exception, e: raise validators.Invalid( 'The passed regex_string is not a valid expression' % self.regex_string, value, state) for dict_value in value.itervalues(): if not re.match(compiled_regex, str(dict_value)): raise validators.Invalid( 'The %s doesnt match to the regex expression that was supplied' % dict_value, value, state)
def validate_python(self, value, state): # http://xkcd.com/936/ if value.lower() in (u'correct horse battery staple', u'correcthorsebatterystaple', u'tr0ub4dor&3'): raise validators.Invalid(self.message('xkcd', state), value, state) if "pwquality" in modules: try: pw_quality = pwquality.PWQSettings() pw_quality.read_config() pw_quality.check(value, None, None) except pwquality.PWQError as (e, msg): raise validators.Invalid( self.message('pwquality', state) % {'pwq_msg': msg}, value, state)
def validate_python(self, value, state): """ The actual validator """ #firstly run the supers one if self.min and self.min: if value < self.min: raise validators.Invalid( 'The number you entered should be bigger that %d' % (self.min), value, state) if self.max and self.max: if value > self.max: raise validators.Invalid( 'The number you entered exceeds the %d' % (self.max), value, state)
def validate_python(self, value, state): # pylint: disable-msg=C0111 for blocked in self.email_blacklist: if value.endswith(blocked): raise validators.Invalid( self.message('blacklist', state, email=value), value, state)
def validate_python(self, value, state): # pylint: disable-msg=C0111 if value not in ('system', 'bugzilla', 'cla', 'cvs', 'bzr', 'git', \ 'hg', 'mtn', 'svn', 'shell', 'torrent', 'tracker', \ 'tracking', 'user', 'pkgdb'): raise validators.Invalid( self.message('invalid_type', state, type=value), value, state)
def validate_python(self, value, state): # pylint: disable-msg=C0111 ev = EmailValidator() try: ev.validate_or_raise(value) except: raise validators.Invalid(self.message('invalid', state), value, state)
def validate_python(self, value, state): if value is None: return try: float(value) except: raise validators.Invalid( self.message('no_float', state, value=value), value, state)
def validate_python(self, value, state): VALID_CHARS = "0123456789abcdefABCDEF " for char in value: if char not in VALID_CHARS: raise validators.Invalid( self.message('invalid_key', state, char=char), value, state)
def validate_python(self, value, state): # pylint: disable-msg=C0111 try: # Just make sure the group already exists # pylint: disable-msg=W0612 group = Groups.by_name(value) except InvalidRequestError: raise validators.Invalid( self.message('no_group', state, group=value), value, state)
def validate_python(self, value, state): # http://xkcd.com/936/ if value.lower() in (u'correct horse battery staple', u'correcthorsebatterystaple', u'tr0ub4dor&3'): raise validators.Invalid(self.message('xkcd', state), value, state) diversity = set(value) if len(diversity) < 2: raise validators.Invalid(self.message('strength', state), value, state) length = len(value) if length >= 20: return if length < 9: raise validators.Invalid(self.message('strength', state), value, state) lower = upper = digit = space = symbol = False for c in value: if c.isalpha(): if c.islower(): lower = True else: upper = True elif c.isdigit(): digit = True elif c.isspace(): space = True else: symbol = True if upper and lower and digit and symbol: if length >= 9: return elif upper and lower and (digit or symbol): if length >= 10: return elif (lower or upper) and (digit or symbol): if length >= 12: return raise validators.Invalid(self.message('strength', state), value, state)
def _to_python(self, value, state): # pylint: disable-msg=C0111,W0613 if value is None: return None else: try: return float(value) except: raise validators.Invalid( self.message('no_float', state, value=value), value, state)
def _to_python(self, value, state): """ Will check just the type here and return value to be validated in validate_python """ try: value = float(value) except (ValueError, TypeError): raise validators.Invalid('The field should be a float', value, state) return float(value)
def validate_python(self, value, state): # pylint: disable-msg=C0111 # value = value.file.read() keylines = value.split('\n') for keyline in keylines: if not keyline: continue keyline = keyline.strip() validline = re.match('^(rsa|ssh-rsa) [ \t]*[^ \t]+.*$', keyline) if not validline: raise validators.Invalid( self.message('invalid_key', state, key=keyline), value, state)
def validate_python(self, values, state): # Check that a name was entered name = values.get(self.name_field) if not name: raise validators.Invalid(self.message('noname', state), values, state) # If override is set, then we skip the rest of testing if values.get(self.override, False): return # Check that the name is more than one word split_name = name.split(u' ') if len(split_name) < 2: raise validators.Invalid(self.message('lastfirst', state), values, state) # Check for initials for name_part in split_name: if len(name_part.rstrip(u'.')) <= 1: raise validators.Invalid(self.message('initial', state), values, state)
def validate_python(self, values, state): errors = {} # If override is set, then we skip the rest of testing if values.get(self.override, False): return # Check for initials, only first or last name etc. name = values.get(self.name_field) name_regex = re.compile(r'^\S{2}\S*\b.*\b\S{2}\S*$', flags=re.UNICODE) if not name_regex.match(name): errors[self.name_field] = self.message('initial', state) # raise errors if errors: error_list = errors.items() error_list.sort() error_message = '<br>\n'.join( ['%s: %s' % (name, values) for name, values in error_list]) raise validators.Invalid(error_message, values, state, error_dict=errors)
def _to_python(self, value, state): aos = value.strip() badAccession = False badSeq = False fasta = False #First check if accession. Check by attempting to get fasta from Uniprot. If fails, badAccession will be set to true url = "http://www.uniprot.org/uniprot/" + value.strip() + ".fasta" handle = urllib.urlopen(url) if aos.startswith('>'): seq = ''.join(aos.splitlines()[1:]) badAccession = True fasta = True else: seq = aos try: record = Bio.SeqIO.read(handle, 'fasta') except ValueError: badAccession = True # If it's a bad accession, try it out as a then simple sequence if badAccession: seq = seq.upper() seq = re.sub(r'[^\x20-\x7E]', '', seq) # Using IUPAC amino acid alphabet. Verify sequence only has these characters peptide = Seq(seq, IUPAC.protein) if not Bio.Alphabet._verify_alphabet(peptide): badSeq = True #If badAccession and badSeq, raise as invalid. Both must be bad. if badAccession and badSeq: raise validators.Invalid('Invalid Uniprot Accession or Sequence', value, state) return value
def _to_python(self, value, state): """ Will check just the type here and return value to be validated in validate_python """ #will add more beautiful validation here after #integrate the complex widgets for lists and dicts #print "Im in the list validator the value i recieved is : ",value if self.not_empty: if len(value) == 0: raise validators.Invalid( 'Empty list passed when not_empty is set', value, state) tmp = [] if type(tmp) != type(value): value = list(value) #concert the data to proper format final_list = [] for hash_data in value: final_list.extend(hash_data.values()) return final_list
class PasswordStrength(validators.UnicodeString): '''Make sure that a password meets our strength requirements''' messages = { 'strength': _('Passwords must meet certain strength requirements. If they have a mix of symbols, upper and lowercase letters, and digits they must be at least 9 characters. If they have a mix of upper and lowercase letters and digits they must be at least 10 characters. If they have lowercase letters and digits, they must be at least 12 characters. Letters alone need to have at least 3 different characters and be 20 or more characters in length.' ), 'xkcd': _('Malicious hackers read xkcd, you know'), 'pwquality': _(r'libpwquality reports this is a weak password: %(pwq_msg)s'), } def validate_python(self, value, state): # http://xkcd.com/936/ if value.lower() in (u'correct horse battery staple', u'correcthorsebatterystaple', u'tr0ub4dor&3'): raise validators.Invalid(self.message('xkcd', state), value, state) if "pwquality" in modules: try: pw_quality = pwquality.PWQSettings() pw_quality.read_config() pw_quality.check(value, None, None) except pwquality.PWQError as (e, msg): raise validators.Invalid( self.message('pwquality', state) % {'pwq_msg': msg}, value, state) diversity = set(value) if len(diversity) < 2: raise validators.Invalid(self.message('strength', state), value, state) length = len(value) if length >= 20: return if length < 9: raise validators.Invalid(self.message('strength', state), value, state) lower = upper = digit = space = symbol = False for c in value: if c.isalpha(): if c.islower(): lower = True else: upper = True elif c.isdigit(): digit = True elif c.isspace(): space = True else: symbol = True if upper and lower and digit and symbol: if length >= 9: return elif upper and lower and (digit or symbol): if length >= 10: return elif (lower or upper) and (digit or symbol): if length >= 12: return raise validators.Invalid(self.message('strength', state), value, state)
def validate_python(self, value, state): # pylint: disable-msg=C0111 if value not in available_languages() + ['C']: raise validators.Invalid( self.message('not_available', state, lang=value), value, state)
def _to_python(self, value, state): try: return self.enum_type.from_string(value) except ValueError: raise validators.Invalid(self.message('invalid', state), value, state)
def validate_python(self, value, state): # pylint: disable-msg=C0111 if not self.pass_regex.match(value): raise validators.Invalid(self.message('invalid_pass', state, username=value), value, state)
def validate_python(self, value, state): # pylint: disable-msg=C0111 if value.endswith('@fedoraproject.org'): raise validators.Invalid(self.message('no_loop', state), value, state)
def validate_python(self, value, state): # pylint: disable-msg=C0111 if value not in ('username', 'sponsor', 'role_type', 'role_status', \ 'creation', 'approval'): raise validators.Invalid( _("Invalid sort key.") % value, value, state)