PageRenderTime 11ms CodeModel.GetById 1ms RepoModel.GetById 0ms app.codeStats 0ms

/django/contrib/localflavor/tr/forms.py

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