# Path: Blog/accounts/forms.py
# class UserLoginForm(forms.Form):
#     username = forms.CharField()
#     password = forms.CharField(widget=forms.PasswordInput)
# 
#     def clean(self, *args, **kwargs):
#         username = self.cleaned_data.get("username")
#         password = self.cleaned_data.get("password")
# 
#         if username and password:
#             user = authenticate(username = username, password = password)
#             if not user:
#                 raise forms.ValidationError("Incorrect username or password.")
#             if not user.check_password(password):
#                 raise forms.ValidationError("Incorrect username or password.")
#             if not user.is_active:
#                 raise forms.ValidationError("This user is no longer active.")
#         return super(UserLoginForm, self).clean(*args, **kwargs)
# 
# class UserRegistrationForm(forms.ModelForm):
#     email = forms.EmailField(label="Email", required=True)
#     email2 = forms.EmailField(label="Confirm Email", required=True)
#     password = forms.CharField(widget=forms.PasswordInput)
# 
#     class Meta:
#         model = User
#         fields =[
#             'username',
#             'email',
#             'email2',
#             'password'
#         ]
# 
#     def clean(self, *args, **kwargs):
#         email = self.cleaned_data.get("email")
#         email2 = self.cleaned_data.get("email2")
#         if email!=email2:
#             self.add_error("email2", "Emails must match!")
#             raise forms.ValidationError("Emails must match!")
#         email_qs = User.objects.filter(email=email)
#         if email_qs.exists():
#             self.add_error("email", "This email has already been used.")
#             raise forms.ValidationError("Emails used!")
#         return super(UserRegistrationForm, self).clean(*args, **kwargs)
# 
# Path: Blog/accounts/models.py
# class Account(models.Model):
#     user = models.OneToOneField(User)
#     email_activated = models.BooleanField(default=False)
# 
#     def get_email_activation_url(self):
#         if self.user.id is None:
#             return None
#         email_token = serializer.dumps(self.id)
#         activation_url = settings.MYHOST + reverse("accounts:activation", kwargs={"token":email_token})
#         return activation_url


from django.shortcuts import render
from django.contrib.auth import (
    authenticate,
    login,
    logout
)
from django.http import HttpResponseRedirect, Http404
from django.contrib import messages
from django.contrib.auth import get_user_model
from django.conf import settings
from .forms import UserLoginForm, UserRegistrationForm
from Blog.celery_tasks import send_html_email
from .models import Account
from accounts import serializer
import logging
        user = form.save(commit=False)
        password = form.cleaned_data.get("password")
        user.set_password(password)
        user.save()
        account = Account()
        account.user = user
        account.save()
        new_user = authenticate(username = user.username, password=password)
        login(request,new_user)
        send_html_email.delay(
            from_addr = settings.EMAIL_SMTP_USERNAME,
            to_addr = new_user.email,
            html= settings.EMAIL_ACTIVATION_TEMPLATE.format(username=new_user.username, activation_url=account.get_email_activation_url()),
            subject = "bigborg.top Email Activation"
        )
        next = request.GET.get("next")
        if next:
            return HttpResponseRedirect(next)
        return HttpResponseRedirect("/")
    return render(request, "accounts/form.html", context)

def logout_view(request):
    logout(request)
    messages.success(request,"Logged out!")
    return HttpResponseRedirect("/")

def activate_email(request, token):
    try:
        id = serializer.loads(token)
        print(id)

# Path: Blog/accounts/forms.py
# class UserLoginForm(forms.Form):
#     username = forms.CharField()
#     password = forms.CharField(widget=forms.PasswordInput)
# 
#     def clean(self, *args, **kwargs):
#         username = self.cleaned_data.get("username")
#         password = self.cleaned_data.get("password")
# 
#         if username and password:
#             user = authenticate(username = username, password = password)
#             if not user:
#                 raise forms.ValidationError("Incorrect username or password.")
#             if not user.check_password(password):
#                 raise forms.ValidationError("Incorrect username or password.")
#             if not user.is_active:
#                 raise forms.ValidationError("This user is no longer active.")
#         return super(UserLoginForm, self).clean(*args, **kwargs)
# 
# class UserRegistrationForm(forms.ModelForm):
#     email = forms.EmailField(label="Email", required=True)
#     email2 = forms.EmailField(label="Confirm Email", required=True)
#     password = forms.CharField(widget=forms.PasswordInput)
# 
#     class Meta:
#         model = User
#         fields =[
#             'username',
#             'email',
#             'email2',
#             'password'
#         ]
# 
#     def clean(self, *args, **kwargs):
#         email = self.cleaned_data.get("email")
#         email2 = self.cleaned_data.get("email2")
#         if email!=email2:
#             self.add_error("email2", "Emails must match!")
#             raise forms.ValidationError("Emails must match!")
#         email_qs = User.objects.filter(email=email)
#         if email_qs.exists():
#             self.add_error("email", "This email has already been used.")
#             raise forms.ValidationError("Emails used!")
#         return super(UserRegistrationForm, self).clean(*args, **kwargs)
# 
# Path: Blog/accounts/models.py
# class Account(models.Model):
#     user = models.OneToOneField(User)
#     email_activated = models.BooleanField(default=False)
# 
#     def get_email_activation_url(self):
#         if self.user.id is None:
#             return None
#         email_token = serializer.dumps(self.id)
#         activation_url = settings.MYHOST + reverse("accounts:activation", kwargs={"token":email_token})
#         return activation_url


# Path: Blog/accounts/views.py
from django.shortcuts import render
from django.contrib.auth import (
    authenticate,
    login,
    logout
)
from django.http import HttpResponseRedirect, Http404
from django.contrib import messages
from django.contrib.auth import get_user_model
from django.conf import settings
from .forms import UserLoginForm, UserRegistrationForm
from Blog.celery_tasks import send_html_email
from .models import Account
from accounts import serializer
import logging

        user = form.save(commit=False)
        password = form.cleaned_data.get("password")
        user.set_password(password)
        user.save()
        account = Account()
        account.user = user
        account.save()
        new_user = authenticate(username = user.username, password=password)
        login(request,new_user)
        send_html_email.delay(
            from_addr = settings.EMAIL_SMTP_USERNAME,
            to_addr = new_user.email,
            html= settings.EMAIL_ACTIVATION_TEMPLATE.format(username=new_user.username, activation_url=account.get_email_activation_url()),
            subject = "bigborg.top Email Activation"
        )
        next = request.GET.get("next")
        if next:
            return HttpResponseRedirect(next)
        return HttpResponseRedirect("/")
    return render(request, "accounts/form.html", context)

def logout_view(request):
    logout(request)
    messages.success(request,"Logged out!")
    return HttpResponseRedirect("/")

def activate_email(request, token):
    try:
        id = serializer.loads(token)
        print(id)

        account = Account.objects.filter(id=id).first()