mirror of
https://github.com/ArchiveBox/ArchiveBox.git
synced 2025-05-13 06:34:25 -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
|
|
@ -55,12 +55,14 @@ INSTALLED_APPS = [
|
||||||
|
|
||||||
|
|
||||||
MIDDLEWARE = [
|
MIDDLEWARE = [
|
||||||
|
'core.middleware.TimezoneMiddleware',
|
||||||
'django.middleware.security.SecurityMiddleware',
|
'django.middleware.security.SecurityMiddleware',
|
||||||
'django.contrib.sessions.middleware.SessionMiddleware',
|
'django.contrib.sessions.middleware.SessionMiddleware',
|
||||||
'django.middleware.common.CommonMiddleware',
|
'django.middleware.common.CommonMiddleware',
|
||||||
'django.middleware.csrf.CsrfViewMiddleware',
|
'django.middleware.csrf.CsrfViewMiddleware',
|
||||||
'django.contrib.auth.middleware.AuthenticationMiddleware',
|
'django.contrib.auth.middleware.AuthenticationMiddleware',
|
||||||
'django.contrib.messages.middleware.MessageMiddleware',
|
'django.contrib.messages.middleware.MessageMiddleware',
|
||||||
|
'core.middleware.CacheControlMiddleware',
|
||||||
]
|
]
|
||||||
|
|
||||||
AUTHENTICATION_BACKENDS = [
|
AUTHENTICATION_BACKENDS = [
|
||||||
|
|
|
@ -1,5 +1,8 @@
|
||||||
{% load i18n static %}<!DOCTYPE html>
|
{% load i18n static tz %}
|
||||||
{% get_current_language as LANGUAGE_CODE %}{% get_current_language_bidi as LANGUAGE_BIDI %}
|
{% get_current_language as LANGUAGE_CODE %}
|
||||||
|
{% get_current_language_bidi as LANGUAGE_BIDI %}
|
||||||
|
|
||||||
|
<!DOCTYPE html>
|
||||||
<html lang="{{ LANGUAGE_CODE|default:"en-us" }}" {% if LANGUAGE_BIDI %}dir="rtl"{% endif %}>
|
<html lang="{{ LANGUAGE_CODE|default:"en-us" }}" {% if LANGUAGE_BIDI %}dir="rtl"{% endif %}>
|
||||||
<head>
|
<head>
|
||||||
<title>{% block title %}{% endblock %} | ArchiveBox</title>
|
<title>{% block title %}{% endblock %} | ArchiveBox</title>
|
||||||
|
|
|
@ -1,5 +1,4 @@
|
||||||
{% load admin_urls %}
|
{% load static tz admin_urls %}
|
||||||
{% load static %}
|
|
||||||
|
|
||||||
<!DOCTYPE html>
|
<!DOCTYPE html>
|
||||||
<html lang="en">
|
<html lang="en">
|
||||||
|
@ -66,6 +65,35 @@
|
||||||
</footer>
|
</footer>
|
||||||
{% endblock %}
|
{% endblock %}
|
||||||
</div>
|
</div>
|
||||||
|
<script>
|
||||||
|
// hide images that fail to load
|
||||||
|
document.querySelector('body').addEventListener('error', function (e) {
|
||||||
|
e.target.style.opacity = 0;
|
||||||
|
}, true)
|
||||||
|
|
||||||
|
// setup timezone
|
||||||
|
{% get_current_timezone as TIME_ZONE %}
|
||||||
|
window.TIME_ZONE = '{{TIME_ZONE}}'
|
||||||
|
|
||||||
|
window.setCookie = function(name, value, days) {
|
||||||
|
let expires = ""
|
||||||
|
if (days) {
|
||||||
|
const date = new Date()
|
||||||
|
date.setTime(date.getTime() + (days*24*60*60*1000))
|
||||||
|
expires = "; expires=" + date.toUTCString()
|
||||||
|
}
|
||||||
|
document.cookie = name + "=" + (value || "") + expires + "; path=/"
|
||||||
|
}
|
||||||
|
|
||||||
|
function setTimeOffset() {
|
||||||
|
if (window.GMT_OFFSET) return
|
||||||
|
window.GMT_OFFSET = -(new Date).getTimezoneOffset()
|
||||||
|
window.setCookie('GMT_OFFSET', window.GMT_OFFSET, 365)
|
||||||
|
}
|
||||||
|
|
||||||
|
jQuery(document).ready(function () {
|
||||||
|
setTimeOffset();
|
||||||
|
});
|
||||||
|
</script>
|
||||||
</body>
|
</body>
|
||||||
|
</html>
|
||||||
</html>
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue