| 1821 | |
| 1822 | # CAPostalCodeField ############################################################## |
| 1823 | |
| 1824 | CAPostalCodeField validates that the data is a six-character Canadian postal code. |
| 1825 | >>> from django.contrib.localflavor.ca.forms import CAPostalCodeField |
| 1826 | >>> f = CAPostalCodeField() |
| 1827 | >>> f.clean('T2S 2H7') |
| 1828 | u'T2S 2H7' |
| 1829 | >>> f.clean('T2S 2H') |
| 1830 | Traceback (most recent call last): |
| 1831 | ... |
| 1832 | ValidationError: [u'Enter a postal code in the format XXX XXX.'] |
| 1833 | >>> f.clean('2T6 H8I') |
| 1834 | Traceback (most recent call last): |
| 1835 | ... |
| 1836 | ValidationError: [u'Enter a postal code in the format XXX XXX.'] |
| 1837 | >>> f.clean('T2S2H') |
| 1838 | Traceback (most recent call last): |
| 1839 | ... |
| 1840 | ValidationError: [u'Enter a postal code in the format XXX XXX.'] |
| 1841 | >>> f.clean(90210) |
| 1842 | Traceback (most recent call last): |
| 1843 | ... |
| 1844 | ValidationError: [u'Enter a postal code in the format XXX XXX.'] |
| 1845 | >>> f.clean(None) |
| 1846 | Traceback (most recent call last): |
| 1847 | ... |
| 1848 | ValidationError: [u'This field is required.'] |
| 1849 | >>> f.clean('') |
| 1850 | Traceback (most recent call last): |
| 1851 | ... |
| 1852 | ValidationError: [u'This field is required.'] |
| 1853 | >>> f = CAPostalCodeField(required=False) |
| 1854 | >>> f.clean('T2S 2H7') |
| 1855 | u'T2S 2H7' |
| 1856 | >>> f.clean('T2S2H7') |
| 1857 | Traceback (most recent call last): |
| 1858 | ... |
| 1859 | ValidationError: [u'Enter a postal code in the format XXX XXX.'] |
| 1860 | >>> f.clean('T2S 2H') |
| 1861 | Traceback (most recent call last): |
| 1862 | ... |
| 1863 | ValidationError: [u'Enter a postal code in the format XXX XXX.'] |
| 1864 | >>> f.clean('2T6 H8I') |
| 1865 | Traceback (most recent call last): |
| 1866 | ... |
| 1867 | ValidationError: [u'Enter a postal code in the format XXX XXX.'] |
| 1868 | >>> f.clean('T2S2H') |
| 1869 | Traceback (most recent call last): |
| 1870 | ... |
| 1871 | ValidationError: [u'Enter a postal code in the format XXX XXX.'] |
| 1872 | >>> f.clean(90210) |
| 1873 | Traceback (most recent call last): |
| 1874 | ... |
| 1875 | ValidationError: [u'Enter a postal code in the format XXX XXX.'] |
| 1876 | >>> f.clean(None) |
| 1877 | u'' |
| 1878 | >>> f.clean('') |
| 1879 | u'' |
| 1880 | |
| 1881 | # CAPhoneNumberField ########################################################## |
| 1882 | |
| 1883 | CAPhoneNumberField validates that the data is a valid Canadian phone number, |
| 1884 | including the area code. It's normalized to XXX-XXX-XXXX format. |
| 1885 | Note: This test is exactly the same as the USPhoneNumberField except using a real |
| 1886 | Candian area code |
| 1887 | |
| 1888 | >>> from django.contrib.localflavor.ca.forms import CAPhoneNumberField |
| 1889 | >>> f = CAPhoneNumberField() |
| 1890 | >>> f.clean('403-555-1212') |
| 1891 | u'403-555-1212' |
| 1892 | >>> f.clean('4035551212') |
| 1893 | u'403-555-1212' |
| 1894 | >>> f.clean('403 555-1212') |
| 1895 | u'403-555-1212' |
| 1896 | >>> f.clean('(403) 555-1212') |
| 1897 | u'403-555-1212' |
| 1898 | >>> f.clean('403 555 1212') |
| 1899 | u'403-555-1212' |
| 1900 | >>> f.clean('403.555.1212') |
| 1901 | u'403-555-1212' |
| 1902 | >>> f.clean('403.555-1212') |
| 1903 | u'403-555-1212' |
| 1904 | >>> f.clean(' (403) 555.1212 ') |
| 1905 | u'403-555-1212' |
| 1906 | >>> f.clean('555-1212') |
| 1907 | Traceback (most recent call last): |
| 1908 | ... |
| 1909 | ValidationError: [u'Phone numbers must be in XXX-XXX-XXXX format.'] |
| 1910 | >>> f.clean('403-55-1212') |
| 1911 | Traceback (most recent call last): |
| 1912 | ... |
| 1913 | ValidationError: [u'Phone numbers must be in XXX-XXX-XXXX format.'] |
| 1914 | >>> f.clean(None) |
| 1915 | Traceback (most recent call last): |
| 1916 | ... |
| 1917 | ValidationError: [u'This field is required.'] |
| 1918 | >>> f.clean('') |
| 1919 | Traceback (most recent call last): |
| 1920 | ... |
| 1921 | ValidationError: [u'This field is required.'] |
| 1922 | |
| 1923 | >>> f = CAPhoneNumberField(required=False) |
| 1924 | >>> f.clean('403-555-1212') |
| 1925 | u'403-555-1212' |
| 1926 | >>> f.clean('4035551212') |
| 1927 | u'403-555-1212' |
| 1928 | >>> f.clean('403 555-1212') |
| 1929 | u'403-555-1212' |
| 1930 | >>> f.clean('(403) 555-1212') |
| 1931 | u'403-555-1212' |
| 1932 | >>> f.clean('403 555 1212') |
| 1933 | u'403-555-1212' |
| 1934 | >>> f.clean('403.555.1212') |
| 1935 | u'403-555-1212' |
| 1936 | >>> f.clean('403.555-1212') |
| 1937 | u'403-555-1212' |
| 1938 | >>> f.clean(' (403) 555.1212 ') |
| 1939 | u'403-555-1212' |
| 1940 | >>> f.clean('555-1212') |
| 1941 | Traceback (most recent call last): |
| 1942 | ... |
| 1943 | ValidationError: [u'Phone numbers must be in XXX-XXX-XXXX format.'] |
| 1944 | >>> f.clean('403-55-1212') |
| 1945 | Traceback (most recent call last): |
| 1946 | ... |
| 1947 | ValidationError: [u'Phone numbers must be in XXX-XXX-XXXX format.'] |
| 1948 | >>> f.clean(None) |
| 1949 | u'' |
| 1950 | >>> f.clean('') |
| 1951 | u'' |
| 1952 | |
| 1953 | # CAProvinceField ################################################################ |
| 1954 | |
| 1955 | CAProvinceField validates that the data is either an abbreviation or name of a |
| 1956 | Canadian province. |
| 1957 | >>> from django.contrib.localflavor.ca.forms import CAProvinceField |
| 1958 | >>> f = CAProvinceField() |
| 1959 | >>> f.clean('ab') |
| 1960 | u'AB' |
| 1961 | >>> f.clean('BC') |
| 1962 | u'BC' |
| 1963 | >>> f.clean('nova scotia') |
| 1964 | u'NS' |
| 1965 | >>> f.clean(' manitoba ') |
| 1966 | u'MB' |
| 1967 | >>> f.clean('T2S 2H7') |
| 1968 | Traceback (most recent call last): |
| 1969 | ... |
| 1970 | ValidationError: [u'Enter a Canadian province or territory.'] |
| 1971 | >>> f.clean(None) |
| 1972 | Traceback (most recent call last): |
| 1973 | ... |
| 1974 | ValidationError: [u'This field is required.'] |
| 1975 | >>> f.clean('') |
| 1976 | Traceback (most recent call last): |
| 1977 | ... |
| 1978 | ValidationError: [u'This field is required.'] |
| 1979 | |
| 1980 | >>> f = CAProvinceField(required=False) |
| 1981 | >>> f.clean('ab') |
| 1982 | u'AB' |
| 1983 | >>> f.clean('BC') |
| 1984 | u'BC' |
| 1985 | >>> f.clean('nova scotia') |
| 1986 | u'NS' |
| 1987 | >>> f.clean(' manitoba ') |
| 1988 | u'MB' |
| 1989 | >>> f.clean('T2S 2H7') |
| 1990 | Traceback (most recent call last): |
| 1991 | ... |
| 1992 | ValidationError: [u'Enter a Canadian province or territory.'] |
| 1993 | >>> f.clean(None) |
| 1994 | u'' |
| 1995 | >>> f.clean('') |
| 1996 | u'' |
| 1997 | |
| 1998 | # CAProvinceSelect ############################################################### |
| 1999 | |
| 2000 | CAProvinceSelect is a Select widget that uses a list of Canadian provinces/territories |
| 2001 | as its choices. |
| 2002 | >>> from django.contrib.localflavor.ca.forms import CAProvinceSelect |
| 2003 | >>> w = CAProvinceSelect() |
| 2004 | >>> print w.render('province', 'AB') |
| 2005 | <select name="province"> |
| 2006 | <option value="AB" selected="selected">Alberta</option> |
| 2007 | <option value="BC">British Columbia</option> |
| 2008 | <option value="MB">Manitoba</option> |
| 2009 | <option value="NB">New Brunswick</option> |
| 2010 | <option value="NF">Newfoundland and Labrador</option> |
| 2011 | <option value="NT">Northwest Territories</option> |
| 2012 | <option value="NS">Nova Scotia</option> |
| 2013 | <option value="NU">Nunavut</option> |
| 2014 | <option value="ON">Ontario</option> |
| 2015 | <option value="PE">Prince Edward Island</option> |
| 2016 | <option value="QC">Quebec</option> |
| 2017 | <option value="SK">Saskatchewan</option> |
| 2018 | <option value="YK">Yukon</option> |
| 2019 | </select> |
| 2020 | |
| 2021 | # CASocialInsuranceNumberField ################################################# |
| 2022 | >>> from django.contrib.localflavor.ca.forms import CASocialInsuranceNumberField |
| 2023 | >>> f = CASocialInsuranceNumberField() |
| 2024 | >>> f.clean('046-454-286') |
| 2025 | u'046-454-286' |
| 2026 | >>> f.clean('046-454-287') |
| 2027 | Traceback (most recent call last): |
| 2028 | ... |
| 2029 | ValidationError: [u'Enter a valid Canadian Social Insurance number in XXX-XXX-XXXX format.'] |
| 2030 | >>> f.clean('046 454 286') |
| 2031 | Traceback (most recent call last): |
| 2032 | ... |
| 2033 | ValidationError: [u'Enter a valid Canadian Social Insurance number in XXX-XXX-XXXX format.'] |
| 2034 | >>> f.clean('046-44-286') |
| 2035 | Traceback (most recent call last): |
| 2036 | ... |
| 2037 | ValidationError: [u'Enter a valid Canadian Social Insurance number in XXX-XXX-XXXX format.'] |
| 2038 | |