diff --git a/polyclinic_scheduling/apps/RUG_template/middleware.py b/polyclinic_scheduling/apps/RUG_template/middleware.py new file mode 100644 index 0000000..cf9f7d3 --- /dev/null +++ b/polyclinic_scheduling/apps/RUG_template/middleware.py @@ -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 \ No newline at end of file diff --git a/polyclinic_scheduling/polyclinic_scheduling/.env.example b/polyclinic_scheduling/polyclinic_scheduling/.env.example index a7f96ec..2d0d79e 100644 --- a/polyclinic_scheduling/polyclinic_scheduling/.env.example +++ b/polyclinic_scheduling/polyclinic_scheduling/.env.example @@ -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 diff --git a/polyclinic_scheduling/polyclinic_scheduling/settings.py b/polyclinic_scheduling/polyclinic_scheduling/settings.py index 0c21089..f628770 100644 --- a/polyclinic_scheduling/polyclinic_scheduling/settings.py +++ b/polyclinic_scheduling/polyclinic_scheduling/settings.py @@ -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 diff --git a/requirements.txt b/requirements.txt index e9f338a..5523ee0 100644 --- a/requirements.txt +++ b/requirements.txt @@ -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 \ No newline at end of file +django-ipware==2.1.0 \ No newline at end of file