Package gluon :: Module validators
[hide private]
[frames] | no frames]

Module validators

source code

This file is part of the web2py Web Framework Copyrighted by Massimo Di Pierro <mdipierro@cs.depaul.edu> License: LGPLv3 (http://www.gnu.org/licenses/lgpl.html)

Thanks to ga2arch for help with IS_IN_DB and IS_NOT_IN_DB on GAE

Classes [hide private]
  Validator
Root for all validators, mainly for documentation purposes.
  IS_MATCH
example:
  IS_EQUAL_TO
example:
  IS_EXPR
example:
  IS_LENGTH
Checks if length of field's value fits between given boundaries.
  IS_JSON
example:
  IS_IN_SET
example:
  IS_IN_DB
example:
  IS_NOT_IN_DB
example:
  IS_INT_IN_RANGE
Determine that the argument is (or can be represented as) an int, and that it falls within the specified range.
  IS_FLOAT_IN_RANGE
Determine that the argument is (or can be represented as) a float, and that it falls within the specified inclusive range.
  IS_DECIMAL_IN_RANGE
Determine that the argument is (or can be represented as) a Python Decimal, and that it falls within the specified inclusive range.
  IS_NOT_EMPTY
example:
  IS_ALPHANUMERIC
example:
  IS_EMAIL
Checks if field's value is a valid email address.
  IS_LIST_OF_EMAILS
use as follows:...
  IS_GENERIC_URL
Rejects a URL string if any of the following is true: * The string is empty or None * The string uses characters that are not allowed in a URL * The URL scheme specified (if one is specified) is not valid
  IS_HTTP_URL
Rejects a URL string if any of the following is true: * The string is empty or None * The string uses characters that are not allowed in a URL * The string breaks any of the HTTP syntactic rules * The URL scheme specified (if one is specified) is not 'http' or 'https' * The top-level domain (if a host name is specified) does not exist
  IS_URL
Rejects a URL string if any of the following is true: * The string is empty or None * The string uses characters that are not allowed in a URL * The string breaks any of the HTTP syntactic rules * The URL scheme specified (if one is specified) is not 'http' or 'https' * The top-level domain (if a host name is specified) does not exist
  IS_TIME
example:
  UTC
UTC
  IS_DATE
example:
  IS_DATETIME
example:
  IS_DATE_IN_RANGE
example:
  IS_DATETIME_IN_RANGE
example:
  IS_LIST_OF
  IS_LOWER
convert to lower case
  IS_UPPER
convert to upper case
  IS_SLUG
convert arbitrary text string to a slug
  ANY_OF
test if any of the validators in a list return successfully
  IS_EMPTY_OR
dummy class for testing IS_EMPTY_OR
  IS_NULL_OR
dummy class for testing IS_EMPTY_OR
  CLEANUP
example:
  LazyCrypt
Stores a lazy password hash
  CRYPT
example::
  IS_STRONG
example:
  IS_IN_SUBSET
  IS_IMAGE
Checks if file uploaded through file input was saved in one of selected image formats and has dimensions (width and height) within given boundaries.
  IS_UPLOAD_FILENAME
Checks if name and extension of file uploaded through file input matches given criteria.
  IS_IPV4
Checks if field's value is an IP version 4 address in decimal form.
  IS_IPV6
Checks if field's value is an IP version 6 address.
  IS_IPADDRESS
Checks if field's value is an IP Address (v4 or v6).
Functions [hide private]
 
translate(text) source code
 
options_sorter(x, y) source code
 
range_error_message(error_message, what_to_enter, minimum, maximum)
build the error message for the number range validators
source code
 
str2dec(number) source code
 
is_empty(value, empty_regex=None)
test empty field
source code
 
escape_unicode(string)
Converts a unicode string into US-ASCII, using a simple conversion scheme.
source code
 
unicode_to_ascii_authority(authority)
Follows the steps in RFC 3490, Section 4 to convert a unicode authority string into its ASCII equivalent.
source code
 
unicode_to_ascii_url(url, prepend_scheme)
Converts the inputed unicode url into a US-ASCII equivalent.
source code
 
urlify(s, maxlen=80, keep_underscores=False)
Convert incoming string to a simplified ASCII subset.
source code
 
calc_entropy(string)
calculate a simple entropy for a given string
source code
Variables [hide private]
  regex_isint = re.compile(r'^[\+-]?\d+$')
  JSONErrors = (<type 'exceptions.NameError'>, <type 'exceptions...
  have_current = True
  regex1 = re.compile(r'\w+\.\w+')
  regex2 = re.compile(r'%\(([^\)]+)\)\d*(?:\.\d+)?[a-zA-Z]')
  official_url_schemes = ['aaa', 'aaas', 'acap', 'cap', 'cid', '...
  unofficial_url_schemes = ['about', 'adiumxtra', 'aim', 'afp', ...
  all_url_schemes = [None, 'aaa', 'aaas', 'acap', 'cap', 'cid', ...
  http_schemes = [None, 'http', 'https']
  url_split_regex = re.compile(r'^(([^:/\?#]+):)?(//([^/\?#]*))?...
  label_split_regex = re.compile(r'[\.\u3002\uff0e\uff61]')
  official_top_level_domains = ['ac', 'ad', 'ae', 'aero', 'af', ...
  regex_time = re.compile(r'((?P<h>[0-9]+))([^0-9 ]+(?P<m>[0-9 ]...
  utc = UTC()
  lowerset = frozenset([u'a', u'b', u'c', u'd', u'e', u'f', u'g'...
  upperset = frozenset([u'A', u'B', u'C', u'D', u'E', u'F', u'G'...
  numberset = frozenset([u'0', u'1', u'2', u'3', u'4', u'5', u'6...
  sym1set = frozenset([u'!', u'#', u'$', u'%', u'&', u'(', u')',...
  sym2set = frozenset([u'"', u''', u'+', u',', u'-', u'.', u'/',...
  otherset = frozenset([u'0', u'1', u'2', u'3', u'4', u'5', u'6'...
  __package__ = 'gluon'
Function Details [hide private]

escape_unicode(string)

source code 

Converts a unicode string into US-ASCII, using a simple conversion scheme.
Each unicode character that does not have a US-ASCII equivalent is
converted into a URL escaped form based on its hexadecimal value.
For example, the unicode character '\u4e86' will become the string '%4e%86'

:param string: unicode string, the unicode string to convert into an
    escaped US-ASCII form
:returns: the US-ASCII escaped form of the inputted string
:rtype: string

@author: Jonathan Benn

unicode_to_ascii_authority(authority)

source code 

Follows the steps in RFC 3490, Section 4 to convert a unicode authority
string into its ASCII equivalent.
For example, u'www.Alliancefrançaise.nu' will be converted into
'www.xn--alliancefranaise-npb.nu'

:param authority: unicode string, the URL authority component to convert,
                  e.g. u'www.Alliancefrançaise.nu'
:returns: the US-ASCII character equivalent to the inputed authority,
         e.g. 'www.xn--alliancefranaise-npb.nu'
:rtype: string
:raises Exception: if the function is not able to convert the inputed
    authority

@author: Jonathan Benn

unicode_to_ascii_url(url, prepend_scheme)

source code 

Converts the inputed unicode url into a US-ASCII equivalent. This function
goes a little beyond RFC 3490, which is limited in scope to the domain name
(authority) only. Here, the functionality is expanded to what was observed
on Wikipedia on 2009-Jan-22:

   Component    Can Use Unicode?
   ---------    ----------------
   scheme       No
   authority    Yes
   path         Yes
   query        Yes
   fragment     No

The authority component gets converted to punycode, but occurrences of
unicode in other components get converted into a pair of URI escapes (we
assume 4-byte unicode). E.g. the unicode character U+4E2D will be
converted into '%4E%2D'. Testing with Firefox v3.0.5 has shown that it can
understand this kind of URI encoding.

:param url: unicode string, the URL to convert from unicode into US-ASCII
:param prepend_scheme: string, a protocol scheme to prepend to the URL if
    we're having trouble parsing it.
    e.g. "http". Input None to disable this functionality
:returns: a US-ASCII equivalent of the inputed url
:rtype: string

@author: Jonathan Benn

urlify(s, maxlen=80, keep_underscores=False)

source code 

Convert incoming string to a simplified ASCII subset. if (keep_underscores): underscores are retained in the string else: underscores are translated to hyphens (default)


Variables Details [hide private]

JSONErrors

Value:
(<type 'exceptions.NameError'>,
 <type 'exceptions.TypeError'>,
 <type 'exceptions.ValueError'>,
 <type 'exceptions.AttributeError'>,
 <type 'exceptions.KeyError'>)

official_url_schemes

Value:
['aaa',
 'aaas',
 'acap',
 'cap',
 'cid',
 'crid',
 'data',
 'dav',
...

unofficial_url_schemes

Value:
['about',
 'adiumxtra',
 'aim',
 'afp',
 'aw',
 'callto',
 'chrome',
 'cvs',
...

all_url_schemes

Value:
[None,
 'aaa',
 'aaas',
 'acap',
 'cap',
 'cid',
 'crid',
 'data',
...

url_split_regex

Value:
re.compile(r'^(([^:/\?#]+):)?(//([^/\?#]*))?([^\?#]*)(\?([^#]*))?(#(.*\
))?')

official_top_level_domains

Value:
['ac',
 'ad',
 'ae',
 'aero',
 'af',
 'ag',
 'ai',
 'al',
...

regex_time

Value:
re.compile(r'((?P<h>[0-9]+))([^0-9 ]+(?P<m>[0-9 ]+))?([^0-9ap ]+(?P<s>\
[0-9]*))?((?P<d>[ap]m))?')

lowerset

Value:
frozenset([u'a',
           u'b',
           u'c',
           u'd',
           u'e',
           u'f',
           u'g',
           u'h',
...

upperset

Value:
frozenset([u'A',
           u'B',
           u'C',
           u'D',
           u'E',
           u'F',
           u'G',
           u'H',
...

numberset

Value:
frozenset([u'0',
           u'1',
           u'2',
           u'3',
           u'4',
           u'5',
           u'6',
           u'7',
...

sym1set

Value:
frozenset([u'!',
           u'#',
           u'$',
           u'%',
           u'&',
           u'(',
           u')',
           u'*',
...

sym2set

Value:
frozenset([u'"',
           u''',
           u'+',
           u',',
           u'-',
           u'.',
           u'/',
           u':',
...

otherset

Value:
frozenset([u'0',
           u'1',
           u'2',
           u'3',
           u'4',
           u'5',
           u'6',
           u'7',
...