<s> """<STR_LIT>""" <EOL> from __future__ import absolute_import <EOL> import re <EOL> from django . contrib . localflavor . ch . ch_states import STATE_CHOICES <EOL> from django . core . validators import EMPTY_VALUES <EOL> from django . forms import ValidationError <EOL> from django . forms . fields import Field , RegexField , Select <EOL> from django . utils . encoding import smart_unicode <EOL> from django . utils . translation import ugettext_lazy as _ <EOL> id_re = re . compile ( r"<STR_LIT>" ) <EOL> phone_digits_re = re . compile ( r'<STR_LIT>' ) <EOL> class CHZipCodeField ( RegexField ) : <EOL> default_error_messages = { <EOL> '<STR_LIT>' : _ ( '<STR_LIT>' ) , <EOL> } <EOL> def __init__ ( self , max_length = None , min_length = None , * args , ** kwargs ) : <EOL> super ( CHZipCodeField , self ) . __init__ ( r'<STR_LIT>' , <EOL> max_length , min_length , * args , ** kwargs ) <EOL> class CHPhoneNumberField ( Field ) : <EOL> """<STR_LIT>""" <EOL> default_error_messages = { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> } <EOL> def clean ( self , value ) : <EOL> super ( CHPhoneNumberField , self ) . clean ( value ) <EOL> if value in EMPTY_VALUES : <EOL> return u'<STR_LIT>' <EOL> value = re . sub ( '<STR_LIT>' , '<STR_LIT>' , smart_unicode ( value ) ) <EOL> m = phone_digits_re . search ( value ) <EOL> if m : <EOL> return u'<STR_LIT>' % ( value [ <NUM_LIT:0> : <NUM_LIT:3> ] , value [ <NUM_LIT:3> : <NUM_LIT:6> ] , value [ <NUM_LIT:6> : <NUM_LIT:8> ] , value [ <NUM_LIT:8> : <NUM_LIT:10> ] ) <EOL> raise ValidationError ( self . error_messages [ '<STR_LIT>' ] ) <EOL> class CHStateSelect ( Select ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , attrs = None ) : <EOL> super ( CHStateSelect , self ) . __init__ ( attrs , choices = STATE_CHOICES ) <EOL> class CHIdentityCardNumberField ( Field ) : <EOL> """<STR_LIT>""" <EOL> default_error_messages = { <EOL> '<STR_LIT>' : _ ( '<STR_LIT>' ) , <EOL> } <EOL> def has_valid_checksum ( self , number ) : <EOL> given_number , given_checksum = number [ : - <NUM_LIT:1> ] , number [ - <NUM_LIT:1> ] <EOL> new_number = given_number <EOL> calculated_checksum = <NUM_LIT:0> <EOL> fragment = "<STR_LIT>" <EOL> parameter = <NUM_LIT:7> <EOL> first = str ( number [ : <NUM_LIT:1> ] ) <EOL> if first . isalpha ( ) : <EOL> num = ord ( first . upper ( ) ) - <NUM_LIT> <EOL> if num < <NUM_LIT:0> or num > <NUM_LIT:8> : <EOL> return False <EOL> new_number = str ( num ) + new_number [ <NUM_LIT:1> : ] <EOL> new_number = new_number [ : <NUM_LIT:8> ] + '<STR_LIT:0>' <EOL> if not new_number . isdigit ( ) : <EOL> return False <EOL> for i in range ( len ( new_number ) ) : <EOL> fragment = int ( new_number [ i ] ) * parameter <EOL> calculated_checksum += fragment <EOL> if parameter == <NUM_LIT:1> : <EOL> parameter = <NUM_LIT:7> <EOL> elif parameter == <NUM_LIT:3> : <EOL> parameter = <NUM_LIT:1> <EOL> elif parameter == <NUM_LIT:7> : <EOL> parameter = <NUM_LIT:3> <EOL> return str ( calculated_checksum ) [ - <NUM_LIT:1> ] == given_checksum <EOL> def clean ( self , value ) : <EOL> super ( CHIdentityCardNumberField , self ) . clean ( value ) <EOL> if value in EMPTY_VALUES : <EOL> return u'<STR_LIT>' <EOL> match = re . match ( id_re , value ) <EOL> if not match : <EOL> raise ValidationError ( self . error_messages [ '<STR_LIT>' ] ) <EOL> idnumber , pos9 , checksum = match . groupdict ( ) [ '<STR_LIT>' ] , match . groupdict ( ) [ '<STR_LIT>' ] , match . groupdict ( ) [ '<STR_LIT>' ] <EOL> if idnumber == '<STR_LIT>' or idnumber == '<STR_LIT>' : <EOL> raise ValidationError ( self . error_messages [ '<STR_LIT>' ] ) <EOL> all_digits = "<STR_LIT>" % ( idnumber , pos9 , checksum ) <EOL> if not self . has_valid_checksum ( all_digits ) : <EOL> raise ValidationError ( self . error_messages [ '<STR_LIT>' ] ) <EOL> return u'<STR_LIT>' % ( idnumber , pos9 , checksum ) </s>
