2022-09-18 21:00:41 +00:00
|
|
|
from os import getenv
|
|
|
|
from time import time
|
2020-05-17 06:57:00 +00:00
|
|
|
|
2020-02-14 20:35:02 +00:00
|
|
|
import pytz
|
|
|
|
|
2020-05-17 06:57:00 +00:00
|
|
|
from django.conf import settings
|
2021-11-28 19:02:23 +00:00
|
|
|
from django.utils import timezone, translation
|
2021-11-12 18:13:08 +00:00
|
|
|
from django.conf.locale.en import formats as formats_en_us
|
2021-11-13 16:02:31 +00:00
|
|
|
from django.conf.locale.en_GB import formats as formats_en_gb
|
2022-09-18 21:00:41 +00:00
|
|
|
from django.contrib.auth.middleware import RemoteUserMiddleware
|
2021-11-12 18:13:08 +00:00
|
|
|
|
|
|
|
|
|
|
|
def update_en_us_date_formats():
|
|
|
|
"""
|
|
|
|
Update the datetime formats for the en-US locale. This is handled here and
|
|
|
|
not using `FORMAT_MODULE_PATH` because the processing of format modules
|
|
|
|
does not allow us to distinguish appropriately between en-US and en-GB
|
|
|
|
based on user settings.
|
|
|
|
"""
|
|
|
|
if settings.USE_24_HOUR_TIME_FORMAT:
|
2022-02-10 00:00:30 +00:00
|
|
|
formats_en_us.DATETIME_FORMAT = "N j, Y, H:i:s"
|
2021-11-12 18:13:08 +00:00
|
|
|
custom_input_formats = [
|
2022-02-10 00:00:30 +00:00
|
|
|
"%m/%d/%Y %H:%M:%S", # '10/25/2006 14:30:59'
|
|
|
|
"%m/%d/%Y %H:%M", # '10/25/2006 14:30'
|
2021-11-12 18:13:08 +00:00
|
|
|
]
|
2022-02-10 00:00:30 +00:00
|
|
|
formats_en_us.SHORT_DATETIME_FORMAT = "m/d/Y G:i:s"
|
|
|
|
formats_en_us.TIME_FORMAT = "H:i:s"
|
2021-11-12 18:13:08 +00:00
|
|
|
else:
|
|
|
|
# These formats are added to support the locale style of Baby Buddy's
|
|
|
|
# frontend library, which uses momentjs.
|
|
|
|
custom_input_formats = [
|
2022-02-10 00:00:30 +00:00
|
|
|
"%m/%d/%Y %I:%M:%S %p", # '10/25/2006 2:30:59 PM'
|
|
|
|
"%m/%d/%Y %I:%M %p", # '10/25/2006 2:30 PM'
|
2021-11-12 18:13:08 +00:00
|
|
|
]
|
|
|
|
|
|
|
|
# Add custom "short" version of `MONTH_DAY_FORMAT`.
|
2022-02-10 00:00:30 +00:00
|
|
|
formats_en_us.SHORT_MONTH_DAY_FORMAT = "M j"
|
2021-11-12 18:13:08 +00:00
|
|
|
|
|
|
|
# Append all other input formats from the base locale.
|
2022-02-10 00:00:30 +00:00
|
|
|
formats_en_us.DATETIME_INPUT_FORMATS = (
|
2021-11-12 18:13:08 +00:00
|
|
|
custom_input_formats + formats_en_us.DATETIME_INPUT_FORMATS
|
2022-02-10 00:00:30 +00:00
|
|
|
)
|
2021-11-12 18:13:08 +00:00
|
|
|
|
|
|
|
|
2021-11-13 16:02:31 +00:00
|
|
|
def update_en_gb_date_formats():
|
|
|
|
if settings.USE_24_HOUR_TIME_FORMAT:
|
2021-11-15 17:24:08 +00:00
|
|
|
# 25 October 2006 14:30:00
|
2022-02-10 00:00:30 +00:00
|
|
|
formats_en_gb.DATETIME_FORMAT = "j F Y H:i:s"
|
2021-11-13 16:02:31 +00:00
|
|
|
custom_input_formats = [
|
2022-02-10 00:00:30 +00:00
|
|
|
"%d/%m/%Y %H:%M:%S", # '25/10/2006 14:30:59'
|
|
|
|
"%d/%m/%Y %H:%M", # '25/10/2006 14:30'
|
2021-11-13 16:02:31 +00:00
|
|
|
]
|
2022-02-10 00:00:30 +00:00
|
|
|
formats_en_gb.SHORT_DATETIME_FORMAT = "d/m/Y H:i"
|
|
|
|
formats_en_gb.TIME_FORMAT = "H:i"
|
2021-11-13 16:02:31 +00:00
|
|
|
else:
|
2022-02-10 00:00:30 +00:00
|
|
|
formats_en_gb.DATETIME_FORMAT = "j F Y f a" # 25 October 2006 2:30 p.m
|
2021-11-13 16:02:31 +00:00
|
|
|
# These formats are added to support the locale style of Baby Buddy's
|
|
|
|
# frontend library, which uses momentjs.
|
|
|
|
custom_input_formats = [
|
2022-02-10 00:00:30 +00:00
|
|
|
"%d/%m/%Y %I:%M:%S %p", # '25/10/2006 2:30:59 PM'
|
|
|
|
"%d/%m/%Y %I:%M %p", # '25/10/2006 2:30 PM'
|
2021-11-13 16:02:31 +00:00
|
|
|
]
|
|
|
|
|
|
|
|
# Append all other input formats from the base locale.
|
2022-02-10 00:00:30 +00:00
|
|
|
formats_en_gb.DATETIME_INPUT_FORMATS = (
|
2021-11-13 16:02:31 +00:00
|
|
|
custom_input_formats + formats_en_gb.DATETIME_INPUT_FORMATS
|
2022-02-10 00:00:30 +00:00
|
|
|
)
|
2021-11-13 16:02:31 +00:00
|
|
|
|
|
|
|
|
2021-11-12 18:13:08 +00:00
|
|
|
class UserLanguageMiddleware:
|
|
|
|
"""
|
|
|
|
Customizes settings based on user language setting.
|
|
|
|
"""
|
2022-02-10 00:00:30 +00:00
|
|
|
|
2021-11-12 18:13:08 +00:00
|
|
|
def __init__(self, get_response):
|
|
|
|
self.get_response = get_response
|
|
|
|
|
|
|
|
def __call__(self, request):
|
|
|
|
user = request.user
|
2022-02-10 00:00:30 +00:00
|
|
|
if hasattr(user, "settings") and user.settings.language:
|
2021-11-28 19:18:00 +00:00
|
|
|
language = user.settings.language
|
|
|
|
elif request.LANGUAGE_CODE:
|
|
|
|
language = request.LANGUAGE_CODE
|
|
|
|
else:
|
|
|
|
language = settings.LANGUAGE_CODE
|
|
|
|
|
|
|
|
if language:
|
2022-02-10 00:00:30 +00:00
|
|
|
if language == "en-US":
|
2021-11-28 19:02:23 +00:00
|
|
|
update_en_us_date_formats()
|
2022-02-10 00:00:30 +00:00
|
|
|
elif language == "en-GB":
|
2021-11-28 19:44:36 +00:00
|
|
|
update_en_gb_date_formats()
|
2021-11-28 19:02:23 +00:00
|
|
|
|
2021-11-28 19:18:00 +00:00
|
|
|
# Set the language before generating the response.
|
|
|
|
translation.activate(language)
|
|
|
|
|
2021-11-28 19:02:23 +00:00
|
|
|
response = self.get_response(request)
|
|
|
|
|
|
|
|
# Deactivate the translation before the response is sent so it not
|
|
|
|
# reused in other threads.
|
|
|
|
translation.deactivate()
|
|
|
|
|
|
|
|
return response
|
2020-02-14 20:35:02 +00:00
|
|
|
|
|
|
|
|
|
|
|
class UserTimezoneMiddleware:
|
|
|
|
"""
|
2022-06-11 21:27:52 +00:00
|
|
|
Sets the timezone based on a user specific setting. This middleware must run after
|
|
|
|
`django.contrib.auth.middleware.AuthenticationMiddleware` because it uses the
|
|
|
|
request.user object.
|
2020-02-14 20:35:02 +00:00
|
|
|
"""
|
2022-02-10 00:00:30 +00:00
|
|
|
|
2020-02-14 20:35:02 +00:00
|
|
|
def __init__(self, get_response):
|
|
|
|
self.get_response = get_response
|
|
|
|
|
|
|
|
def __call__(self, request):
|
2021-10-17 21:45:53 +00:00
|
|
|
user = request.user
|
2022-02-10 00:00:30 +00:00
|
|
|
if hasattr(user, "settings") and user.settings.timezone:
|
2020-02-14 20:35:02 +00:00
|
|
|
try:
|
2021-10-17 21:45:53 +00:00
|
|
|
timezone.activate(pytz.timezone(user.settings.timezone))
|
2020-02-14 20:35:02 +00:00
|
|
|
except pytz.UnknownTimeZoneError:
|
|
|
|
pass
|
|
|
|
return self.get_response(request)
|
2020-05-17 06:57:00 +00:00
|
|
|
|
2020-05-17 10:11:08 +00:00
|
|
|
|
2020-05-17 06:57:00 +00:00
|
|
|
class RollingSessionMiddleware:
|
|
|
|
"""
|
2021-08-05 13:34:08 +00:00
|
|
|
Periodically resets the session expiry for existing sessions.
|
2020-05-17 06:57:00 +00:00
|
|
|
"""
|
2022-02-10 00:00:30 +00:00
|
|
|
|
2020-05-17 06:57:00 +00:00
|
|
|
def __init__(self, get_response):
|
|
|
|
self.get_response = get_response
|
|
|
|
|
|
|
|
def __call__(self, request):
|
2021-08-05 13:34:08 +00:00
|
|
|
if request.session.keys():
|
2022-02-10 00:00:30 +00:00
|
|
|
session_refresh = request.session.get("session_refresh")
|
2021-08-05 13:34:08 +00:00
|
|
|
if session_refresh:
|
|
|
|
try:
|
2022-09-18 21:00:41 +00:00
|
|
|
delta = int(time()) - session_refresh
|
2021-08-05 13:34:08 +00:00
|
|
|
except (ValueError, TypeError):
|
|
|
|
delta = settings.ROLLING_SESSION_REFRESH + 1
|
|
|
|
if delta > settings.ROLLING_SESSION_REFRESH:
|
2022-09-18 21:00:41 +00:00
|
|
|
request.session["session_refresh"] = int(time())
|
2021-08-05 13:34:08 +00:00
|
|
|
request.session.set_expiry(settings.SESSION_COOKIE_AGE)
|
|
|
|
else:
|
2022-09-18 21:00:41 +00:00
|
|
|
request.session["session_refresh"] = int(time())
|
2020-05-17 06:57:00 +00:00
|
|
|
return self.get_response(request)
|
2022-09-18 21:00:41 +00:00
|
|
|
|
|
|
|
|
|
|
|
class CustomRemoteUser(RemoteUserMiddleware):
|
|
|
|
"""
|
|
|
|
Middleware used for remote authentication when `REVERSE_PROXY_AUTH` is True.
|
|
|
|
"""
|
|
|
|
|
|
|
|
header = getenv("PROXY_HEADER", "HTTP_REMOTE_USER")
|