Skip to content

Commit

Permalink
Adding view caching, refactoring logout
Browse files Browse the repository at this point in the history
  • Loading branch information
= committed Nov 14, 2024
1 parent 4e42738 commit 6f8419d
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 2 deletions.
21 changes: 21 additions & 0 deletions crank/decorators.py
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
3 changes: 2 additions & 1 deletion crank/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand All @@ -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())
]
13 changes: 13 additions & 0 deletions crank/views/logout.py
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

Check warning on line 13 in crank/views/logout.py

View check run for this annotation

Codecov / codecov/patch

crank/views/logout.py#L12-L13

Added lines #L12 - L13 were not covered by tests
5 changes: 4 additions & 1 deletion templates/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,10 @@
{% if user.is_staff %}
<a class="btn btn-primary btn-sm" href="/admin">Admin</a>
{% endif %}
<a class="btn btn-secondary btn-sm" href="/logout">Logout {{ user.username }}</a>
<form method="post" action="{% url 'account_logout' %}" style="display:inline;">
{% csrf_token %}
<button class="btn btn-secondary btn-sm" type="submit">Logout {{ user.username }}</button>
</form>
{% else %}
<a class="btn btn-light btn-sm" href="{% provider_login_url 'google' %}">
<i class="fab fa-google"></i>
Expand Down

0 comments on commit 6f8419d

Please sign in to comment.