Ticket #14563: tr-localflavor.patch

File tr-localflavor.patch, 5.9 KB (added by serkank, 14 years ago)

Patch for adding Turkish localflavor.

  • \contrib\localflavor\tr\forms.py

    diff -Naru --exclude=*.pyc django-orig\contrib\localflavor\tr\forms.py django\contrib\localflavor\tr\forms.py
    old new  
     1"""
     2TR-specific Form helpers
     3"""
     4
     5from django.core.validators import EMPTY_VALUES
     6from django.forms import ValidationError
     7from django.forms.fields import Field, RegexField, Select, CharField
     8from django.utils.encoding import smart_unicode
     9from django.utils.translation import ugettext_lazy as _
     10import re
     11
     12phone_digits_re = re.compile(r'^(\+90|0)? ?(([1-9]\d{2})|\([1-9]\d{2}\)) ?([2-9]\d{2} ?\d{2} ?\d{2})$')
     13
     14class TRZipCodeField(RegexField):
     15    default_error_messages = {
     16        'invalid': _('Enter a zip code in the format XXXXX.'),
     17    }
     18
     19    def __init__(self, *args, **kwargs):
     20        super(TRZipCodeField, self).__init__(r'^\d{5}$',
     21            max_length=None, min_length=None, *args, **kwargs)
     22
     23class TRPhoneNumberField(CharField):
     24    default_error_messages = {
     25        'invalid': _('Phone numbers must be in 0XXX XXX XXXX format.'),
     26    }
     27
     28    def clean(self, value):
     29        super(TRPhoneNumberField, self).clean(value)
     30        if value in EMPTY_VALUES:
     31            return u''
     32        value = re.sub('(\(|\)|\s+)', '', smart_unicode(value))
     33        m = phone_digits_re.search(value)
     34        if m:
     35            return u'%s-%s-%s' % (m.group(1), m.group(2), m.group(3))
     36        raise ValidationError(self.error_messages['invalid'])
     37
     38class TRIdentificationNumberField(Field):
     39    """
     40    A Turkey Identification Number number.
     41        See: http://tr.wikipedia.org/wiki/T%C3%BCrkiye_Cumhuriyeti_Kimlik_Numaras%C4%B1
     42
     43    Checks the following rules to determine whether the number is valid:
     44
     45        * The number is 11-digits.
     46        * First digit is not 0.
     47        * Conforms to the following two formula:
     48          (sum(1st, 3rd, 5th, 7th, 9th)*7 - sum(2nd,4th,6th,8th)) % 10 = 10th digit
     49          sum(1st to 10th) % 10 = 11th digit
     50    """
     51    default_error_messages = {
     52        'invalid': _('Enter a valid Turkish Identification number.'),
     53        'not_11': _('Turkish Identification number must be 11 digits.'),
     54    }
     55
     56    def clean(self, value):
     57        super(TRIdentificationNumberField, self).clean(value)
     58        if value in EMPTY_VALUES:
     59            return u''
     60        if len(value) != 11:
     61            raise ValidationError(self.error_messages['not_11'])
     62        if int(value[0]) == 0:
     63            raise ValidationError(self.error_messages['invalid'])
     64        chksum = (sum([int(value[i]) for i in xrange(0,9,2)])*7-
     65                                          sum([int(value[i]) for i in xrange(1,9,2)])) % 10
     66        if chksum != int(value[9]) or \
     67           (sum([int(value[i]) for i in xrange(10)]) % 10) != int(value[10]):
     68            raise ValidationError(self.error_messages['invalid'])
     69        return u'%s' % (value)
     70
     71class TRProvinceSelect(Select):
     72    """
     73    A Select widget that uses a list of Turkey cities as its choices.
     74    """
     75    def __init__(self, attrs=None):
     76        from tr_provinces import PROVINCE_CHOICES
     77        super(TRProvinceSelect, self).__init__(attrs, choices=PROVINCE_CHOICES)
  • \contrib\localflavor\tr\tr_provinces.py

    diff -Naru --exclude=*.pyc django-orig\contrib\localflavor\tr\tr_provinces.py django\contrib\localflavor\tr\tr_provinces.py
    old new  
     1# -*- coding: utf-8 -*-
     2"""
     3This exists in this standalone file so that it's only imported into memory
     4when explicitly needed.
     5"""
     6
     7PROVINCE_CHOICES = (
     8    ('01', ('Adana')),
     9    ('02', ('Adıyaman')),
     10    ('03', ('Afyonkarahisar')),
     11    ('04', ('Ağrı')),
     12    ('68', ('Aksaray')),
     13    ('05', ('Amasya')),
     14    ('06', ('Ankara')),
     15    ('07', ('Antalya')),
     16    ('75', ('Ardahan')),
     17    ('08', ('Artvin')),
     18    ('09', ('Aydın')),
     19    ('10', ('Balıkesir')),
     20    ('74', ('Bartın')),
     21    ('72', ('Batman')),
     22    ('69', ('Bayburt')),
     23    ('11', ('Bilecik')),
     24    ('12', ('Bingöl')),
     25    ('13', ('Bitlis')),
     26    ('14', ('Bolu')),
     27    ('15', ('Burdur')),
     28    ('16', ('Bursa')),
     29    ('17', ('Çanakkale')),
     30    ('18', ('Çankırı')),
     31    ('19', ('Çorum')),
     32    ('20', ('Denizli')),
     33    ('21', ('Diyarbakır')),
     34    ('81', ('Düzce')),
     35    ('22', ('Edirne')),
     36    ('23', ('Elazığ')),
     37    ('24', ('Erzincan')),
     38    ('25', ('Erzurum')),
     39    ('26', ('Eskişehir')),
     40    ('27', ('Gaziantep')),
     41    ('28', ('Giresun')),
     42    ('29', ('Gümüşhane')),
     43    ('30', ('Hakkari')),
     44    ('31', ('Hatay')),
     45    ('76', ('Iğdır')),
     46    ('32', ('Isparta')),
     47    ('33', ('Mersin')),
     48    ('34', ('İstanbul')),
     49    ('35', ('İzmir')),
     50    ('78', ('Karabük')),
     51    ('36', ('Kars')),
     52    ('37', ('Kastamonu')),
     53    ('38', ('Kayseri')),
     54    ('39', ('Kırklareli')),
     55    ('40', ('Kırşehir')),
     56    ('41', ('Kocaeli')),
     57    ('42', ('Konya')),
     58    ('43', ('Kütahya')),
     59    ('44', ('Malatya')),
     60    ('45', ('Manisa')),
     61    ('46', ('Kahramanmaraş')),
     62    ('70', ('Karaman')),
     63    ('71', ('Kırıkkale')),
     64    ('79', ('Kilis')),
     65    ('47', ('Mardin')),
     66    ('48', ('Muğla')),
     67    ('49', ('Muş')),
     68    ('50', ('Nevşehir')),
     69    ('51', ('Niğde')),
     70    ('52', ('Ordu')),
     71    ('80', ('Osmaniye')),
     72    ('53', ('Rize')),
     73    ('54', ('Sakarya')),
     74    ('55', ('Samsun')),
     75    ('56', ('Siirt')),
     76    ('57', ('Sinop')),
     77    ('58', ('Sivas')),
     78    ('73', ('Şırnak')),
     79    ('59', ('Tekirdağ')),
     80    ('60', ('Tokat')),
     81    ('61', ('Trabzon')),
     82    ('62', ('Tunceli')),
     83    ('63', ('Şanlıurfa')),
     84    ('64', ('Uşak')),
     85    ('65', ('Van')),
     86    ('77', ('Yalova')),
     87    ('66', ('Yozgat')),
     88    ('67', ('Zonguldak')),
     89)
Back to Top