# Path: src/librement/registration/forms.py
# class RegistrationForm(forms.ModelForm):
#     first_name = forms.CharField(max_length=30)
#     last_name = forms.CharField(max_length=30)
# 
#     email = forms.EmailField()
# 
#     password = forms.CharField(required=False)
#     password_confirm = forms.CharField(required=False)
# 
#     class Meta:
#         model = Profile
#         fields = (
#             'account_type',
#             'organisation',
#             'country',
#         )
# 
#     def clean_email(self):
#         val = self.cleaned_data['email']
# 
#         if Email.objects.filter(email=val).exists():
#             raise forms.ValidationError("Email address already in use.")
# 
#         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(
#             self.cleaned_data['first_name'],
#             self.cleaned_data['last_name'],
#         )
# 
#         user = User(
#             is_active=False,
#             first_name=self.cleaned_data['first_name'],
#             last_name=self.cleaned_data['last_name'],
#             username=username,
#         )
#         user.set_password(self.cleaned_data['password'])
#         user.save()
# 
#         # Store the user's email address; we don't use User.email as we support
#         # multiple email addresses.
#         email = user.emails.create(email=self.cleaned_data['email'])
# 
#         # Update Profile model rather than create a new one.
#         profile = super(RegistrationForm, self).save(commit=False)
#         profile.user = user
# 
#         if profile.account_type == AccountEnum.INDIVIDUAL:
#             profile.display_name = u"%s %s" % (user.first_name, user.last_name)
#         else:
#             profile.display_name = profile.organisation
# 
#         profile.save()
# 
#         # Send confirmation email
#         send_mail((email.email,), 'registration/confirm.email', {
#             'user': user,
#         })
# 
#         return user


from django.conf import settings
from django.shortcuts import render, redirect
from django.contrib.auth.decorators import login_required
from .forms import RegistrationForm
# Copyright 2012 The Librement Developers
#
# See the AUTHORS file at the top-level directory of this distribution
# and at http://librement.net/copyright/
#
# This file is part of Librement. It is subject to the license terms in
# the LICENSE file found in the top-level directory of this distribution
# and at http://librement.net/license/. No part of Librement, including
# this file, may be copied, modified, propagated, or distributed except
# according to the terms contained in the LICENSE file.



def view(request):
    if request.user.is_authenticated():
        return redirect(settings.LOGIN_REDIRECT_URL)

    if request.method == 'POST':

# Path: src/librement/registration/forms.py
# class RegistrationForm(forms.ModelForm):
#     first_name = forms.CharField(max_length=30)
#     last_name = forms.CharField(max_length=30)
# 
#     email = forms.EmailField()
# 
#     password = forms.CharField(required=False)
#     password_confirm = forms.CharField(required=False)
# 
#     class Meta:
#         model = Profile
#         fields = (
#             'account_type',
#             'organisation',
#             'country',
#         )
# 
#     def clean_email(self):
#         val = self.cleaned_data['email']
# 
#         if Email.objects.filter(email=val).exists():
#             raise forms.ValidationError("Email address already in use.")
# 
#         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(
#             self.cleaned_data['first_name'],
#             self.cleaned_data['last_name'],
#         )
# 
#         user = User(
#             is_active=False,
#             first_name=self.cleaned_data['first_name'],
#             last_name=self.cleaned_data['last_name'],
#             username=username,
#         )
#         user.set_password(self.cleaned_data['password'])
#         user.save()
# 
#         # Store the user's email address; we don't use User.email as we support
#         # multiple email addresses.
#         email = user.emails.create(email=self.cleaned_data['email'])
# 
#         # Update Profile model rather than create a new one.
#         profile = super(RegistrationForm, self).save(commit=False)
#         profile.user = user
# 
#         if profile.account_type == AccountEnum.INDIVIDUAL:
#             profile.display_name = u"%s %s" % (user.first_name, user.last_name)
#         else:
#             profile.display_name = profile.organisation
# 
#         profile.save()
# 
#         # Send confirmation email
#         send_mail((email.email,), 'registration/confirm.email', {
#             'user': user,
#         })
# 
#         return user


# Path: src/librement/registration/views.py
from django.conf import settings
from django.shortcuts import render, redirect
from django.contrib.auth.decorators import login_required
from .forms import RegistrationForm

# Copyright 2012 The Librement Developers
#
# See the AUTHORS file at the top-level directory of this distribution
# and at http://librement.net/copyright/
#
# This file is part of Librement. It is subject to the license terms in
# the LICENSE file found in the top-level directory of this distribution
# and at http://librement.net/license/. No part of Librement, including
# this file, may be copied, modified, propagated, or distributed except
# according to the terms contained in the LICENSE file.



def view(request):
    if request.user.is_authenticated():
        return redirect(settings.LOGIN_REDIRECT_URL)

    if request.method == 'POST':

        form = RegistrationForm(request.POST)