sso/accounts/views.py

165 lines
5.3 KiB
Python

# Copyright © 2022 Alan Alexander Thomas <alan2000alex@gmail.com>
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
#
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
from audioop import reverse
from multiprocessing import get_context
# Create your views here.
from re import template
from urllib import response
from django.shortcuts import render, redirect, get_object_or_404
from django.contrib.auth.password_validation import validate_password
from django.contrib.auth import authenticate, login, logout, get_user_model
from django.http import HttpResponse, HttpResponseRedirect
from django.views.decorators.csrf import csrf_protect
from django.core.exceptions import ValidationError
from django.contrib.auth.decorators import login_required
from django.contrib import messages
from django.urls import reverse
# Create your views here.
from django.http import HttpResponse
def index(request):
return HttpResponse("Hello World.")
# @csrf_protect
# def login(request):
# return render(request, 'accounts/login.html')
# login page
@csrf_protect
def login_user(request):
def default_login_ctx():
return {
"title": "Login",
"footer": footer_ctx(),
}
if request.method == "POST":
email = request.POST["email"]
password = request.POST["password"]
# domain_check = email.split("@")
# check user exists
User = get_user_model()
if not User.objects.filter(email=email).exists():
messages.info(request, "Username OR password is incorrect")
return redirect(reverse("accounts:login"))
username = User.objects.get(email=email).username
user = authenticate(request, username=username, password=password)
if user is not None:
login(request, user)
# Redirect to a success page.
print(reverse("accounts:success_page"))
if "next" in request.POST:
next = request.POST["next"]
if len(next) > 0:
return redirect(next)
return redirect(reverse("accounts:success_page"))
else:
# Return an 'invalid login' error message.
messages.info(request, "Username OR password is incorrect")
return redirect(reverse("accounts:login"))
else:
context = {}
if "next" in request.GET:
next = request.GET["next"]
context["next"] = next
return render(request, "accounts/login.html", context=context)
# success page
@login_required(login_url="/accounts/login/")
@csrf_protect
def success_page(request):
return render(request, "accounts/success.html")
# user registratoin
@csrf_protect
def register(request):
# response = "You are at the Registration Page."
get_context = {}
if request.method == "GET":
if "next" in request.GET:
get_context["next"] = request.GET["next"]
return render(request, "accounts/register.html", get_context)
context = {}
# variables
email = request.POST["email"]
password = request.POST["password"]
password_confirm = request.POST["password-confirm"]
# password matching
if password != password_confirm:
context["error"] = {
"title": "Registration Failed",
"reason": "Passwords do not match.",
}
return render(request, "accounts/register.html", status=400, context=context)
# domain verification
domain_check = email.split("@")
if domain_check[1] != "vitap.ac.in":
context["error"] = {
"title": "Registration Failed",
"reason": "We do not provide services for this domain yet.",
}
return render(request, "accounts/register.html", status=400, context=context)
# email verification
User = get_user_model()
if any(
[
User.objects.filter(email=email).exists(),
User.objects.filter(username=domain_check[0]).exists(),
]
):
context["error"] = {
"title": "Registration Failed",
"reason": "This email is already registered.",
}
return render(request, "accounts/register.html", status=400, context=context)
user = get_user_model()(
username=domain_check[0],
email=email,
)
user.set_password(password)
try:
user.full_clean()
validate_password(password, user=user)
except ValidationError as err:
reason = ""
for errors in err:
reason += errors + " "
context["error"] = {"title": "Registration Failed", "reason": reason}
print(reason)
return render(request, "accounts/register.html", status=400, context=context)
user.save()
return HttpResponse("New acc. can be registered.")