[Oi-commits] trunk/piyango - fix bug

Uğur Çetin oi-svn at pardus.org.tr
Fri Jan 29 15:49:25 EET 2010


Author: jnmbk
Date: Fri Jan 29 15:49:25 2010
New Revision: 2911

Modified:
   trunk/piyango/forms.py
Log:
fix bug

---
 forms.py |   47 ++++++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 46 insertions(+), 1 deletion(-)

Modified: trunk/piyango/forms.py
=================================================================
--- trunk/piyango/forms.py	(original)
+++ trunk/piyango/forms.py	Fri Jan 29 15:49:25 2010
@@ -10,7 +10,52 @@
 from django import forms
 
 from oi.piyango.models import Person
-from oi.shipit.forms import TurkishIdentityNumberField
+
+class TurkishIdentityNumberField(forms.Field):
+    """
+    A Republic of Turkey Identity number.
+
+    Checks the following rules to determine whether the number is valid:
+
+        * Last 2 characters of ID should validate the following algorithm.
+    """
+    default_error_messages = {
+        'invalid': 'Lütfen geçerli bir TC kimlik numarası girin.',
+        'duplicate': 'Bu TC kimlik numarası daha önce kullanılmış.',
+    }
+
+    def clean(self, value):
+        super(TurkishIdentityNumberField, self).clean(value)
+        if not value:
+            return u''
+
+        #check for duplicates
+        if Person.objects.filter(tcidentity=value, confirmed=True).count() > 0:
+            raise forms.ValidationError(self.error_messages['duplicate'])
+
+        #check number integrity
+        match = re.match(re.compile(r"^\d{11}$"), value)
+        if not match:
+            raise forms.ValidationError(self.error_messages['invalid'])
+
+        odds, evens = 0, 0
+        for i in range(9):
+            if i % 2:
+                evens += int(value[i])
+            else:
+                odds += int(value[i])
+
+        t1 = odds * 3 + evens
+        c1 = (10 - t1 % 10) % 10
+        t2 = c1 + evens
+        t3 = t2 * 3 + odds
+        c2 = (10 - t3 % 10) % 10
+
+        if c1 != int(value[9]) or c2 != int(value[10]):
+            raise forms.ValidationError(self.error_messages['invalid'])
+
+        return value
+
 
 class PersonForm(forms.ModelForm):
     tcidentity = TurkishIdentityNumberField(label="TC Kimlik No")


More information about the Oi-commits mailing list