mirror of
https://github.com/ArchiveBox/ArchiveBox.git
synced 2025-05-12 22:25:44 -04:00
add new timezone autosetting and cache header setting middlewares
This commit is contained in:
parent
1977ae8962
commit
cf7d7e4990
4 changed files with 76 additions and 6 deletions
37
archivebox/core/middleware.py
Normal file
37
archivebox/core/middleware.py
Normal file
|
@ -0,0 +1,37 @@
|
|||
__package__ = 'archivebox.core'
|
||||
|
||||
from django.utils import timezone
|
||||
|
||||
from ..config import PUBLIC_SNAPSHOTS
|
||||
|
||||
|
||||
def detect_timezone(request, activate: bool=True):
|
||||
gmt_offset = (request.COOKIES.get('GMT_OFFSET') or '').strip()
|
||||
tz = None
|
||||
if gmt_offset.replace('-', '').isdigit():
|
||||
tz = timezone.get_fixed_timezone(int(gmt_offset))
|
||||
if activate:
|
||||
timezone.activate(tz)
|
||||
# print('GMT_OFFSET', gmt_offset, tz)
|
||||
return tz
|
||||
|
||||
|
||||
def TimezoneMiddleware(get_response):
|
||||
def middleware(request):
|
||||
detect_timezone(request, activate=True)
|
||||
return get_response(request)
|
||||
|
||||
return middleware
|
||||
|
||||
|
||||
def CacheControlMiddleware(get_response):
|
||||
def middleware(request):
|
||||
response = get_response(request)
|
||||
|
||||
if '/archive/' in request.path or '/static/' in request.path:
|
||||
policy = 'public' if PUBLIC_SNAPSHOTS else 'private'
|
||||
response['Cache-Control'] = f'{policy}, max-age=60, stale-while-revalidate=300'
|
||||
# print('Set Cache-Control header to', response['Cache-Control'])
|
||||
return response
|
||||
|
||||
return middleware
|
Loading…
Add table
Add a link
Reference in a new issue