This repository has been archived on 2023-02-21. You can view files and clone it, but cannot push or open issues or pull requests.
portail-citoyen/portail_citoyen/models.py

119 lines
4.8 KiB
Python

import string
from django.db import models
from django.core.validators import RegexValidator, MinLengthValidator, \
MaxLengthValidator
from django.utils.translation import ugettext_lazy as _, pgettext_lazy
from django.contrib.auth.models import Group
from django.core.exceptions import ValidationError
from authentic2.models import AbstractUser
def only_digits(value):
return u''.join(x for x in value if x in string.digits)
def validate_lun(value):
l = [(int(x)* (1+i%2)) for i, x in enumerate(reversed(value))]
return sum(x-9 if x > 10 else x for x in l) % 10 == 0
def validate_siret(value):
RegexValidator(r'^( *[0-9] *){14}$',
_('SIRET number must contain 14 digits'), 'coin')(value)
value = only_digits(value)
if value in ('9'*14, '8'*14, '7'*14, '6'*14):
return
if not validate_lun(value) or not validate_lun(value[:9]):
raise ValidationError(_('SIRET validation code does not match'))
class Citoyen(AbstractUser):
title = models.CharField(pgettext_lazy('person title', 'title'), max_length=16, blank=True,
choices=tuple((x, x) for x in (_('Mrs'),
_('Mr'))))
first_name = models.CharField(_('first name'), max_length=30, blank=True)
last_name = models.CharField(_('last name'), max_length=30, blank=True)
email = models.EmailField(_('e-mail address'), max_length=128, blank=True)
city = models.CharField(_('city'), max_length=64, blank=True)
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)])
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')),])
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')),])
enterprise_name = models.CharField(verbose_name=_('enterprise name'),
max_length=64, blank=True)
enterprise_siret = models.CharField(verbose_name=_('enterprise siret'),
max_length=20, blank=True,
validators=[validate_siret])
enterprise_id = models.CharField(verbose_name=_('enterprise internal id'),
max_length=64, blank=True)
def clean(self):
if self.enterprise_siret:
self.enterprise_siret = only_digits(self.enterprise_siret)
super(Citoyen, self).clean()
REQUIRED_FIELDS = ['first_name', 'last_name', 'email', 'enterprise_name', 'enterprise_siret']
USERNAME = 'username'
USER_PROFILE = ( 'title', 'username', 'first_name', 'last_name', 'email',
'phone', 'mobile', 'address', 'postal_code', 'city', 'roles',
'enterprise_name', 'enterprise_siret')
from authentic2.attribute_aggregator.core import ATTRIBUTE_MAPPING
ATTRIBUTE_MAPPING['l']['profile_field_name'] = 'city'
ATTRIBUTE_MAPPING['personalTitle']['profile_field_name'] = 'title'
ATTRIBUTE_MAPPING['street']['profile_field_name'] = 'address'
ATTRIBUTE_MAPPING['postalCode']['profile_field_name'] = 'postal_code'
ATTRIBUTE_MAPPING['mobile']['profile_field_name'] = 'mobile'
ATTRIBUTE_MAPPING.update({
"enterpriseName": {
"oid": "2.5.4.7",
"display_name": _("Enterprise Name"),
"profile_field_name": "enterprise_name",
"type": "http://www.w3.org/2001/XMLSchema#string",
},
"enterpriseSIRET": {
"oid": "2.5.4.7",
"display_name": _("Enterprise SIRET"),
"profile_field_name": "enterprise_siret",
"type": "http://www.w3.org/2001/XMLSchema#string",
},
"enterpriseId": {
"oid": "2.5.4.7",
"display_name": _("Enterprise ID"),
"profile_field_name": "enterprise_id",
"type": "http://www.w3.org/2001/XMLSchema#string",
},
})
class Role(Group):
class Meta:
proxy = True
verbose_name = _('role')
verbose_name_plural = _('roles')
import permission_hack