2010-12-04 13:25:24 +08:00
|
|
|
"""
|
|
|
|
TR-specific Form helpers
|
|
|
|
"""
|
|
|
|
|
2012-06-08 00:08:47 +08:00
|
|
|
from __future__ import absolute_import, unicode_literals
|
2011-10-18 08:47:49 +08:00
|
|
|
|
|
|
|
import re
|
|
|
|
|
|
|
|
from django.contrib.localflavor.tr.tr_provinces import PROVINCE_CHOICES
|
2010-12-04 13:25:24 +08:00
|
|
|
from django.core.validators import EMPTY_VALUES
|
|
|
|
from django.forms import ValidationError
|
|
|
|
from django.forms.fields import Field, RegexField, Select, CharField
|
2012-07-21 16:00:10 +08:00
|
|
|
from django.utils.encoding import smart_text
|
2010-12-04 13:25:24 +08:00
|
|
|
from django.utils.translation import ugettext_lazy as _
|
2011-10-18 08:47:49 +08:00
|
|
|
|
2010-12-04 13:25:24 +08:00
|
|
|
|
|
|
|
phone_digits_re = re.compile(r'^(\+90|0)? ?(([1-9]\d{2})|\([1-9]\d{2}\)) ?([2-9]\d{2} ?\d{2} ?\d{2})$')
|
|
|
|
|
|
|
|
class TRPostalCodeField(RegexField):
|
|
|
|
default_error_messages = {
|
2012-06-08 00:08:47 +08:00
|
|
|
'invalid': _('Enter a postal code in the format XXXXX.'),
|
2010-12-04 13:25:24 +08:00
|
|
|
}
|
|
|
|
|
2011-05-03 19:52:04 +08:00
|
|
|
def __init__(self, max_length=5, min_length=5, *args, **kwargs):
|
2010-12-04 13:25:24 +08:00
|
|
|
super(TRPostalCodeField, self).__init__(r'^\d{5}$',
|
2011-05-03 19:52:04 +08:00
|
|
|
max_length, min_length, *args, **kwargs)
|
2010-12-04 13:25:24 +08:00
|
|
|
|
|
|
|
def clean(self, value):
|
|
|
|
value = super(TRPostalCodeField, self).clean(value)
|
|
|
|
if value in EMPTY_VALUES:
|
2012-06-08 00:08:47 +08:00
|
|
|
return ''
|
2010-12-04 13:25:24 +08:00
|
|
|
if len(value) != 5:
|
|
|
|
raise ValidationError(self.error_messages['invalid'])
|
|
|
|
province_code = int(value[:2])
|
|
|
|
if province_code == 0 or province_code > 81:
|
|
|
|
raise ValidationError(self.error_messages['invalid'])
|
|
|
|
return value
|
|
|
|
|
|
|
|
|
|
|
|
class TRPhoneNumberField(CharField):
|
|
|
|
default_error_messages = {
|
2012-06-08 00:08:47 +08:00
|
|
|
'invalid': _('Phone numbers must be in 0XXX XXX XXXX format.'),
|
2010-12-04 13:25:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
def clean(self, value):
|
|
|
|
super(TRPhoneNumberField, self).clean(value)
|
|
|
|
if value in EMPTY_VALUES:
|
2012-06-08 00:08:47 +08:00
|
|
|
return ''
|
2012-07-21 16:00:10 +08:00
|
|
|
value = re.sub('(\(|\)|\s+)', '', smart_text(value))
|
2010-12-04 13:25:24 +08:00
|
|
|
m = phone_digits_re.search(value)
|
|
|
|
if m:
|
2012-06-08 00:08:47 +08:00
|
|
|
return '%s%s' % (m.group(2), m.group(4))
|
2010-12-04 13:25:24 +08:00
|
|
|
raise ValidationError(self.error_messages['invalid'])
|
|
|
|
|
|
|
|
class TRIdentificationNumberField(Field):
|
|
|
|
"""
|
|
|
|
A Turkey Identification Number number.
|
2011-01-03 21:19:59 +08:00
|
|
|
See: http://tr.wikipedia.org/wiki/T%C3%BCrkiye_Cumhuriyeti_Kimlik_Numaras%C4%B1
|
2010-12-04 13:25:24 +08:00
|
|
|
|
|
|
|
Checks the following rules to determine whether the number is valid:
|
|
|
|
|
|
|
|
* The number is 11-digits.
|
|
|
|
* First digit is not 0.
|
|
|
|
* Conforms to the following two formula:
|
|
|
|
(sum(1st, 3rd, 5th, 7th, 9th)*7 - sum(2nd,4th,6th,8th)) % 10 = 10th digit
|
|
|
|
sum(1st to 10th) % 10 = 11th digit
|
|
|
|
"""
|
|
|
|
default_error_messages = {
|
2012-06-08 00:08:47 +08:00
|
|
|
'invalid': _('Enter a valid Turkish Identification number.'),
|
|
|
|
'not_11': _('Turkish Identification number must be 11 digits.'),
|
2010-12-04 13:25:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
def clean(self, value):
|
|
|
|
super(TRIdentificationNumberField, self).clean(value)
|
|
|
|
if value in EMPTY_VALUES:
|
2012-06-08 00:08:47 +08:00
|
|
|
return ''
|
2010-12-04 13:25:24 +08:00
|
|
|
if len(value) != 11:
|
|
|
|
raise ValidationError(self.error_messages['not_11'])
|
|
|
|
if not re.match(r'^\d{11}$', value):
|
|
|
|
raise ValidationError(self.error_messages['invalid'])
|
|
|
|
if int(value[0]) == 0:
|
|
|
|
raise ValidationError(self.error_messages['invalid'])
|
2012-07-21 00:53:11 +08:00
|
|
|
chksum = (sum([int(value[i]) for i in range(0, 9, 2)]) * 7 -
|
|
|
|
sum([int(value[i]) for i in range(1, 9, 2)])) % 10
|
2010-12-04 13:25:24 +08:00
|
|
|
if chksum != int(value[9]) or \
|
2012-07-21 00:53:11 +08:00
|
|
|
(sum([int(value[i]) for i in range(10)]) % 10) != int(value[10]):
|
2010-12-04 13:25:24 +08:00
|
|
|
raise ValidationError(self.error_messages['invalid'])
|
|
|
|
return value
|
|
|
|
|
|
|
|
class TRProvinceSelect(Select):
|
|
|
|
"""
|
|
|
|
A Select widget that uses a list of provinces in Turkey as its choices.
|
|
|
|
"""
|
|
|
|
def __init__(self, attrs=None):
|
|
|
|
super(TRProvinceSelect, self).__init__(attrs, choices=PROVINCE_CHOICES)
|