"""
Author: Michel Peltriaux
Organization: Struktur- und Genehmigungsdirektion Nord, Rhineland-Palatinate, Germany
Contact: michel.peltriaux@sgdnord.rlp.de
Created on: 27.01.22

"""
from django.contrib.auth.decorators import login_required
from django.http import HttpRequest, JsonResponse

from api.models import APIUserToken


@login_required
def generate_new_token_view(request: HttpRequest):
    """ Handles request for fetching

    Args:
        request (HttpRequest): The incoming request

    Returns:

    """

    if request.method == "GET":
        token = APIUserToken()
        while APIUserToken.objects.filter(token=token.token).exists():
            token = APIUserToken()
        return JsonResponse(
            data={
                "gen_data": token.token
            }
        )
    else:
        raise NotImplementedError