# Path: myproject/utils.py
# def recaptcha_is_valid(request):
#     if settings.GOOGLE_RECAPTCHA_ENABLED:
#         recaptcha_response = request.POST.get('g-recaptcha-response')
#         data = {
#             'secret': settings.GOOGLE_RECAPTCHA_SECRET_KEY,
#             'response': recaptcha_response
#         }
#         r = requests.post('https://www.google.com/recaptcha/api/siteverify', data=data)
#         result = r.json()
#         return result['success']
#     return True
# 
# Path: accounts/forms.py
# class SignUpForm(UserCreationForm):
#     email = forms.CharField(max_length=254, required=True, widget=forms.EmailInput())
# 
#     class Meta:
#         model = User
#         fields = ('username', 'email', 'password1', 'password2')
# 
# class UserInformationUpdateForm(forms.ModelForm):
# 	email = forms.EmailField()
# 	class Meta:
# 		model = User
# 		fields = ('first_name', 'last_name', 'email', )


from django.contrib.auth import login as auth_login
from django.contrib.auth.decorators import login_required
from django.contrib.auth.models import User
from django.shortcuts import redirect, render
from django.urls import reverse_lazy
from django.utils.decorators import method_decorator
from django.views.generic import UpdateView
from myproject.utils import recaptcha_is_valid
from .forms import SignUpForm, UserInformationUpdateForm




def signup(request):
    if request.method == 'POST':
        form = SignUpForm(request.POST)
        if form.is_valid() and recaptcha_is_valid(request):
            user = form.save()
            auth_login(request, user)
            return redirect('home')
    else:

# Path: myproject/utils.py
# def recaptcha_is_valid(request):
#     if settings.GOOGLE_RECAPTCHA_ENABLED:
#         recaptcha_response = request.POST.get('g-recaptcha-response')
#         data = {
#             'secret': settings.GOOGLE_RECAPTCHA_SECRET_KEY,
#             'response': recaptcha_response
#         }
#         r = requests.post('https://www.google.com/recaptcha/api/siteverify', data=data)
#         result = r.json()
#         return result['success']
#     return True
# 
# Path: accounts/forms.py
# class SignUpForm(UserCreationForm):
#     email = forms.CharField(max_length=254, required=True, widget=forms.EmailInput())
# 
#     class Meta:
#         model = User
#         fields = ('username', 'email', 'password1', 'password2')
# 
# class UserInformationUpdateForm(forms.ModelForm):
# 	email = forms.EmailField()
# 	class Meta:
# 		model = User
# 		fields = ('first_name', 'last_name', 'email', )


# Path: accounts/views.py
from django.contrib.auth import login as auth_login
from django.contrib.auth.decorators import login_required
from django.contrib.auth.models import User
from django.shortcuts import redirect, render
from django.urls import reverse_lazy
from django.utils.decorators import method_decorator
from django.views.generic import UpdateView
from myproject.utils import recaptcha_is_valid
from .forms import SignUpForm, UserInformationUpdateForm





def signup(request):
    if request.method == 'POST':
        form = SignUpForm(request.POST)
        if form.is_valid() and recaptcha_is_valid(request):
            user = form.save()
            auth_login(request, user)
            return redirect('home')
    else:

        form = SignUpForm()