models: allow customizing regexp and error message for phone and postal code user's fields

This commit is contained in:
Benjamin Dauvergne 2014-05-23 15:40:57 +02:00
parent 85fa01fd77
commit 419702be71
3 changed files with 24 additions and 11 deletions

View File

@ -1,3 +1,4 @@
from django.utils.translation import ugettext_lazy as _
import sys
class AppSettings(object):
@ -13,6 +14,13 @@ class AppSettings(object):
'allow_redirects': False,
},
'PROFILE_FORM_PLUGIN_FORM_CLASS': 'portail_citoyen.forms.ProfileFormPluginForm',
'POSTAL_CODE_REGEXP': r'^[0-9]*$',
'POSTAL_CODE_MESSAGE': _('Postal code must be five numbers'),
'HOME_PHONE_REGEXP': r'^0[1234589][0-9]{8}$',
'HOME_PHONE_MESSAGE': _('Phone number must start with 01, 02, 03, 04, 05, 08 or 07 and be ten digits long without spaces'),
'MOBILE_PHONE_REGEXP': r'^0[67][0-9]{8}$',
'MOBILE_PHONE_MESSAGE': _('Mobile phone number must start with 06 or 07 and be ten digits long without spaces'),
}
__prefix = 'PORTAIL_CITOYEN_'

View File

@ -7,6 +7,8 @@ from django.contrib.auth.models import Group
from authentic2.models import AbstractUser
from . import app_settings
class Citoyen(AbstractUser):
title = models.CharField(pgettext_lazy('person title', 'title'), max_length=16, blank=True,
@ -20,25 +22,23 @@ class Citoyen(AbstractUser):
address = models.CharField(_('address'), max_length=128, blank=True)
postal_code = models.CharField(_('postal code'), max_length=5, blank=True,
validators=[
RegexValidator(r'^[0-9]*$',
_('Postal code must be five numbers'),
_('Invalid postal code')),
MinLengthValidator(5),
MaxLengthValidator(5)])
RegexValidator(app_settings.POSTAL_CODE_REGEXP,
app_settings.POSTAL_CODE_MESSAGE,
'invalid postal code')])
phone = models.CharField(verbose_name=_("phone"),
max_length=16, blank=True,
help_text=_('Phone number must start with 01, 02, 03, 04, 05, 08 or 07 and be ten digits long without spaces'),
validators=[
RegexValidator(r'^0[1234589][0-9]{8}$',
_('Phone number must start with 01, 02, 03, 04, 05, 08 or 07 and be ten digits long without spaces'),
_('Invalid mobile phone number')),])
RegexValidator(app_settings.HOME_PHONE_REGEXP,
app_settings.HOME_PHONE_MESSAGE,
'invalid phone'),])
mobile = models.CharField(verbose_name=_("mobile"),
max_length=16, blank=True,
help_text=_('Mobile phone number must start with 06 or 07 and be ten digits long without spaces'),
validators=[
RegexValidator(r'^0[67][0-9]{8}$',
_('Mobile phone number must start with 06 or 07 and be ten digits long without spaces'),
_('Invalid mobile phone number')),])
RegexValidator(app_settings.MOBILE_PHONE_REGEXP,
app_settings.MOBILE_PHONE_MESSAGE,
'invalid phone'),])
REQUIRED_FIELDS = ['first_name', 'last_name', 'email']
USERNAME = 'username'

View File

@ -431,6 +431,11 @@ if 'USE_MEMCACHED' in os.environ:
}
SESSION_ENGINE = 'django.contrib.sessions.backends.cached_db'
for key in ('POSTAL_CODE_REGEXP', 'POSTAL_CODE_MESSAGE', 'HOME_PHONE_REGEXP', 'HOME_PHONE_MESSAGE', 'MOBILE_PHONE_REGEXP', 'MOBILE_PHONE_MESSAGE'):
key = 'PORTAIL_CITOYEN_' + key
if key in os.environ:
globals()[key] = unicode(os.environ[key], 'utf8')
# try to import local_settings.py (useless, in theory)
try:
from portail_citoyen.local_settings import *