app/django/contrib/localflavor/es/forms.py
changeset 323 ff1a9aa48cfd
parent 54 03e267d67478
--- a/app/django/contrib/localflavor/es/forms.py	Tue Oct 14 12:36:55 2008 +0000
+++ b/app/django/contrib/localflavor/es/forms.py	Tue Oct 14 16:00:59 2008 +0000
@@ -3,9 +3,9 @@
 Spanish-specific Form helpers
 """
 
-from django.newforms import ValidationError
-from django.newforms.fields import RegexField, Select, EMPTY_VALUES
-from django.utils.translation import ugettext as _
+from django.forms import ValidationError
+from django.forms.fields import RegexField, Select, EMPTY_VALUES
+from django.utils.translation import ugettext_lazy as _
 import re
 
 class ESPostalCodeField(RegexField):
@@ -76,8 +76,8 @@
         self.cif_control = 'JABCDEFGHI'
         self.cif_types = 'ABCDEFGHKLMNPQS'
         self.nie_types = 'XT'
-        super(ESIdentityCardNumberField, self).__init__(r'^([%s]?)[ -]?(\d+)[ -]?([%s]?)$' % (self.cif_types + self.nie_types + self.cif_types.lower() + self.nie_types.lower(), self.nif_control + self.nif_control.lower()),
-                max_length=None, min_length=None,
+        id_card_re = re.compile(r'^([%s]?)[ -]?(\d+)[ -]?([%s]?)$' % (self.cif_types + self.nie_types, self.nif_control + self.cif_control), re.IGNORECASE)
+        super(ESIdentityCardNumberField, self).__init__(id_card_re, max_length=None, min_length=None,
                 error_message=self.default_error_messages['invalid%s' % (self.only_nif and '_only_nif' or '')],
                 *args, **kwargs)
 
@@ -88,7 +88,7 @@
         nif_get_checksum = lambda d: self.nif_control[int(d)%23]
 
         value = value.upper().replace(' ', '').replace('-', '')
-        m = re.match(r'^([%s]?)[ -]?(\d+)[ -]?([%s]?)$' % (self.cif_types + self.nie_types, self.nif_control), value)
+        m = re.match(r'^([%s]?)[ -]?(\d+)[ -]?([%s]?)$' % (self.cif_types + self.nie_types, self.nif_control + self.cif_control), value)
         letter1, number, letter2 = m.groups()
 
         if not letter1 and letter2:
@@ -108,7 +108,7 @@
             if not letter2:
                 number, letter2 = number[:-1], int(number[-1])
             checksum = cif_get_checksum(number)
-            if letter2 in [checksum, self.cif_control[checksum]]:
+            if letter2 in (checksum, self.cif_control[checksum]):
                 return value
             else:
                 raise ValidationError, self.error_messages['invalid_cif']
@@ -180,5 +180,5 @@
 def cif_get_checksum(number):
     s1 = sum([int(digit) for pos, digit in enumerate(number) if int(pos) % 2])
     s2 = sum([sum([int(unit) for unit in str(int(digit) * 2)]) for pos, digit in enumerate(number) if not int(pos) % 2])
-    return 10 - ((s1 + s2) % 10)
+    return (10 - ((s1 + s2) % 10)) % 10