<s> """<STR_LIT>""" <EOL> from __future__ import unicode_literals <EOL> import re <EOL> from datetime import date <EOL> from django . core . validators import EMPTY_VALUES <EOL> from django . forms import ValidationError <EOL> from django . forms . fields import Field <EOL> from django . utils . translation import gettext_lazy as _ <EOL> id_re = re . compile ( r'<STR_LIT>' ) <EOL> class KWCivilIDNumberField ( Field ) : <EOL> """<STR_LIT>""" <EOL> default_error_messages = { <EOL> '<STR_LIT>' : _ ( '<STR_LIT>' ) , <EOL> } <EOL> def has_valid_checksum ( self , value ) : <EOL> weight = ( <NUM_LIT:2> , <NUM_LIT:1> , <NUM_LIT:6> , <NUM_LIT:3> , <NUM_LIT:7> , <NUM_LIT:9> , <NUM_LIT:10> , <NUM_LIT:5> , <NUM_LIT:8> , <NUM_LIT:4> , <NUM_LIT:2> ) <EOL> calculated_checksum = <NUM_LIT:0> <EOL> for i in range ( <NUM_LIT:11> ) : <EOL> calculated_checksum += int ( value [ i ] ) * weight [ i ] <EOL> remainder = calculated_checksum % <NUM_LIT:11> <EOL> checkdigit = <NUM_LIT:11> - remainder <EOL> if checkdigit != int ( value [ <NUM_LIT:11> ] ) : <EOL> return False <EOL> return True <EOL> def clean ( self , value ) : <EOL> super ( KWCivilIDNumberField , self ) . clean ( value ) <EOL> if value in EMPTY_VALUES : <EOL> return '<STR_LIT>' <EOL> if not re . match ( r'<STR_LIT>' , value ) : <EOL> raise ValidationError ( self . error_messages [ '<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> try : <EOL> date ( int ( gd [ '<STR_LIT>' ] ) , int ( gd [ '<STR_LIT>' ] ) , int ( gd [ '<STR_LIT>' ] ) ) <EOL> except ValueError : <EOL> raise ValidationError ( self . error_messages [ '<STR_LIT>' ] ) <EOL> if not self . has_valid_checksum ( value ) : <EOL> raise ValidationError ( self . error_messages [ '<STR_LIT>' ] ) <EOL> return value </s>
