|
@@ -3,7 +3,6 @@ from __future__ import unicode_literals
|
|
|
|
|
|
import ldapdb.models
|
|
|
import unicodedata
|
|
|
-import string
|
|
|
import datetime
|
|
|
from django.db import models
|
|
|
from django.db.models import Q
|
|
@@ -12,6 +11,7 @@ from django.dispatch import receiver
|
|
|
from django.contrib.auth.models import AbstractUser
|
|
|
from django.conf import settings
|
|
|
from django.core.validators import RegexValidator
|
|
|
+from django.core.exceptions import ValidationError
|
|
|
from ldapdb.models.fields import CharField, IntegerField, ListField
|
|
|
|
|
|
from coin.offers.models import OfferSubscription
|
|
@@ -35,10 +35,13 @@ class Member(CoinLdapSyncMixin, AbstractUser):
|
|
|
)
|
|
|
|
|
|
status = models.CharField(max_length=50, choices=MEMBER_STATUS_CHOICES,
|
|
|
- default='pending', verbose_name='statut')
|
|
|
+ default='member', verbose_name='statut')
|
|
|
type = models.CharField(max_length=20, choices=MEMBER_TYPE_CHOICES,
|
|
|
default='natural_person', verbose_name='type')
|
|
|
|
|
|
+ nickname = models.CharField(max_length=64, blank=True,
|
|
|
+ verbose_name="nom d'usage",
|
|
|
+ help_text='Pseudonyme, …')
|
|
|
organization_name = models.CharField(max_length=200, blank=True,
|
|
|
verbose_name="nom de l'organisme",
|
|
|
help_text='Pour une personne morale')
|
|
@@ -75,14 +78,26 @@ class Member(CoinLdapSyncMixin, AbstractUser):
|
|
|
# passwords for both ldap and local db
|
|
|
_password_ldap = None
|
|
|
|
|
|
+ def clean(self):
|
|
|
+ if self.type == 'legal_entity':
|
|
|
+ if not self.organization_name:
|
|
|
+ raise ValidationError("Le nom de l'organisme est obligatoire "
|
|
|
+ "pour une personne morale")
|
|
|
+ elif self.type == 'natural_person':
|
|
|
+ if not (self.first_name and self.last_name):
|
|
|
+ raise ValidationError("Le nom et prénom sont obligatoires "
|
|
|
+ "pour une personne physique")
|
|
|
+
|
|
|
def __unicode__(self):
|
|
|
- name = self.first_name + ' ' + self.last_name
|
|
|
- if self.organization_name:
|
|
|
- name += ' (%s)' % self.organization_name
|
|
|
- return name
|
|
|
+ if self.type == 'legal_entity':
|
|
|
+ return self.organization_name
|
|
|
+ elif self.nickname:
|
|
|
+ return self.nickname
|
|
|
+ else:
|
|
|
+ return self.first_name + ' ' + self.last_name
|
|
|
|
|
|
def get_full_name(self):
|
|
|
- return '%s %s' % (self.first_name, self.last_name)
|
|
|
+ return str(self)
|
|
|
|
|
|
def get_short_name(self):
|
|
|
return self.username
|
|
@@ -172,8 +187,12 @@ class Member(CoinLdapSyncMixin, AbstractUser):
|
|
|
ldap_user.nick_name = self.username
|
|
|
ldap_user.uidNumber = uid_number
|
|
|
|
|
|
- ldap_user.last_name = self.last_name
|
|
|
- ldap_user.first_name = self.first_name
|
|
|
+ if self.type == 'natural_person':
|
|
|
+ ldap_user.last_name = self.last_name
|
|
|
+ ldap_user.first_name = self.first_name
|
|
|
+ elif self.type == 'legal_entity':
|
|
|
+ ldap_user.last_name = self.organization_name
|
|
|
+ ldap_user.first_name = ""
|
|
|
|
|
|
# If a password is definied in _password_ldap, change it in LDAP
|
|
|
if self._password_ldap:
|
|
@@ -216,37 +235,44 @@ class Member(CoinLdapSyncMixin, AbstractUser):
|
|
|
class Meta:
|
|
|
verbose_name = 'membre'
|
|
|
|
|
|
-# Hack to force email, first_name ans last_name to be required by Member model
|
|
|
+# Hack to force email to be required by Member model
|
|
|
Member._meta.get_field('email')._unique = True
|
|
|
Member._meta.get_field('email').blank = False
|
|
|
Member._meta.get_field('email').null = False
|
|
|
-Member._meta.get_field('first_name').blank = False
|
|
|
-Member._meta.get_field('first_name').null = False
|
|
|
-Member._meta.get_field('last_name').blank = False
|
|
|
-Member._meta.get_field('last_name').null = False
|
|
|
|
|
|
|
|
|
def count_active_members():
|
|
|
return Member.objects.filter(status='member').count()
|
|
|
|
|
|
|
|
|
-def get_automatic_username(first_name, last_name):
|
|
|
+def get_automatic_username(member):
|
|
|
"""
|
|
|
Calcul le username automatiquement en fonction
|
|
|
du nom et du prénom
|
|
|
"""
|
|
|
|
|
|
- # Première lettre de chaque partie du prénom
|
|
|
- first_name_letters = ''.join(
|
|
|
- [c[0] for c in first_name.split('-')]
|
|
|
- )
|
|
|
- # Concaténer avec nom de famille
|
|
|
- username = ('%s%s' % (first_name_letters, last_name))
|
|
|
+ # S'il s'agit d'une entreprise, utilise son nom:
|
|
|
+ if member.type == 'legal_entity' and member.organization_name:
|
|
|
+ username = member.organization_name
|
|
|
+ # Sinon, si un pseudo est définit, l'utilise
|
|
|
+ elif member.nickname:
|
|
|
+ username = member.nickname
|
|
|
+ # Sinon, utilise nom et prenom
|
|
|
+ elif member.first_name and member.last_name:
|
|
|
+ # Première lettre de chaque partie du prénom
|
|
|
+ first_name_letters = ''.join(
|
|
|
+ [c[0] for c in member.first_name.split('-')]
|
|
|
+ )
|
|
|
+ # Concaténer avec nom de famille
|
|
|
+ username = ('%s%s' % (first_name_letters, member.last_name))
|
|
|
+ else:
|
|
|
+ raise Exception('Il n\'y a pas sufissement d\'informations pour déterminer un login automatiquement')
|
|
|
+
|
|
|
# Remplacer ou enlever les caractères non ascii
|
|
|
username = unicodedata.normalize('NFD', username)\
|
|
|
.encode('ascii', 'ignore')
|
|
|
- # Enlever ponctuation et espace
|
|
|
- punctuation = (string.punctuation + ' ').encode('ascii')
|
|
|
+ # Enlever ponctuation (sauf _-.) et espace
|
|
|
+ punctuation = ('!"#$%&\'()*+,/:;<=>?@[\\]^`{|}~ ').encode('ascii')
|
|
|
username = username.translate(None, punctuation)
|
|
|
# En minuscule
|
|
|
username = username.lower()
|
|
@@ -257,7 +283,7 @@ def get_automatic_username(first_name, last_name):
|
|
|
member = Member.objects.filter(username=username)
|
|
|
base_username = username
|
|
|
incr = 2
|
|
|
- # Tant qu'un membre est trouvé, incrément un entier à la fin
|
|
|
+ # Tant qu'un membre est trouvé, incrémente un entier à la fin
|
|
|
while member:
|
|
|
if len(base_username) >= 30:
|
|
|
username = base_username[30 - len(str(incr)):]
|
|
@@ -379,8 +405,7 @@ def define_username(sender, instance, **kwargs):
|
|
|
le calcul automatiquement en fonction du nom et du prénom
|
|
|
"""
|
|
|
if not instance.username and not instance.pk:
|
|
|
- instance.username = get_automatic_username(instance.first_name,
|
|
|
- instance.last_name)
|
|
|
+ instance.username = get_automatic_username(instance)
|
|
|
|
|
|
|
|
|
@receiver(pre_save, sender=LdapUser)
|