def get_input(field, required): "Get user input" prompt = "Please enter the %s:" % field while 1: if field in ['password1', 'password2']: value = getpass.getpass(prompt=prompt) else: value = raw_input(prompt) if not required: break if required and value.strip() != "": if field not in ['email', 'password1', 'password2']: break if field == 'email': if not EMAIL_RE.match(value): print "Please provide a valid email address." else: break if field == 'password1': try: cracklib.VeryFascistCheck(value) except ValueError, message: print str(message) else: break if field == 'password2': if values['password1'] == value: break else: print 'password2 does not match password1'
def from_addr_check(form, field): "Check from address" if (not EMAIL_RE.match(field.data) and not DOM_RE.match(field.data) and not IPV4_RE.match(field.data) and not IPV4_NET_OR_RANGE_RE.match(field.data) and not ipaddr_is_valid(field.data)): raise validators.ValidationError( _('Provide either a valid IP, email,' ' Domain address, or IP network or range'))
def validate_altrecipients(self, field): "validate alt recipients" if self.usealt.data and not field.data and self.release.data: raise validators.ValidationError( _('Provide atleast one alternative recipient')) if self.usealt.data and field.data and self.release.data: emails = field.data.split(',') for email in emails: if not EMAIL_RE.match(email.strip()): raise validators.ValidationError( _('Invalid email address'))
def _set_type(obj): "Set type of object" if EMAIL_RE.match(obj.from_address): obj.from_addr_type = 1 return if DOM_RE.match(obj.from_address): obj.from_addr_type = 2 return if IPV4_NET_OR_RANGE_RE.match(obj.from_address): obj.from_addr_type = 3 return if ipaddr_is_valid(obj.from_address): obj.from_addr_type = 4 return
def admin_toaddr_check(form, field): "Admin check to field" if field.data != '': #email if EMAIL_RE.match(field.data) or DOM_RE.match(field.data): if '@' in field.data: domain = field.data.split('@')[1] else: domain = field.data try: Session.query(Domain.name).filter(Domain.name == domain).one() except NoResultFound: raise validators.ValidationError( _('The domain %(dom)s is not local') % dict(dom=domain)) elif field.data != 'any': raise validators.ValidationError( _('Provide either a valid email or domain address'))
def check_form(form, field): "validate the form" filteredby = dict(FILTER_BY) filteritems = dict(FILTER_ITEMS) # field <-> filter checks if ((field.data in BOOL_FIELDS and int(form.filtered_by.data) not in BOOL_FILTER) or (field.data in TEXT_FIELDS and int(form.filtered_by.data) not in TEXT_FILTER) or (field.data in TIME_FIELDS and int(form.filtered_by.data) not in TIME_FILTER) or (field.data in NUM_FIELDS and int(form.filtered_by.data) not in NUM_FILTER)): raise validators.ValidationError( _('%(field)s does not support the "%(filt)s" filter') % dict(field=filteritems[field.data], filt=filteredby[form.filtered_by.data])) # time if ((field.data in TEXT_FIELDS or field.data in TIME_FIELDS or field.data in NUM_FIELDS) and form.filtered_value.data in EMPTY_VALUES): raise validators.ValidationError(_('Please supply a value to query')) # numerics if field.data in NUM_FIELDS and not isnumeric(form.filtered_value.data): raise validators.ValidationError(_('The value has to be numeric')) # emails if (field.data in TEXT_FIELDS and int(form.filtered_by.data) in [1, 2] and field.data in EMAIL_FIELDS): if not EMAIL_RE.match(form.filtered_value.data): raise validators.ValidationError( _('%(email)s is not a valid e-mail address.') % dict(email=escape(form.filtered_value.data))) # domains if (field.data in TEXT_FIELDS and int(form.filtered_by.data) in [1, 2] and field.data in DOMAIN_FIELDS): if not DOM_RE.match(form.filtered_value.data): raise validators.ValidationError( _('%(dom)s is not a valid domain address.') % dict(dom=escape(form.filtered_value.data))) # regex on text fields if field.data in TEXT_FIELDS and int(form.filtered_by.data) in [7, 8]: try: re.compile(form.filtered_value.data) except re.error: raise validators.ValidationError(_('Invalid regex supplied')) if field.data in TEXT_FIELDS: # ip if field.data == 'clientip': if not IPV4_RE.match(form.filtered_value.data): raise validators.ValidationError( _('Please provide a valid ipv4 address')) # hostname if field.data == 'hostname': if (not IPV4_RE.match(form.filtered_value.data) and not DOM_RE.match(form.filtered_value.data)): raise validators.ValidationError( _("Please provide a valid hostname or ipv4 address")) if field.data in TIME_FIELDS: if field.data == 'date': try: datetime.date( *time.strptime(form.filtered_value.data, '%Y-%m-%d')[:3]) except ValueError: raise validators.ValidationError( _('Please provide a valid date in YYYY-MM-DD format')) if field.data == 'time': try: datetime.time( *time.strptime(form.filtered_value.data, '%H:%M')[3:6]) except ValueError: raise validators.ValidationError( _('Please provide valid time in HH:MM format'))
def check_email(option, opt_str, value, parser): "check validity of email address" if not EMAIL_RE.match(value): raise OptionValueError("%s is not a valid email address" % value) setattr(parser.values, option.dest, value)