Skip to content

Commit

Permalink
common: add new 'common' app for custom middleware
Browse files Browse the repository at this point in the history
- new middleware was required for setting cache-control headers without
  impacting server-side cache
- this middleware is potenially cross-cutting
- settings requires modules to exist in an app
  • Loading branch information
jsnshrmn committed Apr 28, 2022
1 parent 4b0a621 commit bb6ba8a
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 0 deletions.
1 change: 1 addition & 0 deletions TWLight/common/__init__.py
@@ -0,0 +1 @@

9 changes: 9 additions & 0 deletions TWLight/common/apps.py
@@ -0,0 +1,9 @@
from django.apps import AppConfig


class MiddlewareConfig(AppConfig):
name = "TWLight.common"
label = "common"

def ready(self):
import TWLight.common.middleware
1 change: 1 addition & 0 deletions TWLight/common/middleware/__init__.py
@@ -0,0 +1 @@

17 changes: 17 additions & 0 deletions TWLight/common/middleware/cache.py
@@ -0,0 +1,17 @@
from django.conf import settings
from django.utils.cache import add_never_cache_headers


class NeverCacheHttpHeadersMiddleware:
"""
Callable middleware that sets cache-control headers without impacting server-side caching
"""

def __init__(self, get_response):
self.get_response = get_response

def __call__(self, request):
response = self.get_response(request)
if request.path in settings.NEVER_CACHE_HTTP_HEADER_PATHS:
add_never_cache_headers(response)
return response
7 changes: 7 additions & 0 deletions TWLight/settings/base.py
Expand Up @@ -48,6 +48,11 @@
}
}

# Site paths that shouldn't be cached browser-side
NEVER_CACHE_HTTP_HEADER_PATHS = [
"/users/my_library/",
]

# An atypical way of setting django languages for TranslateWiki integration:
# https://translatewiki.net/wiki/Thread:Support/_The_following_issue_is_unconfirmed,_still_to_be_investigated._Adding_TheWikipediaLibrary_Card_Platform_TranslateWiki

Expand Down Expand Up @@ -129,6 +134,7 @@ def get_django_faker_languages_intersection(languages):
"TWLight.comments",
"TWLight.api",
"TWLight.ezproxy",
"TWLight.common",
]

# dal (autocomplete_light) must go before django.contrib.admin.
Expand Down Expand Up @@ -171,6 +177,7 @@ def get_django_faker_languages_intersection(languages):
# That’s why SessionMiddleware must be enabled and appear before
# MessageMiddleware.
"django.contrib.messages.middleware.MessageMiddleware",
"TWLight.common.middleware.cache.NeverCacheHttpHeadersMiddleware",
]


Expand Down

0 comments on commit bb6ba8a

Please sign in to comment.