import datetime

from django.contrib.auth import logout
from django.shortcuts import render, redirect
from .forms import RegisterForm
from django.http import HttpResponse
from django.contrib.auth.forms import AuthenticationForm
from django.conf import settings
from django.contrib.auth import authenticate, login
from django.http import HttpResponseRedirect
from django.contrib import messages
# Create your views here.

def register(response):
    if response.user.is_authenticated:
        return redirect("homepage")

    else:
        if response.method == "POST":
            form = RegisterForm(response.POST)
            if form.is_valid():
                new_user = form.save()
                # messages.info(response, "Thanks for registering. You are now logged in.")
                new_user = authenticate(username=form.cleaned_data['username'],
                                        password=form.cleaned_data['password1'],
                                        )
                login(response, new_user)
            return redirect("homepage")
        else:
            form = RegisterForm()

        return render(response, "register/register.html", {"form": form})



[((16, 15, 16, 35), 'django.shortcuts.redirect', 'redirect', ({(16, 24, 16, 34): '"""homepage"""'}, {}), "('homepage')", False, 'from django.shortcuts import render, redirect\n'), ((32, 15, 32, 73), 'django.shortcuts.render', 'render', ({(32, 22, 32, 30): 'response', (32, 32, 32, 56): '"""register/register.html"""', (32, 58, 32, 72): "{'form': form}"}, {}), "(response, 'register/register.html', {'form': form})", False, 'from django.shortcuts import render, redirect\n'), ((28, 19, 28, 39), 'django.shortcuts.redirect', 'redirect', ({(28, 28, 28, 38): '"""homepage"""'}, {}), "('homepage')", False, 'from django.shortcuts import render, redirect\n'), ((24, 27, 26, 41), 'django.contrib.auth.authenticate', 'authenticate', (), '', False, 'from django.contrib.auth import authenticate, login\n'), ((27, 16, 27, 41), 'django.contrib.auth.login', 'login', ({(27, 22, 27, 30): 'response', (27, 32, 27, 40): 'new_user'}, {}), '(response, new_user)', False, 'from django.contrib.auth import authenticate, login\n')]