def f(values): if isinstance(values, (list, tuple)): cardnumber, cc_type = values else: cardnumber, cc_type = values, None exc = Invalid() type_ok = not f.require_type if f.require_type and cc_type is None: m = _msg(f.msg, "credit_card.require_type", "no credit card type specified") exc.add_error_message(f.cc_type_field, m) elif not (cc_type is None) and cc_type not in f.types: m = _msg(f.msg, "credit_card.type_check", "unrecognized credit card type") exc.add_error_message(f.cc_type_field, m) else: type_ok = True try: if type_ok: _cc.check_credit_card(cardnumber, cc_type) else: _cc.check_credit_card(cardnumber) except _cc.CreditCardValidationException: m = _msg(f.msg, "credit_card.invalid", "invalid credit card number") exc.add_error_message(f.cc_field, m) if exc.errors: raise exc else: return values
def f(value): try: username, domain = value.split('@', 1) except ValueError: raise Invalid(_msg(f.msg, 'email.format', 'invalid format')) if not _usernameRE.match(username): raise Invalid(_msg(f.msg, 'email.username', 'invalid username')) if not _domainRE.match(domain): raise Invalid(_msg(f.msg, 'email.domain', 'invalid domain')) if f.check_dns: try: a = DNS.DnsRequest(domain, qtype='mx').req().answers if not a: a = DNS.DnsRequest(domain, qtype='a').req().answers dnsdomains = [x['data'] for x in a] except (socket.error, DNS.DNSError), e: raise Invalid(_msg(f.msg, 'email.socket_error', 'socket error')) if not dnsdomains: raise Invalid(_msg(f.msg, 'email.domain_error', 'no such domain'))
def f(value): cant_check = ( f.check_exists and set(f.schemas).difference(set(('http', 'https'))) ) if cant_check: raise RuntimeError(_error_message) schema, netloc, path, params, query, fragment = urllib.parse.urlparse( value ) if schema not in f.schemas: raise Invalid(_msg(f.msg, "url.schema", "schema not allowed")) if schema == '' and f.default_schema: schema = f.default_schema if netloc == '' and f.default_host: netloc = f.default_host url = urllib.parse.urlunparse( (schema, netloc, path, params, query, fragment) ) if f.check_exists: newpath = urllib.parse.urlunparse( ('', '', path, params, query, fragment) ) if schema == 'http': conn = http.client.HTTPConnection elif schema == 'https': conn = http.client.HTTPSConnection else: assert False, "not reached" try: c = conn(netloc) c.request('HEAD', newpath) res = c.getresponse() except (http.client.HTTPException, socket.error) as e: raise Invalid(_msg(f.msg, "url.http_error", "http error")) else: if 200 <= res.status < 400: # this fudges on redirects. return url raise Invalid(_msg(f.msg, 'url.not_exists', "url not OK")) return url
def f(values): if isinstance(values, (list, tuple)): cardnumber, cc_type=values else: cardnumber, cc_type=values, None exc=Invalid() type_ok=not f.require_type if f.require_type and cc_type is None: m=_msg(f.msg, "credit_card.require_type", "no credit card type specified") exc.add_error_message(f.cc_type_field, m) elif not (cc_type is None) and cc_type not in f.types: m=_msg(f.msg, "credit_card.type_check", "unrecognized credit card type") exc.add_error_message(f.cc_type_field, m) else: type_ok=True try: if type_ok: _cc.check_credit_card(cardnumber, cc_type) else: _cc.check_credit_card(cardnumber) except _cc.CreditCardValidationException: m=_msg(f.msg, "credit_card.invalid", "invalid credit card number") exc.add_error_message(f.cc_field, m) if exc.errors: raise exc else: return values
def f(value): try: username, domain = value.split('@', 1) except ValueError: raise Invalid(_msg(f.msg, 'email.format', 'invalid format')) if not _usernameRE.match(username): raise Invalid(_msg(f.msg, 'email.username', 'invalid username')) if not _domainRE.match(domain): raise Invalid(_msg(f.msg, 'email.domain', 'invalid domain')) if f.check_dns: try: a = DNS.DnsRequest(domain, qtype='mx').req().answers if not a: a = DNS.DnsRequest(domain, qtype='a').req().answers dnsdomains = [x['data'] for x in a] except (socket.error, DNS.DNSError), e: raise Invalid(_msg(f.msg, 'email.socket_error', 'socket error')) if not dnsdomains: raise Invalid( _msg(f.msg, 'email.domain_error', 'no such domain'))
def f(value): if f.check_exists and set(f.schemas).difference(set( ('http', 'https'))): m = "existence check not supported for schemas other than http and https" raise RuntimeError(m) schema, netloc, path, params, query, fragment = urlparse.urlparse( value) if schema not in f.schemas: raise Invalid(_msg(f.msg, "url.schema", "schema not allowed")) if schema == '' and f.default_schema: schema = f.default_schema if netloc == '' and f.default_host: netloc = f.default_host url = urlparse.urlunparse( (schema, netloc, path, params, query, fragment)) if f.check_exists: newpath = urlparse.urlunparse( ('', '', path, params, query, fragment)) if schema == 'http': conn = httplib.HTTPConnection elif schema == 'https': conn = httplib.HTTPSConnection else: assert False, "not reached" try: c = conn(netloc) c.request('HEAD', newpath) res = c.getresponse() except (httplib.HTTPException, socket.error), e: raise Invalid(_msg(f.msg, "url.http_error", "http error")) else: if 200 <= res.status < 400: # this fudges on redirects. return url raise Invalid(_msg(f.msg, 'url.not_exists', "url not OK"))