<s> """<STR_LIT>""" <EOL> from __future__ import absolute_import <EOL> from django . contrib . localflavor . cl . cl_regions import REGION_CHOICES <EOL> from django . core . validators import EMPTY_VALUES <EOL> from django . forms import ValidationError <EOL> from django . forms . fields import RegexField , Select <EOL> from django . utils . translation import ugettext_lazy as _ <EOL> from django . utils . encoding import smart_unicode <EOL> class CLRegionSelect ( Select ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , attrs = None ) : <EOL> super ( CLRegionSelect , self ) . __init__ ( attrs , choices = REGION_CHOICES ) <EOL> class CLRutField ( RegexField ) : <EOL> """<STR_LIT>""" <EOL> default_error_messages = { <EOL> '<STR_LIT>' : _ ( '<STR_LIT>' ) , <EOL> '<STR_LIT:strict>' : _ ( '<STR_LIT>' ) , <EOL> '<STR_LIT>' : _ ( '<STR_LIT>' ) , <EOL> } <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> if '<STR_LIT:strict>' in kwargs : <EOL> del kwargs [ '<STR_LIT:strict>' ] <EOL> super ( CLRutField , self ) . __init__ ( r'<STR_LIT>' , <EOL> error_message = self . default_error_messages [ '<STR_LIT:strict>' ] , * args , ** kwargs ) <EOL> else : <EOL> super ( CLRutField , self ) . __init__ ( r'<STR_LIT>' , * args , ** kwargs ) <EOL> def clean ( self , value ) : <EOL> """<STR_LIT>""" <EOL> super ( CLRutField , self ) . clean ( value ) <EOL> if value in EMPTY_VALUES : <EOL> return u'<STR_LIT>' <EOL> rut , verificador = self . _canonify ( value ) <EOL> if self . _algorithm ( rut ) == verificador : <EOL> return self . _format ( rut , verificador ) <EOL> else : <EOL> raise ValidationError ( self . error_messages [ '<STR_LIT>' ] ) <EOL> def _algorithm ( self , rut ) : <EOL> """<STR_LIT>""" <EOL> suma = <NUM_LIT:0> <EOL> multi = <NUM_LIT:2> <EOL> for r in rut [ : : - <NUM_LIT:1> ] : <EOL> suma += int ( r ) * multi <EOL> multi += <NUM_LIT:1> <EOL> if multi == <NUM_LIT:8> : <EOL> multi = <NUM_LIT:2> <EOL> return u'<STR_LIT>' [ <NUM_LIT:11> - suma % <NUM_LIT:11> ] <EOL> def _canonify ( self , rut ) : <EOL> """<STR_LIT>""" <EOL> rut = smart_unicode ( rut ) . replace ( '<STR_LIT:U+0020>' , '<STR_LIT>' ) . replace ( '<STR_LIT:.>' , '<STR_LIT>' ) . replace ( '<STR_LIT:->' , '<STR_LIT>' ) <EOL> return rut [ : - <NUM_LIT:1> ] , rut [ - <NUM_LIT:1> ] . upper ( ) <EOL> def _format ( self , code , verifier = None ) : <EOL> """<STR_LIT>""" <EOL> if verifier is None : <EOL> verifier = code [ - <NUM_LIT:1> ] <EOL> code = code [ : - <NUM_LIT:1> ] <EOL> while len ( code ) > <NUM_LIT:3> and '<STR_LIT:.>' not in code [ : <NUM_LIT:3> ] : <EOL> pos = code . find ( '<STR_LIT:.>' ) <EOL> if pos == - <NUM_LIT:1> : <EOL> new_dot = - <NUM_LIT:3> <EOL> else : <EOL> new_dot = pos - <NUM_LIT:3> <EOL> code = code [ : new_dot ] + '<STR_LIT:.>' + code [ new_dot : ] <EOL> return u'<STR_LIT>' % ( code , verifier ) </s>
