2007-08-11 18:04:19 +08:00
|
|
|
"""
|
|
|
|
India-specific Form helpers.
|
|
|
|
"""
|
|
|
|
|
2010-01-05 11:56:19 +08:00
|
|
|
from django.core.validators import EMPTY_VALUES
|
2008-07-19 09:22:26 +08:00
|
|
|
from django.forms import ValidationError
|
2010-01-05 11:56:19 +08:00
|
|
|
from django.forms.fields import Field, RegexField, Select
|
2007-08-11 18:04:19 +08:00
|
|
|
from django.utils.encoding import smart_unicode
|
|
|
|
from django.utils.translation import gettext
|
|
|
|
import re
|
|
|
|
|
|
|
|
|
|
|
|
class INZipCodeField(RegexField):
|
2007-12-17 16:05:27 +08:00
|
|
|
default_error_messages = {
|
|
|
|
'invalid': gettext(u'Enter a zip code in the format XXXXXXX.'),
|
|
|
|
}
|
|
|
|
|
2011-05-03 19:52:04 +08:00
|
|
|
def __init__(self, max_length=None, min_length=None, *args, **kwargs):
|
2007-08-11 18:04:19 +08:00
|
|
|
super(INZipCodeField, self).__init__(r'^\d{6}$',
|
2011-05-03 19:52:04 +08:00
|
|
|
max_length, min_length, *args, **kwargs)
|
2007-08-11 18:04:19 +08:00
|
|
|
|
|
|
|
class INStateField(Field):
|
|
|
|
"""
|
|
|
|
A form field that validates its input is a Indian state name or
|
|
|
|
abbreviation. It normalizes the input to the standard two-letter vehicle
|
|
|
|
registration abbreviation for the given state or union territory
|
|
|
|
"""
|
2007-12-17 16:05:27 +08:00
|
|
|
default_error_messages = {
|
|
|
|
'invalid': u'Enter a Indian state or territory.',
|
|
|
|
}
|
|
|
|
|
2007-08-11 18:04:19 +08:00
|
|
|
def clean(self, value):
|
|
|
|
from in_states import STATES_NORMALIZED
|
|
|
|
super(INStateField, self).clean(value)
|
|
|
|
if value in EMPTY_VALUES:
|
|
|
|
return u''
|
|
|
|
try:
|
|
|
|
value = value.strip().lower()
|
|
|
|
except AttributeError:
|
|
|
|
pass
|
|
|
|
else:
|
|
|
|
try:
|
|
|
|
return smart_unicode(STATES_NORMALIZED[value.strip().lower()])
|
|
|
|
except KeyError:
|
|
|
|
pass
|
2007-12-17 16:05:27 +08:00
|
|
|
raise ValidationError(self.error_messages['invalid'])
|
2007-08-11 18:04:19 +08:00
|
|
|
|
|
|
|
class INStateSelect(Select):
|
|
|
|
"""
|
|
|
|
A Select widget that uses a list of Indian states/territories as its
|
|
|
|
choices.
|
|
|
|
"""
|
|
|
|
def __init__(self, attrs=None):
|
|
|
|
from in_states import STATE_CHOICES
|
|
|
|
super(INStateSelect, self).__init__(attrs, choices=STATE_CHOICES)
|
|
|
|
|