Skip to content

Commit 1e4855c

Browse files
committed
fix: override logout view
1 parent 06bbc5b commit 1e4855c

File tree

3 files changed

+13
-5
lines changed

3 files changed

+13
-5
lines changed

codeforlife/urls.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,12 @@
66
import typing as t
77

88
from django.contrib import admin
9-
from django.contrib.auth.views import LogoutView
109
from django.http import HttpResponse
1110
from django.urls import URLPattern, URLResolver, include, path, re_path
1211
from rest_framework import status
1312

1413
from .settings import SERVICE_IS_ROOT, SERVICE_NAME
15-
from .views import csrf
14+
from .views import CsrfCookieView, LogoutView
1615

1716
UrlPatterns = t.List[t.Union[URLResolver, URLPattern]]
1817

@@ -39,7 +38,7 @@ def get_urlpatterns(
3938
),
4039
path(
4140
"api/csrf/cookie/",
42-
csrf.CookieView.as_view(),
41+
CsrfCookieView.as_view(),
4342
name="get-csrf-cookie",
4443
),
4544
path(

codeforlife/views/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@
44
"""
55

66
from .api import APIView
7-
from .csrf import CookieView
7+
from .common import CsrfCookieView, LogoutView
88
from .decorators import action, cron_job
99
from .model import ModelViewSet

codeforlife/views/csrf.py renamed to codeforlife/views/common.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
Created on 12/04/2024 at 16:51:36(+01:00).
44
"""
55

6+
from django.contrib.auth.views import LogoutView as _LogoutView
7+
from django.http import JsonResponse
68
from django.utils.decorators import method_decorator
79
from django.views.decorators.csrf import ensure_csrf_cookie
810
from rest_framework.request import Request
@@ -12,7 +14,7 @@
1214
from ..permissions import AllowAny
1315

1416

15-
class CookieView(APIView):
17+
class CsrfCookieView(APIView):
1618
"""A view to get a CSRF cookie."""
1719

1820
http_method_names = ["get"]
@@ -24,3 +26,10 @@ def get(self, request: Request):
2426
Return a response which Django will auto-insert a CSRF cookie into.
2527
"""
2628
return Response()
29+
30+
31+
class LogoutView(_LogoutView):
32+
"""Override Django's logout view to always return a JSON response."""
33+
34+
def render_to_response(self, context, **response_kwargs):
35+
return JsonResponse({})

0 commit comments

Comments
 (0)