-
Notifications
You must be signed in to change notification settings - Fork 619
/
Copy pathmiddleware.py
35 lines (27 loc) · 984 Bytes
/
middleware.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
from django.conf import settings
class AdminNoCaching:
"""
Middleware to ensure the admin is not cached by Fastly or other caches
"""
def __init__(self, get_response):
self.get_response = get_response
def __call__(self, request):
response = self.get_response(request)
if request.path.startswith("/admin"):
response["Cache-Control"] = "private"
return response
class GlobalSurrogateKey:
"""
Middleware to insert a Surrogate-Key for purging in Fastly or other caches
"""
def __init__(self, get_response):
self.get_response = get_response
def __call__(self, request):
response = self.get_response(request)
if hasattr(settings, "GLOBAL_SURROGATE_KEY"):
response["Surrogate-Key"] = " ".join(
filter(
None, [settings.GLOBAL_SURROGATE_KEY, response.get("Surrogate-Key")]
)
)
return response