-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding view caching, refactoring logout
- Loading branch information
=
committed
Nov 14, 2024
1 parent
4e42738
commit 6f8419d
Showing
4 changed files
with
40 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters