# Path: src/librement/registration/utils.py
# def invent_username(first_name, last_name):
#     counter = itertools.count(random.randrange(1000000))
# 
#     base = slugify(u'%s%s' % (first_name, last_name))
# 
#     username = base or 'user'
# 
#     while User.objects.filter(username=username).exists():
#         # We must ensure that our username suggestions are under 30
#         # characters or we'll simply fail later when we try and create a
#         # User (hence the [:30]), but we also need space for the variable
#         # suffix (hence the [:25]) or we run the risk of entering into
#         # infinite loop trying the same combination again and again.
#         username = '%s%d' % (base[:25], counter.next())
#         username = username[:30]
# 
#     return username


from email_from_template import send_mail
from django import forms
from django.contrib.auth.models import User
from librement.profile.enums import AccountEnum
from librement.account.models import Email
from librement.profile.models import Profile
from .utils import invent_username

        return val

    def clean_password_confirm(self):
        password = self.cleaned_data.get('password', '')
        password_confirm = self.cleaned_data['password_confirm']

        if password != password_confirm:
            raise forms.ValidationError("Passwords do not match.")

        if len(password) < 8:
            raise forms.ValidationError(
                "Password must be at least 8 characters"
            )

        return password

    def clean_organisation(self):
        val = self.cleaned_data['organisation']

        account_type = self.cleaned_data.get('account_type')

        if account_type != AccountEnum.INDIVIDUAL and val == '':
            raise forms.ValidationError(
                "Required field for company/non-profit accounts"
            )

        return val

    def save(self):

# Path: src/librement/registration/utils.py
# def invent_username(first_name, last_name):
#     counter = itertools.count(random.randrange(1000000))
# 
#     base = slugify(u'%s%s' % (first_name, last_name))
# 
#     username = base or 'user'
# 
#     while User.objects.filter(username=username).exists():
#         # We must ensure that our username suggestions are under 30
#         # characters or we'll simply fail later when we try and create a
#         # User (hence the [:30]), but we also need space for the variable
#         # suffix (hence the [:25]) or we run the risk of entering into
#         # infinite loop trying the same combination again and again.
#         username = '%s%d' % (base[:25], counter.next())
#         username = username[:30]
# 
#     return username


# Path: src/librement/registration/forms.py
from email_from_template import send_mail
from django import forms
from django.contrib.auth.models import User
from librement.profile.enums import AccountEnum
from librement.account.models import Email
from librement.profile.models import Profile
from .utils import invent_username


        return val

    def clean_password_confirm(self):
        password = self.cleaned_data.get('password', '')
        password_confirm = self.cleaned_data['password_confirm']

        if password != password_confirm:
            raise forms.ValidationError("Passwords do not match.")

        if len(password) < 8:
            raise forms.ValidationError(
                "Password must be at least 8 characters"
            )

        return password

    def clean_organisation(self):
        val = self.cleaned_data['organisation']

        account_type = self.cleaned_data.get('account_type')

        if account_type != AccountEnum.INDIVIDUAL and val == '':
            raise forms.ValidationError(
                "Required field for company/non-profit accounts"
            )

        return val

    def save(self):

        username=invent_username(