# Path: commerce/forms.py
# class RegisterForm(ModelForm):
#     first_name = forms.CharField(label='Votre prénom', required=True)
#     last_name = forms.CharField(label='Votre nom', required=True)
#     email = forms.EmailField(label='Votre adresse e-mail', required=True)
# 
#     class Meta:
#         model = User
#         fields = ['first_name', 'last_name', 'username', 'password', 'email']
# 
# class RegisterFormUpdate(ModelForm):
#     first_name = forms.CharField(label='Votre prénom', required=True)
#     last_name = forms.CharField(label='Votre nom', required=True)
#     email = forms.EmailField(label='Votre adresse e-mail', required=True)
# 
#     class Meta:
#         model = User
#         fields = ['first_name', 'last_name', 'email']
# 
# class AddAddress(ModelForm):
#     class Meta:
#         model = Address
#         fields = ['gender', 'first_name', 'last_name', 'company', 'address', 'additional_address',
#                   'postcode', 'city', 'phone', 'mobilephone', 'fax', 'workphone']


from commerce.models import *
from commerce.forms import RegisterForm, RegisterFormUpdate, AddAddress
from django.contrib import messages
from django.contrib.auth import authenticate, login, logout
from django.core.urlresolvers import reverse
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
from django.shortcuts import render, get_object_or_404, redirect
from django.contrib.auth.decorators import login_required
import stripe
import datetime
                client = Client(user_id=user.id)
                client.save()

                # On connecte le client
                user = authenticate(username=form.cleaned_data['username'], password=form.cleaned_data['password'])
                __move_session_cart_to_database_cart(request, client.id)
                login(request, user)

                if request.GET.get('next', False):
                    return redirect(request.GET['next'])
                else:
                    return redirect(reverse('commerce:root'))
        else:
            user = authenticate(username=request.POST['username'], password=request.POST['password'])
            if user is not None:
                if user.is_active:
                    client = Client.objects.filter(user_id=user.id).first()
                    __move_session_cart_to_database_cart(request, client.id)
                    login(request, user)
                    if request.GET.get('next', False):
                        return redirect(request.GET['next'])
                    else:
                        return redirect(reverse('commerce:root'))
                else:
                    messages.add_message(request, messages.ERROR,
                                         'Votre compte a été désactivé, veuillez-contacter le service client.')
            else:
                messages.add_message(request, messages.ERROR,
                                     'Les identifiants que vous avez saisis sont incorrects !')


# Path: commerce/forms.py
# class RegisterForm(ModelForm):
#     first_name = forms.CharField(label='Votre prénom', required=True)
#     last_name = forms.CharField(label='Votre nom', required=True)
#     email = forms.EmailField(label='Votre adresse e-mail', required=True)
# 
#     class Meta:
#         model = User
#         fields = ['first_name', 'last_name', 'username', 'password', 'email']
# 
# class RegisterFormUpdate(ModelForm):
#     first_name = forms.CharField(label='Votre prénom', required=True)
#     last_name = forms.CharField(label='Votre nom', required=True)
#     email = forms.EmailField(label='Votre adresse e-mail', required=True)
# 
#     class Meta:
#         model = User
#         fields = ['first_name', 'last_name', 'email']
# 
# class AddAddress(ModelForm):
#     class Meta:
#         model = Address
#         fields = ['gender', 'first_name', 'last_name', 'company', 'address', 'additional_address',
#                   'postcode', 'city', 'phone', 'mobilephone', 'fax', 'workphone']


# Path: commerce/views.py
from commerce.models import *
from commerce.forms import RegisterForm, RegisterFormUpdate, AddAddress
from django.contrib import messages
from django.contrib.auth import authenticate, login, logout
from django.core.urlresolvers import reverse
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
from django.shortcuts import render, get_object_or_404, redirect
from django.contrib.auth.decorators import login_required
import stripe
import datetime

                client = Client(user_id=user.id)
                client.save()

                # On connecte le client
                user = authenticate(username=form.cleaned_data['username'], password=form.cleaned_data['password'])
                __move_session_cart_to_database_cart(request, client.id)
                login(request, user)

                if request.GET.get('next', False):
                    return redirect(request.GET['next'])
                else:
                    return redirect(reverse('commerce:root'))
        else:
            user = authenticate(username=request.POST['username'], password=request.POST['password'])
            if user is not None:
                if user.is_active:
                    client = Client.objects.filter(user_id=user.id).first()
                    __move_session_cart_to_database_cart(request, client.id)
                    login(request, user)
                    if request.GET.get('next', False):
                        return redirect(request.GET['next'])
                    else:
                        return redirect(reverse('commerce:root'))
                else:
                    messages.add_message(request, messages.ERROR,
                                         'Votre compte a été désactivé, veuillez-contacter le service client.')
            else:
                messages.add_message(request, messages.ERROR,
                                     'Les identifiants que vous avez saisis sont incorrects !')


    return render(request, 'signin.html', {