コード例 #1
0
    def __call__(self, value):
        try:
            super(URLValidator, self).__call__(value)
        except ValidationError as e:
            # Trivial case failed. Try for possible IDN domain
            if value:
                value = smart_unicode(value)
                scheme, netloc, path, query, fragment = six.moves.urllib.parse.urlsplit(value)
                try:
                    netloc = netloc.encode('idna') # IDN -> ACE
                except UnicodeError: # invalid domain part
                    raise e
                url = six.moves.urllib.parse.urlunsplit((scheme, netloc, path, query, fragment))
                super(URLValidator, self).__call__(url)
            else:
                raise
        else:
            url = value

        if self.verify_exists:
            import six.moves.urllib.request, six.moves.urllib.error, six.moves.urllib.parse
            headers = {
                "Accept": "text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5",
                "Accept-Language": "en-us,en;q=0.5",
                "Accept-Charset": "ISO-8859-1,utf-8;q=0.7,*;q=0.7",
                "Connection": "close",
                "User-Agent": self.user_agent,
            }
            try:
                req = six.moves.urllib.request.Request(url, None, headers)
                u = six.moves.urllib.request.urlopen(req)
            except ValueError:
                raise ValidationError(_('Enter a valid URL.'), code='invalid')
            except: # urllib2.URLError, httplib.InvalidURL, etc.
                raise ValidationError(_('This URL appears to be a broken link.'), code='invalid_link')
コード例 #2
0
 def __call__(self, value):
     cleaned = self.clean(value)
     params = {'limit_value': self.limit_value, 'show_value': cleaned}
     if self.compare(cleaned, self.limit_value):
         raise ValidationError(
             self.message % params,
             code=self.code,
             params=params,
         )
コード例 #3
0
 def __call__(self, value):
     """
     Validates that the input matches the regular expression.
     """
     if not self.regex.search(smart_unicode(value)):
         raise ValidationError(self.message, code=self.code)
コード例 #4
0
def validate_integer(value):
    try:
        int(value)
    except (ValueError, TypeError) as e:
        raise ValidationError('')
コード例 #5
0
ファイル: validators.py プロジェクト: guisup/appengine-python
            url = value

        if self.verify_exists:
            import urllib2
            headers = {
                "Accept": "text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5",
                "Accept-Language": "en-us,en;q=0.5",
                "Accept-Charset": "ISO-8859-1,utf-8;q=0.7,*;q=0.7",
                "Connection": "close",
                "User-Agent": self.user_agent,
            }
            try:
                req = urllib2.Request(url, None, headers)
                u = urllib2.urlopen(req)
            except ValueError:
                raise ValidationError(_(u'Enter a valid URL.'), code='invalid')
            except: # urllib2.URLError, httplib.InvalidURL, etc.
                raise ValidationError(_(u'This URL appears to be a broken link.'), code='invalid_link')


def validate_integer(value):
    try:
        int(value)
    except (ValueError, TypeError), e:
        raise ValidationError('')

class EmailValidator(RegexValidator):

    def __call__(self, value):
        try:
            super(EmailValidator, self).__call__(value)