2018-08-10 18:55:42 -04:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
"""
|
|
|
|
/login app views.
|
|
|
|
"""
|
|
|
|
from django.contrib import messages
|
|
|
|
from django.shortcuts import redirect, render
|
|
|
|
from django.contrib.auth import authenticate, login
|
|
|
|
|
2018-08-12 01:43:47 -04:00
|
|
|
from .forms import LoginForm
|
|
|
|
|
2018-08-10 18:55:42 -04:00
|
|
|
def index(request):
|
|
|
|
"""
|
|
|
|
The login page.
|
|
|
|
"""
|
2018-08-12 01:59:22 -04:00
|
|
|
if request.user.is_authenticated:
|
|
|
|
return redirect('homepage:index')
|
2018-08-10 18:55:42 -04:00
|
|
|
if request.method == "GET":
|
2018-08-12 01:43:47 -04:00
|
|
|
form = LoginForm()
|
|
|
|
return render(request, 'login/index.html', {'form': form})
|
|
|
|
form = LoginForm(request.POST)
|
|
|
|
if form.is_valid():
|
|
|
|
user = authenticate(
|
|
|
|
request,
|
|
|
|
username=form.cleaned_data.get('username'),
|
|
|
|
password=form.cleaned_data.get('password'))
|
|
|
|
if user is not None:
|
|
|
|
login(request, user)
|
|
|
|
messages.success(request, "Logged in")
|
|
|
|
return redirect('homepage:index')
|
|
|
|
messages.error(request, "Invalid credentials")
|
|
|
|
return render(request, 'login/index.html', {'form': form})
|