Add middleware support for detecting user timezones
This commit is contained in:
parent
5100c8b922
commit
a321a69336
38
polyclinic_scheduling/apps/RUG_template/middleware.py
Normal file
38
polyclinic_scheduling/apps/RUG_template/middleware.py
Normal file
@ -0,0 +1,38 @@
|
||||
import pytz
|
||||
import requests
|
||||
|
||||
from ipware import get_client_ip
|
||||
from django.utils import timezone
|
||||
|
||||
# make sure you add `TimezoneMiddleware` appropriately in settings.py: 'apps.RUG_template.middleware.TimezoneMiddleware'
|
||||
class TimezoneMiddleware:
|
||||
""" Middleware to check user timezone. """
|
||||
def __init__(self, get_response):
|
||||
self.get_response = get_response
|
||||
# One-time configuration and initialization.
|
||||
|
||||
def __call__(self, request):
|
||||
# Code to be executed for each request before
|
||||
# the view (and later middleware) are called.
|
||||
client_ip, is_routable = get_client_ip(request)
|
||||
user_time_zone = request.session.get('user_time_zone', None)
|
||||
try:
|
||||
if user_time_zone is None and is_routable and client_ip is not None:
|
||||
# Here we use an online service to get visitor info. Maybe not the nicest way to do it, but it is a way
|
||||
# Also we only check when we get a public IP address. Local networks will not be checked online
|
||||
# https://freegeoip.app
|
||||
freegeoip_response = requests.get('https://freegeoip.app/json/{0}'.format(client_ip))
|
||||
freegeoip_response_json = freegeoip_response.json()
|
||||
user_time_zone = freegeoip_response_json['time_zone']
|
||||
if user_time_zone:
|
||||
request.session['user_time_zone'] = user_time_zone
|
||||
timezone.activate(pytz.timezone(user_time_zone))
|
||||
except:
|
||||
pass
|
||||
|
||||
response = self.get_response(request)
|
||||
|
||||
# Code to be executed for each request/response after
|
||||
# the view is called.
|
||||
|
||||
return response
|
@ -14,7 +14,10 @@ INTERNAL_IPS=127.0.0.1
|
||||
DATABASE_URL=sqlite:////opt/poli_planning/polyclinic_scheduling/db.sqlite3
|
||||
|
||||
# The location on disk where the static files will be placed during deployment. Setting is required
|
||||
STATIC_ROOT =
|
||||
STATIC_ROOT=
|
||||
|
||||
# Enter the default timezone for the visitors when it is not known.
|
||||
TIME_ZONE=Europe/Amsterdam
|
||||
|
||||
# Email settings
|
||||
|
||||
|
@ -61,6 +61,7 @@ MIDDLEWARE = [
|
||||
'django.contrib.auth.middleware.AuthenticationMiddleware',
|
||||
'django.contrib.messages.middleware.MessageMiddleware',
|
||||
'django.middleware.clickjacking.XFrameOptionsMiddleware',
|
||||
'apps.RUG_template.middleware.TimezoneMiddleware',
|
||||
'django.middleware.locale.LocaleMiddleware',
|
||||
]
|
||||
|
||||
@ -130,7 +131,7 @@ LANGUAGES = [
|
||||
('en', _('English')),
|
||||
]
|
||||
|
||||
TIME_ZONE = 'UTC'
|
||||
TIME_ZONE = config('TIME_ZONE', default='UTC')
|
||||
|
||||
USE_I18N = True
|
||||
|
||||
|
@ -2,4 +2,4 @@ Django==3.0.6
|
||||
jsonfield==3.1.0
|
||||
python-decouple==3.3
|
||||
dj-database-url==0.5.0
|
||||
mysqlclient==1.4.6
|
||||
django-ipware==2.1.0
|
Loading…
x
Reference in New Issue
Block a user