def signup_page(request):
    if request.user.is_authenticated():
        return HttpResponseRedirect(reverse('index_page'))

    if request.method == "POST":
        email = request.POST.get('email')
        password = request.POST.get('password')

        if Account.objects.filter(email=email).exists():
            context = {"form_message": {"title": "<span class=\"icon-caution\"></span> SIGNUP ERROR", "message": "Email already exists.", "type": "danger", "animation": "animated shake" }}
            return render(request, 'signup.html', context)

        if not email or not password:
            context = {"form_message": {"title": "<span class=\"icon-caution\"></span> SIGNUP ERROR", "message": "All fields are required.", "type": "danger", "animation": "animated shake" }}
            return render(request, 'signup.html', context)

        try:
            validate_email(email)

        except ValidationError:
            context = {"form_message": {"title": "<span class=\"icon-caution\"></span> SIGNUP ERROR", "message": "Enter a valid email address.", "type": "danger", "animation": "animated shake" }}
            return render(request, 'signup.html', context)
            
        try:
            user = Account()
            user.email = email
            user.set_password(password)
            user.is_activated = False
            user.save()
            Notification.objects.create(account=user, description="Account created.")
            token = ActivationToken(email=email)
            token.save()
            send_activation_token(request, token)
            Notification.objects.create(account=user, description="Activation mail sent.")

        except Exception as e:
            context = {"form_message": {"title": "<span class=\"icon-caution\"></span> SIGNUP ERROR", "message": "Cannot create account. Contact support team.<br/> DEBUG" + str(e), "type": "danger"}
                    }
            return render(request, 'signup.html', context)

        context = {"form_message": {"title": "<span class=\"icon-happy\"></span> SIGNUP SUCCESS", "message": " Activation mail sent.", "type": "success"}}
        return render(request, 'login.html', context)
            

    else:
        return render(request, 'signup.html')