ó
ø¢TQc        "   @@ s
  d  Z  d d l m Z m Z d d l Z d d l m Z d d l m Z d d l	 m
 Z
 m Z m Z d d l m Z d= Z d Z d Z d Z d Z d> Z d3 e f d4 „  ƒ  YZ d5 e f d6 „  ƒ  YZ d7 e
 f d8 „  ƒ  YZ d9 e f d: „  ƒ  YZ d; e f d< „  ƒ  YZ d S(?   u   
Chinese-specific form helpers
i    (   t   absolute_importt   unicode_literalsN(   t   CN_PROVINCE_CHOICES(   t   ValidationError(   t	   CharFieldt
   RegexFieldt   Select(   t   ugettext_lazyu   CNProvinceSelectu   CNPostCodeFieldu   CNIDCardFieldu   CNPhoneNumberFieldu   CNCellNumberFieldu   ^\d{15}(\d{2}[0-9xX])?$u   ^\d{6}$u   ^\d{3,4}-\d{7,8}(-\d+)?$u   ^1[358]\d{9}$i   i   i   i   i   i   i   i   i   i    i!   i"   i#   i$   i%   i)   i*   i+   i,   i-   i.   i2   i3   i4   i5   i6   i=   i>   i?   i@   iA   iG   iQ   i[   t   CNProvinceSelectc           B@ s   e  Z d  Z d d „ Z RS(   uD   
    A select widget with list of Chinese provinces as choices.
    c         C@ s    t  t |  ƒ j | d t ƒd  S(   Nt   choices(   t   superR   t   __init__R   (   t   selft   attrs(    (    s<   ../Django//lib/python/django/contrib/localflavor/cn/forms.pyR   H   s    N(   t   __name__t
   __module__t   __doc__t   NoneR   (    (    (    s<   ../Django//lib/python/django/contrib/localflavor/cn/forms.pyR   D   s   t   CNPostCodeFieldc           B@ s*   e  Z d  Z i e d ƒ d 6Z d „  Z RS(   uf   
    A form field that validates as Chinese post code.
    Valid code is XXXXXX where X is digit.
    u'   Enter a post code in the format XXXXXX.u   invalidc         O@ s    t  t |  ƒ j t | | Ž d  S(   N(   R
   R   R   t   POST_CODE_RE(   R   t   argst   kwargs(    (    s<   ../Django//lib/python/django/contrib/localflavor/cn/forms.pyR   W   s    (   R   R   R   t   _t   default_error_messagesR   (    (    (    s<   ../Django//lib/python/django/contrib/localflavor/cn/forms.pyR   N   s   t   CNIDCardFieldc           B@ s{   e  Z d  Z i e d ƒ d 6e d ƒ d 6e d ƒ d 6e d ƒ d 6Z d	 d
 d „ Z d „  Z d „  Z d „  Z d „  Z	 RS(   u›  
    A form field that validates as Chinese Identification Card Number.

    This field would check the following restrictions:
        * the length could only be 15 or 18.
        * if the length is 18, the last digit could be x or X.
        * has a valid checksum.(length 18 only)
        * has a valid birthdate.
        * has a valid location.

    The checksum algorithm is described in GB11643-1999.
    u+   ID Card Number consists of 15 or 18 digits.u   invalidu&   Invalid ID Card Number: Wrong checksumu   checksumu'   Invalid ID Card Number: Wrong birthdateu   birthdayu+   Invalid ID Card Number: Wrong location codeu   locationi   i   c         O@ s#   t  t |  ƒ j | | | | Ž d  S(   N(   R
   R   R   (   R   t
   max_lengtht
   min_lengthR   R   (    (    s<   ../Django//lib/python/django/contrib/localflavor/cn/forms.pyR   o   s    c         C@ sË   t  t |  ƒ j | ƒ | s  d St j t | ƒ sH t |  j d ƒ ‚ n  |  j | ƒ sm t |  j d ƒ ‚ n  |  j	 | ƒ s’ t |  j d ƒ ‚ n  | j
 ƒ  } |  j | ƒ sÃ t |  j d ƒ ‚ n  d | S(   uD   
        Check whether the input is a valid ID Card Number.
        u    u   invalidu   birthdayu   locationu   checksumu   %s(   R
   R   t   cleant   ret   matcht
   ID_CARD_RER   t   error_messagest   has_valid_birthdayt   has_valid_locationt   uppert   has_valid_checksum(   R   t   value(    (    s<   ../Django//lib/python/django/contrib/localflavor/cn/forms.pyR   s   s    c         C@ s|   d d l  m  } t | ƒ d k r8 | d d !} d } n | d d !} d } y | j | | ƒ t SWn t k
 rw t SXd	 S(
   u}   
        This function would grab the birthdate from the ID card number and test
        whether it is a valid date.
        i    (   t   datetimei   i   i   u   %y%m%di   u   %Y%m%dN(   R%   t   lent   strptimet   Truet
   ValueErrort   False(   R   R$   R%   t   time_stringt   format_string(    (    s<   ../Django//lib/python/django/contrib/localflavor/cn/forms.pyR    Š   s    	c         C@ s   t  | d  ƒ t k S(   uV   
        This method checks if the first two digits in the ID Card are valid.
        i   (   t   intt   CN_LOCATION_CODES(   R   R$   (    (    s<   ../Django//lib/python/django/contrib/localflavor/cn/forms.pyR!   Ÿ   s    c         C@ sK   t  | ƒ d k r t St t d „  d | d  ƒ ƒ d } d | | d k S(   u‡   
        This method checks if the last letter/digit in value is valid
        according to the algorithm the ID Card follows.
        i   c         S@ s   |  t  | ƒ t  d ƒ S(   Nu   0(   t   ord(   t   at   b(    (    s<   ../Django//lib/python/django/contrib/localflavor/cn/forms.pyt   <lambda>°   s    i   i	   i
   i   i   i   i   i   i   i   i   i   u   10X98765432iÿÿÿÿ(   i   i	   i
   i   i   i   i   i   i   i   i   i	   i
   i   i   i   i   (   R&   R(   t   sumt   map(   R   R$   t   checksum_index(    (    s<   ../Django//lib/python/django/contrib/localflavor/cn/forms.pyR#   ¥   s    (
   R   R   R   R   R   R   R   R    R!   R#   (    (    (    s<   ../Django//lib/python/django/contrib/localflavor/cn/forms.pyR   [   s   			t   CNPhoneNumberFieldc           B@ s*   e  Z d  Z i e d ƒ d 6Z d „  Z RS(   uá   
    A form field that validates as Chinese phone number
    A valid phone number could be like:
        010-55555555
    Considering there might be extension phone numbers, so this could also be:
        010-55555555-35
    u   Enter a valid phone number.u   invalidc         O@ s    t  t |  ƒ j t | | Ž d  S(   N(   R
   R6   R   t   PHONE_RE(   R   R   R   (    (    s<   ../Django//lib/python/django/contrib/localflavor/cn/forms.pyR   Ä   s    (   R   R   R   R   R   R   (    (    (    s<   ../Django//lib/python/django/contrib/localflavor/cn/forms.pyR6   ¸   s   t   CNCellNumberFieldc           B@ s*   e  Z d  Z i e d ƒ d 6Z d „  Z RS(   u'  
    A form field that validates as Chinese cell number
    A valid cell number could be like:
        13012345678
    We used a rough rule here, the first digit should be 1, the second could be
    3, 5 and 8, the rest could be what so ever.
    The length of the cell number should be 11.
    u   Enter a valid cell number.u   invalidc         O@ s    t  t |  ƒ j t | | Ž d  S(   N(   R
   R8   R   t   CELL_RE(   R   R   R   (    (    s<   ../Django//lib/python/django/contrib/localflavor/cn/forms.pyR   Õ   s    (   R   R   R   R   R   R   (    (    (    s<   ../Django//lib/python/django/contrib/localflavor/cn/forms.pyR8   È   s   (   u   CNProvinceSelectu   CNPostCodeFieldu   CNIDCardFieldu   CNPhoneNumberFieldu   CNCellNumberField("   i   i   i   i   i   i   i   i   i   i    i!   i"   i#   i$   i%   i)   i*   i+   i,   i-   i.   i2   i3   i4   i5   i6   i=   i>   i?   i@   iA   iG   iQ   i[   (   R   t
   __future__R    R   R   t*   django.contrib.localflavor.cn.cn_provincesR   t   django.formsR   t   django.forms.fieldsR   R   R   t   django.utils.translationR   R   t   __all__R   R   R7   R9   R.   R   R   R   R6   R8   (    (    (    s<   ../Django//lib/python/django/contrib/localflavor/cn/forms.pyt   <module>   sl                                        
]