"""
Author: Michel Peltriaux
Organization: Struktur- und Genehmigungsdirektion Nord, Rhineland-Palatinate, Germany
Contact: ksp-servicestelle@sgdnord.rlp.de
Created on: 19.08.22

"""
from django.contrib.auth import logout
from django.http import HttpRequest
from django.shortcuts import redirect
from django.views import View

from konova.sub_settings.sso_settings import SSO_SERVER_BASE


class LogoutView(View):
    def get(self, request: HttpRequest):
        """
        Logout route for ending the session manually.

        Args:
            request (HttpRequest): The used request object

        Returns:
            A redirect
        """
        user = request.user
        try:
            oauth_token = user.oauth_token
            if oauth_token:
                oauth_token.revoke()
        except AttributeError:
            pass

        logout(request)
        return redirect(SSO_SERVER_BASE)
