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

53 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.http import HttpRequest
from konova.models import RoleGroup
from konova.sub_settings.context_settings import BASE_TITLE, WIKI_URL, BASE_FRONTEND_TITLE, RENDER_HEADER
from konova.utils.session import set_session_user_role
class BaseContext:
"""
Holds all base data which is needed for every context rendering
"""
context = {
"base_title": BASE_TITLE,
"base_frontend_title": BASE_FRONTEND_TITLE,
"language": "en",
"wiki_url": WIKI_URL,
"user": None,
"render_header": RENDER_HEADER,
"current_role": None,
}
def __init__(self, request: HttpRequest, additional_context: dict = {}):
self.context["language"] = request.LANGUAGE_CODE
self.context["user"] = request.user
self.__handle_current_role(request)
# Add additional context, derived from given parameters
self.context.update(additional_context)
def __handle_current_role(self, request: HttpRequest):
""" Reads/Writes current role from/to session
Args:
request (HttpRequest): The incoming request
Returns:
"""
# Store current role in session object to reduce amount of db access
current_role = request.session.get("current_role", {})
if len(current_role) == 0 and request.user.is_authenticated:
role_group = RoleGroup.get_users_role_groups(request.user).first()
current_role = set_session_user_role(request, role_group)
self.context["current_role"] = current_role