konova/kspneo/views.py
2021-07-01 13:36:07 +02:00

67 lines
1.7 KiB
Python

"""
Author: Michel Peltriaux
Organization: Struktur- und Genehmigungsdirektion Nord, Rhineland-Palatinate, Germany
Contact: michel.peltriaux@sgdnord.rlp.de
Created on: 16.11.20
"""
from django.utils.translation import gettext_lazy as _
from django.contrib import messages
from django.contrib.auth import logout
from django.http import HttpRequest
from django.shortcuts import redirect, render
from konova.contexts import BaseContext
from konova.forms import ChangeUserRoleForm
from konova.settings import SSO_SERVER_BASE
from konova.utils.session import get_session_user_role
def logout_view(request: HttpRequest):
"""
Logout route for ending the session manually.
Args:
request (HttpRequest): The used request object
Returns:
A redirect
"""
logout(request)
return redirect(SSO_SERVER_BASE)
def home_view(request: HttpRequest):
"""
Renders the landing page
Args:
request (HttpRequest): The used request object
Returns:
A redirect
"""
template = "konova/home.html"
if request.method == "POST":
form = ChangeUserRoleForm(
request.POST or None,
user=request.user,
)
if form.is_valid():
role = form.save(request)
messages.success(request, _("Role changed"))
else:
messages.error(request, _("Invalid role"))
return redirect("home")
else:
# GET
form = ChangeUserRoleForm(
user=request.user,
initial={"role": int(get_session_user_role(request).get("id", -1))},
)
additional_context = {
"form": form,
}
context = BaseContext(request, additional_context).context
return render(request, template, context)