<s> """<STR_LIT>""" <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 . translation import ugettext_lazy as _ <EOL> import re <EOL> id_re = re . compile ( r"<STR_LIT>" ) <EOL> class DEZipCodeField ( RegexField ) : <EOL> default_error_messages = { <EOL> '<STR_LIT>' : _ ( '<STR_LIT>' ) , <EOL> } <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> super ( DEZipCodeField , self ) . __init__ ( r'<STR_LIT>' , <EOL> max_length = None , min_length = None , * args , ** kwargs ) <EOL> class DEStateSelect ( Select ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , attrs = None ) : <EOL> from de_states import STATE_CHOICES <EOL> super ( DEStateSelect , self ) . __init__ ( attrs , choices = STATE_CHOICES ) <EOL> class DEIdentityCardNumberField ( 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> calculated_checksum = <NUM_LIT:0> <EOL> fragment = "<STR_LIT>" <EOL> parameter = <NUM_LIT:7> <EOL> for i in range ( len ( given_number ) ) : <EOL> fragment = str ( int ( given_number [ i ] ) * parameter ) <EOL> if fragment . isalnum ( ) : <EOL> calculated_checksum += int ( fragment [ - <NUM_LIT:1> ] ) <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 ( DEIdentityCardNumberField , 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> gd = match . groupdict ( ) <EOL> residence , origin = gd [ '<STR_LIT>' ] , gd [ '<STR_LIT>' ] <EOL> birthday , validity , checksum = gd [ '<STR_LIT>' ] , gd [ '<STR_LIT>' ] , gd [ '<STR_LIT>' ] <EOL> if residence == '<STR_LIT>' or birthday == '<STR_LIT>' or validity == '<STR_LIT>' : <EOL> raise ValidationError ( self . error_messages [ '<STR_LIT>' ] ) <EOL> all_digits = u"<STR_LIT>" % ( residence , birthday , validity , checksum ) <EOL> if not self . has_valid_checksum ( residence ) or not self . has_valid_checksum ( birthday ) or not self . has_valid_checksum ( validity ) or not self . has_valid_checksum ( all_digits ) : <EOL> raise ValidationError ( self . error_messages [ '<STR_LIT>' ] ) <EOL> return u'<STR_LIT>' % ( residence , origin , birthday , validity , checksum ) </s>
