2007-03-26 05:10:36 +08:00
|
|
|
"""
|
|
|
|
FR-specific Form helpers
|
|
|
|
"""
|
2012-06-08 00:08:47 +08:00
|
|
|
from __future__ import absolute_import, unicode_literals
|
2007-03-26 05:10:36 +08:00
|
|
|
|
2011-10-18 08:47:49 +08:00
|
|
|
import re
|
|
|
|
|
2012-02-10 02:58:03 +08:00
|
|
|
from django.contrib.localflavor.fr.fr_department import DEPARTMENT_CHOICES
|
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
|
2012-06-30 19:18:07 +08:00
|
|
|
from django.forms.fields import CharField, RegexField, Select
|
2012-07-21 16:00:10 +08:00
|
|
|
from django.utils.encoding import smart_text
|
2008-06-18 21:10:05 +08:00
|
|
|
from django.utils.translation import ugettext_lazy as _
|
2011-10-18 08:47:49 +08:00
|
|
|
|
2007-03-26 05:10:36 +08:00
|
|
|
|
|
|
|
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 = {
|
2008-06-18 21:10:05 +08:00
|
|
|
'invalid': _('Enter a zip code in the format XXXXX.'),
|
2007-12-17 16:05:27 +08:00
|
|
|
}
|
|
|
|
|
2012-06-30 19:18:07 +08:00
|
|
|
def __init__(self, max_length=5, min_length=5, *args, **kwargs):
|
2007-03-26 05:10:36 +08:00
|
|
|
super(FRZipCodeField, self).__init__(r'^\d{5}$',
|
2011-05-03 19:52:04 +08:00
|
|
|
max_length, min_length, *args, **kwargs)
|
2007-03-26 05:10:36 +08:00
|
|
|
|
2012-06-30 19:18:07 +08:00
|
|
|
class FRPhoneNumberField(CharField):
|
2007-03-26 05:10:36 +08:00
|
|
|
"""
|
|
|
|
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 = {
|
2009-09-13 12:40:17 +08:00
|
|
|
'invalid': _('Phone numbers must be in 0X XX XX XX XX format.'),
|
2007-12-17 16:05:27 +08:00
|
|
|
}
|
|
|
|
|
2012-06-30 19:18:07 +08:00
|
|
|
def __init__(self, max_length=14, min_length=10, *args, **kwargs):
|
|
|
|
super(FRPhoneNumberField, self).__init__(
|
|
|
|
max_length, min_length, *args, **kwargs)
|
|
|
|
|
2007-03-26 05:10:36 +08:00
|
|
|
def clean(self, value):
|
|
|
|
super(FRPhoneNumberField, 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))
|
2007-03-26 05:10:36 +08:00
|
|
|
m = phone_digits_re.search(value)
|
|
|
|
if m:
|
2012-06-08 00:08:47 +08:00
|
|
|
return '%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):
|
2012-02-10 02:58:03 +08:00
|
|
|
super(FRDepartmentSelect, self).__init__(attrs, choices=DEPARTMENT_CHOICES)
|