from urllib.parse import quote as urlquote from django.contrib.auth import logout as auth_logout from django.contrib.auth.decorators import login_required from django.http import HttpRequest from django.http.response import Http404 from django.shortcuts import redirect, render from django.urls import reverse def login(request: HttpRequest): if request.user.is_authenticated: return redirect("homepage") match request.method: case "GET": data = request.GET case "POST": data = request.POST case _: raise Http404 next = data.get("next") if next is not None: context = { "pass_url": "{}?next={}".format( reverse("password_login"), urlquote(next, safe="") ), "cas_url": "{}?next={}".format( reverse("cas_ng_login"), urlquote(next, safe="") ), } else: context = { "pass_url": reverse("password_login"), "cas_url": reverse("cas_ng_login"), } return render(request, "mainsite/login.html", context=context) @login_required def logout(request: HttpRequest): if request.session["_auth_user_backend"] != "django_cas_ng.backends.CASBackend": auth_logout(request) return redirect("homepage") return redirect("cas_ng_logout")