# Path: quora/profile/forms.py
# class SignUpForm(forms.ModelForm):
#     firstname = forms.CharField(widget=forms.TextInput(attrs={'class':'form-control'}),
#         max_length=30,
#         required=True,
#         label='First name')
#     username = forms.CharField(widget=forms.TextInput(attrs={'class':'form-control'}),
#         max_length=30,
#         required=True,
#         label='Email')
#     password = forms.CharField(widget=forms.PasswordInput(attrs={'class':'form-control'}))
# 
#     class Meta:
#         model = User
#         exclude = ['last_login', 'date_joined']
#         fields = ['firstname', 'username', 'password']
# 
#     def __init__(self, *args, **kwargs):
#         super(SignUpForm, self).__init__(*args, **kwargs)
#         self.fields['username'].validators.append(ForbiddenUsernamesValidator)
#         self.fields['username'].validators.append(InvalidUsernameValidator)
#         self.fields['username'].validators.append(UniqueUsernameIgnoreCaseValidator)
# 
#     def clean(self):
#         super(SignUpForm, self).clean()
#         password = self.cleaned_data.get('password')
#         return self.cleaned_data


from django.shortcuts import render, redirect
from django.contrib.auth import authenticate, login
from quora.profile.forms import SignUpForm
from django.contrib.auth.models import User

def signup(request):
    if request.method == 'POST':
        form = SignUpForm(request.POST)
        if not form.is_valid():
            return render(request, 'auth/signup.html', {'form': form})
        else:
            firstname = form.cleaned_data.get('firstname')
            username = form.cleaned_data.get('username')
            password = form.cleaned_data.get('password')
            User.objects.create_user(
                first_name=firstname,
                username=username,
                password=password)
            user = authenticate(username=username, password=password)
            login(request, user)
            return redirect('/')
    else:

# Path: quora/profile/forms.py
# class SignUpForm(forms.ModelForm):
#     firstname = forms.CharField(widget=forms.TextInput(attrs={'class':'form-control'}),
#         max_length=30,
#         required=True,
#         label='First name')
#     username = forms.CharField(widget=forms.TextInput(attrs={'class':'form-control'}),
#         max_length=30,
#         required=True,
#         label='Email')
#     password = forms.CharField(widget=forms.PasswordInput(attrs={'class':'form-control'}))
# 
#     class Meta:
#         model = User
#         exclude = ['last_login', 'date_joined']
#         fields = ['firstname', 'username', 'password']
# 
#     def __init__(self, *args, **kwargs):
#         super(SignUpForm, self).__init__(*args, **kwargs)
#         self.fields['username'].validators.append(ForbiddenUsernamesValidator)
#         self.fields['username'].validators.append(InvalidUsernameValidator)
#         self.fields['username'].validators.append(UniqueUsernameIgnoreCaseValidator)
# 
#     def clean(self):
#         super(SignUpForm, self).clean()
#         password = self.cleaned_data.get('password')
#         return self.cleaned_data


# Path: quora/profile/views.py
from django.shortcuts import render, redirect
from django.contrib.auth import authenticate, login
from quora.profile.forms import SignUpForm
from django.contrib.auth.models import User


def signup(request):
    if request.method == 'POST':
        form = SignUpForm(request.POST)
        if not form.is_valid():
            return render(request, 'auth/signup.html', {'form': form})
        else:
            firstname = form.cleaned_data.get('firstname')
            username = form.cleaned_data.get('username')
            password = form.cleaned_data.get('password')
            User.objects.create_user(
                first_name=firstname,
                username=username,
                password=password)
            user = authenticate(username=username, password=password)
            login(request, user)
            return redirect('/')
    else:

        return render(request, 'auth/signup.html', {'form': SignUpForm()})