diff --git a/crank/decorators.py b/crank/decorators.py new file mode 100644 index 00000000..66a22b94 --- /dev/null +++ b/crank/decorators.py @@ -0,0 +1,21 @@ +# Copyright (c) 2024 Isaac Adams +# Licensed under the MIT License. See LICENSE file in the project root for full license information. +from functools import wraps + +from django.utils.decorators import method_decorator +from django.views.decorators.cache import cache_page + +def cache_page_if_anonymous_method(timeout, view_func=None): + def decorator(view_func): + @wraps(view_func) + def _wrapped_view(request, *args, **kwargs): + if request.user.is_authenticated: + return view_func(request, *args, **kwargs) + return cache_page(timeout)(view_func)(request, *args, **kwargs) + return _wrapped_view + + def class_decorator(cls): + cls.dispatch = method_decorator(decorator, name='dispatch')(cls.dispatch) + return cls + + return class_decorator if isinstance(view_func, type) else decorator \ No newline at end of file diff --git a/crank/tests/views/test_logout.py b/crank/tests/views/test_logout.py new file mode 100644 index 00000000..84cd8883 --- /dev/null +++ b/crank/tests/views/test_logout.py @@ -0,0 +1,31 @@ +# Copyright (c) 2024 Isaac Adams +# Licensed under the MIT License. See LICENSE file in the project root for full license information. +# crank/tests/views/test_logout.py +from django.test import TestCase, Client +from django.urls import reverse +from django.contrib.auth.models import User +from allauth.socialaccount.models import SocialApp + +class LogoutViewTests(TestCase): + def setUp(self): + self.client = Client() + self.user = User.objects.create_user(username='testuser', password='testpassword') + self.client.login(username='testuser', password='testpassword') + + # Create a SocialApp instance + self.social_app = SocialApp.objects.create( + provider='google', + name='Google', + client_id='fake-client-id', + secret='fake-secret', + ) + self.social_app.sites.add(1) # Assuming the site ID is 1 + + def test_logout_redirects_to_index(self): + response = self.client.post(reverse('account_logout')) + self.assertRedirects(response, reverse('index')) + + def test_logout_user(self): + self.client.post(reverse('account_logout')) + _ = self.client.get(reverse('index')) + self.assertNotIn('_auth_user_id', self.client.session) \ No newline at end of file diff --git a/crank/urls.py b/crank/urls.py index dfa00bf6..c4c61572 100644 --- a/crank/urls.py +++ b/crank/urls.py @@ -26,6 +26,7 @@ from crank.views.rtopolicy import RTOPolicyChoicesView from crank.views.index import IndexView from crank.views.organization import OrganizationView +from crank.views.logout import CustomLogoutView app_name = "crank" @@ -37,6 +38,6 @@ path('api/funding-round-choices/', cache_page(settings.CACHE_MIDDLEWARE_SECONDS)(FundingRoundChoicesView.as_view()), name='funding_round_choices'), path('api/rto-policy-choices/', cache_page(settings.CACHE_MIDDLEWARE_SECONDS)(RTOPolicyChoicesView.as_view()), name='rto_policy_choices'), path('api-auth/', include('rest_framework.urls')), + path('accounts/logout/', CustomLogoutView.as_view(), name='account_logout'), path('accounts/', include('allauth.urls')), - path('logout', LogoutView.as_view()) ] diff --git a/crank/views/logout.py b/crank/views/logout.py new file mode 100644 index 00000000..d3b36c0a --- /dev/null +++ b/crank/views/logout.py @@ -0,0 +1,13 @@ +# Copyright (c) 2024 Isaac Adams +# Licensed under the MIT License. See LICENSE file in the project root for full license information. +from django.utils.decorators import method_decorator +from django.views.decorators.csrf import csrf_exempt +from allauth.account.views import LogoutView as AllauthLogoutView +from django.shortcuts import redirect, render + + +@method_decorator(csrf_exempt, name='dispatch') +class CustomLogoutView(AllauthLogoutView): + def post(self, request, *args, **kwargs): + _ = super().post(request, *args, **kwargs) + return redirect('index') # Redirect to home page after logout \ No newline at end of file diff --git a/templates/base.html b/templates/base.html index bf1878be..818eba5c 100644 --- a/templates/base.html +++ b/templates/base.html @@ -29,7 +29,10 @@ {% if user.is_staff %} Admin {% endif %} - Logout {{ user.username }} +
{% else %}