2007-03-26 05:10:36 +08:00
|
|
|
"""
|
|
|
|
FR-specific Form helpers
|
|
|
|
"""
|
|
|
|
|
|
|
|
from django.newforms import ValidationError
|
|
|
|
from django.newforms.fields import Field, RegexField, Select, EMPTY_VALUES
|
2007-04-04 14:34:19 +08:00
|
|
|
from django.utils.encoding import smart_unicode
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
from django.utils.translation import ugettext
|
2007-03-26 05:10:36 +08:00
|
|
|
import re
|
|
|
|
|
|
|
|
phone_digits_re = re.compile(r'^0\d(\s|\.)?(\d{2}(\s|\.)?){3}\d{2}$')
|
|
|
|
|
|
|
|
class FRZipCodeField(RegexField):
|
2007-12-17 16:05:27 +08:00
|
|
|
default_error_messages = {
|
|
|
|
'invalid': ugettext('Enter a zip code in the format XXXXX.'),
|
|
|
|
}
|
|
|
|
|
2007-03-26 05:10:36 +08:00
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
super(FRZipCodeField, self).__init__(r'^\d{5}$',
|
2007-12-17 16:05:27 +08:00
|
|
|
max_length=None, min_length=None, *args, **kwargs)
|
2007-03-26 05:10:36 +08:00
|
|
|
|
|
|
|
class FRPhoneNumberField(Field):
|
|
|
|
"""
|
|
|
|
Validate local French phone number (not international ones)
|
|
|
|
The correct format is '0X XX XX XX XX'.
|
|
|
|
'0X.XX.XX.XX.XX' and '0XXXXXXXXX' validate but are corrected to
|
|
|
|
'0X XX XX XX XX'.
|
|
|
|
"""
|
2007-12-17 16:05:27 +08:00
|
|
|
default_error_messages = {
|
|
|
|
'invalid': u'Phone numbers must be in 0X XX XX XX XX format.',
|
|
|
|
}
|
|
|
|
|
2007-03-26 05:10:36 +08:00
|
|
|
def clean(self, value):
|
|
|
|
super(FRPhoneNumberField, self).clean(value)
|
|
|
|
if value in EMPTY_VALUES:
|
|
|
|
return u''
|
|
|
|
value = re.sub('(\.|\s)', '', smart_unicode(value))
|
|
|
|
m = phone_digits_re.search(value)
|
|
|
|
if m:
|
|
|
|
return u'%s %s %s %s %s' % (value[0:2], value[2:4], value[4:6], value[6:8], value[8:10])
|
2007-12-17 16:05:27 +08:00
|
|
|
raise ValidationError(self.error_messages['invalid'])
|
2007-03-26 05:10:36 +08:00
|
|
|
|
|
|
|
class FRDepartmentSelect(Select):
|
|
|
|
"""
|
|
|
|
A Select widget that uses a list of FR departments as its choices.
|
|
|
|
"""
|
|
|
|
def __init__(self, attrs=None):
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
from fr_department import DEPARTMENT_ASCII_CHOICES
|
2007-03-26 05:10:36 +08:00
|
|
|
super(FRDepartmentSelect, self).__init__(attrs, choices=DEPARTMENT_ASCII_CHOICES)
|
|
|
|
|