Merge remote-tracking branch 'mainRepo/master' into feature/VAT_number
This commit is contained in:
commit
c9de757bc7
19 changed files with 521 additions and 45 deletions
|
|
@ -1,5 +1,8 @@
|
|||
from datetime import datetime
|
||||
import logging
|
||||
import random
|
||||
import unicodedata
|
||||
|
||||
from datetime import datetime
|
||||
from django.conf import settings
|
||||
from django.contrib.auth.hashers import make_password
|
||||
from django.contrib.auth.models import AbstractBaseUser, BaseUserManager, \
|
||||
|
|
@ -7,13 +10,17 @@ from django.contrib.auth.models import AbstractBaseUser, BaseUserManager, \
|
|||
from django.contrib.sites.models import Site
|
||||
from django.core.urlresolvers import reverse
|
||||
from django.core.validators import RegexValidator
|
||||
from django.db import models
|
||||
from django.db import models, IntegrityError
|
||||
from django.utils.crypto import get_random_string
|
||||
from django.utils.translation import ugettext_lazy as _
|
||||
from django.core.exceptions import ValidationError
|
||||
|
||||
from utils.mailer import BaseEmail
|
||||
from utils.mailer import DigitalGlarusRegistrationMailer
|
||||
from utils.stripe_utils import StripeUtils
|
||||
from utils.ldap_manager import LdapManager
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
REGISTRATION_MESSAGE = {'subject': "Validation mail",
|
||||
'message': 'Please validate Your account under this link '
|
||||
|
|
@ -42,6 +49,7 @@ class MyUserManager(BaseUserManager):
|
|||
user.is_admin = False
|
||||
user.set_password(password)
|
||||
user.save(using=self._db)
|
||||
user.create_ldap_account(password)
|
||||
return user
|
||||
|
||||
def create_superuser(self, email, name, password):
|
||||
|
|
@ -63,14 +71,56 @@ def get_validation_slug():
|
|||
return make_password(None)
|
||||
|
||||
|
||||
def get_first_and_last_name(full_name):
|
||||
first_name, *last_name = full_name.split(" ")
|
||||
last_name = " ".join(last_name)
|
||||
return first_name, last_name
|
||||
|
||||
|
||||
def assign_username(user):
|
||||
if not user.username:
|
||||
ldap_manager = LdapManager()
|
||||
|
||||
# Try to come up with a username
|
||||
first_name, last_name = get_first_and_last_name(user.name)
|
||||
user.username = unicodedata.normalize('NFKD', first_name + last_name)
|
||||
user.username = "".join([char for char in user.username if char.isalnum()]).lower()
|
||||
exist = True
|
||||
while exist:
|
||||
# Check if it exists
|
||||
exist, entries = ldap_manager.check_user_exists(user.username)
|
||||
if exist:
|
||||
# If username exists in ldap, come up with a new user name and check it again
|
||||
user.username = user.username + str(random.randint(0, 2 ** 10))
|
||||
else:
|
||||
# If username does not exists in ldap, try to save it in database
|
||||
try:
|
||||
user.save()
|
||||
except IntegrityError:
|
||||
# If username exists in database then come up with a new username
|
||||
user.username = user.username + str(random.randint(0, 2 ** 10))
|
||||
exist = True
|
||||
|
||||
|
||||
def validate_name(value):
|
||||
valid_chars = [char for char in value if (char.isalpha() or char == "-" or char == " ")]
|
||||
if len(valid_chars) < len(value):
|
||||
raise ValidationError(
|
||||
_('%(value)s is not a valid name. A valid name can only include letters, spaces or -'),
|
||||
params={'value': value},
|
||||
)
|
||||
|
||||
|
||||
class CustomUser(AbstractBaseUser, PermissionsMixin):
|
||||
VALIDATED_CHOICES = ((0, 'Not validated'), (1, 'Validated'))
|
||||
site = models.ForeignKey(Site, default=1)
|
||||
name = models.CharField(max_length=50)
|
||||
name = models.CharField(max_length=50, validators=[validate_name])
|
||||
email = models.EmailField(unique=True)
|
||||
username = models.CharField(max_length=60, unique=True, null=True)
|
||||
vat_number = models.CharField(max_length=100, default="")
|
||||
|
||||
validated = models.IntegerField(choices=VALIDATED_CHOICES, default=0)
|
||||
in_ldap = models.BooleanField(default=False)
|
||||
# By default, we initialize the validation_slug with appropriate value
|
||||
# This is required for User(page) admin
|
||||
validation_slug = models.CharField(
|
||||
|
|
@ -165,6 +215,29 @@ class CustomUser(AbstractBaseUser, PermissionsMixin):
|
|||
# The user is identified by their email address
|
||||
return self.email
|
||||
|
||||
def create_ldap_account(self, password):
|
||||
# create ldap account for user if it does not exists already.
|
||||
if self.in_ldap:
|
||||
return
|
||||
|
||||
assign_username(self)
|
||||
ldap_manager = LdapManager()
|
||||
try:
|
||||
user_exists_in_ldap, entries = ldap_manager.check_user_exists(self.username)
|
||||
except Exception:
|
||||
logger.exception("Exception occur while searching for user in LDAP")
|
||||
else:
|
||||
if not user_exists_in_ldap:
|
||||
# IF no ldap account
|
||||
first_name, last_name = get_first_and_last_name(self.name)
|
||||
if not last_name:
|
||||
last_name = first_name
|
||||
ldap_manager.create_user(self.username, password=password,
|
||||
firstname=first_name, lastname=last_name,
|
||||
email=self.email)
|
||||
self.in_ldap = True
|
||||
self.save()
|
||||
|
||||
def __str__(self): # __unicode__ on Python 2
|
||||
return self.email
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue