Skip to content

Commit

Permalink
Support Django 5.1 (#76)
Browse files Browse the repository at this point in the history
Refactor cache middleware to be compliant with Django API (fixes bug
caused by Django 5.1). Fixes #74.

---------

Co-authored-by: Anthony Uphof <[email protected]>
Co-authored-by: Gwendolyn <[email protected]>
  • Loading branch information
3 people committed Aug 21, 2024
1 parent 0df74a6 commit f52c780
Showing 1 changed file with 8 additions and 8 deletions.
16 changes: 8 additions & 8 deletions wagtailcache/cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,10 +183,9 @@ class FetchFromCacheMiddleware(MiddlewareMixin):
Mostly stolen from ``django.middleware.cache.FetchFromCacheMiddleware``.
"""

def __init__(self, get_response=None):
def __init__(self, get_response):
self._wagcache = caches[wagtailcache_settings.WAGTAIL_CACHE_BACKEND]
self.get_response = get_response
self._async_check()
super().__init__(get_response)

def process_request(self, request: WSGIRequest) -> Optional[HttpResponse]:
if not wagtailcache_settings.WAGTAIL_CACHE:
Expand Down Expand Up @@ -250,10 +249,9 @@ class UpdateCacheMiddleware(MiddlewareMixin):
Mostly stolen from ``django.middleware.cache.UpdateCacheMiddleware``.
"""

def __init__(self, get_response=None):
def __init__(self, get_response):
self._wagcache = caches[wagtailcache_settings.WAGTAIL_CACHE_BACKEND]
self.get_response = get_response
self._async_check()
super().__init__(get_response)

def process_response(
self, request: WSGIRequest, response: HttpResponse
Expand Down Expand Up @@ -411,13 +409,15 @@ def _wrapped_view_func(
request: WSGIRequest, *args, **kwargs
) -> HttpResponse:
# Try to fetch an already cached page from wagtail-cache.
response = FetchFromCacheMiddleware().process_request(request)
response = FetchFromCacheMiddleware(view_func).process_request(request)
if response:
return response
# Since we don't have a response at this point, process the request.
response = view_func(request, *args, **kwargs)
# Cache the response.
response = UpdateCacheMiddleware().process_response(request, response)
response = UpdateCacheMiddleware(view_func).process_response(
request, response
)
return response

return _wrapped_view_func
Expand Down

0 comments on commit f52c780

Please sign in to comment.