@login_required
@user_passes_test(lambda u: u.is_superuser)
def api_access(request):
    if request.method == "POST":
        form = APIAccessForm(request.POST, user=request.user)
        if form.is_valid():
            if form.cleaned_data.get("enable_api_access"):
                Token.objects.get_or_create(user=request.user)
            else:
                Token.objects.filter(user=request.user).delete()
            return render_to_json_response(_("Access updated"))
        return render_to_json_response({
            "form_errors": form.errors
        }, status=400)
    form = APIAccessForm(user=request.user)
    return render_to_json_response({
        "content": _render_to_string(
            request, "core/api_access.html", {"form": form})
    })
A view to configure API access.
dataset/ETHPy150Open tonioo/modoboa/modoboa/core/views/user.py/api_access