From f852594f544f86eea7450a0aa9d885e4b4dbb383 Mon Sep 17 00:00:00 2001 From: Awais Qureshi Date: Wed, 29 Nov 2023 16:15:38 +0500 Subject: [PATCH] Upgrading ipware (#33804) * feat: Upgrade Python dependency django-ipware (#33805) --- openedx/core/djangoapps/geoinfo/middleware.py | 12 ++++++++---- requirements/edx/base.txt | 4 +++- requirements/edx/development.txt | 7 ++++++- requirements/edx/doc.txt | 6 +++++- requirements/edx/testing.txt | 6 +++++- 5 files changed, 27 insertions(+), 8 deletions(-) diff --git a/openedx/core/djangoapps/geoinfo/middleware.py b/openedx/core/djangoapps/geoinfo/middleware.py index 9b9df817e224..ff9ed271bbd1 100644 --- a/openedx/core/djangoapps/geoinfo/middleware.py +++ b/openedx/core/djangoapps/geoinfo/middleware.py @@ -12,8 +12,7 @@ import logging from django.utils.deprecation import MiddlewareMixin -from ipware.ip import get_client_ip -from ipware.utils import is_public_ip +from python_ipware import IpWare from .api import country_code_from_ip @@ -30,13 +29,18 @@ def process_request(self, request): Store country code in session. """ - new_ip_address = get_client_ip(request)[0] + ipw = IpWare() + new_ip_address_obj, _ = ipw.get_client_ip(meta=request.META) + + if new_ip_address_obj: + new_ip_address = format(new_ip_address_obj) + old_ip_address = request.session.get('ip_address', None) if not new_ip_address and old_ip_address: del request.session['ip_address'] del request.session['country_code'] - elif new_ip_address != old_ip_address and is_public_ip(new_ip_address): + elif new_ip_address != old_ip_address and new_ip_address_obj.is_global: country_code = country_code_from_ip(new_ip_address) request.session['country_code'] = country_code request.session['ip_address'] = new_ip_address diff --git a/requirements/edx/base.txt b/requirements/edx/base.txt index 3bb0a62217f5..fea16cdc7e64 100644 --- a/requirements/edx/base.txt +++ b/requirements/edx/base.txt @@ -286,7 +286,7 @@ django-filter==23.3 # edx-enterprise # lti-consumer-xblock # openedx-blockstore -django-ipware==5.0.2 +django-ipware==6.0.0 # via # -r requirements/edx/kernel.in # edx-enterprise @@ -925,6 +925,8 @@ python-dateutil==2.8.2 # olxcleaner # ora2 # xblock +python-ipware==2.0.0 + # via django-ipware python-memcached==1.59 # via -r requirements/edx/paver.txt python-slugify==8.0.1 diff --git a/requirements/edx/development.txt b/requirements/edx/development.txt index 72bc57a6e310..dd3ac4cf191f 100644 --- a/requirements/edx/development.txt +++ b/requirements/edx/development.txt @@ -483,7 +483,7 @@ django-filter==23.3 # edx-enterprise # lti-consumer-xblock # openedx-blockstore -django-ipware==5.0.2 +django-ipware==6.0.0 # via # -r requirements/edx/doc.txt # -r requirements/edx/testing.txt @@ -1640,6 +1640,11 @@ python-dateutil==2.8.2 # olxcleaner # ora2 # xblock +python-ipware==2.0.0 + # via + # -r requirements/edx/doc.txt + # -r requirements/edx/testing.txt + # django-ipware python-memcached==1.59 # via # -r requirements/edx/doc.txt diff --git a/requirements/edx/doc.txt b/requirements/edx/doc.txt index aebce21c1789..99bfb51b4917 100644 --- a/requirements/edx/doc.txt +++ b/requirements/edx/doc.txt @@ -346,7 +346,7 @@ django-filter==23.3 # edx-enterprise # lti-consumer-xblock # openedx-blockstore -django-ipware==5.0.2 +django-ipware==6.0.0 # via # -r requirements/edx/base.txt # edx-enterprise @@ -1102,6 +1102,10 @@ python-dateutil==2.8.2 # olxcleaner # ora2 # xblock +python-ipware==2.0.0 + # via + # -r requirements/edx/base.txt + # django-ipware python-memcached==1.59 # via -r requirements/edx/base.txt python-slugify==8.0.1 diff --git a/requirements/edx/testing.txt b/requirements/edx/testing.txt index 5f9329c94df5..6a77a39fa793 100644 --- a/requirements/edx/testing.txt +++ b/requirements/edx/testing.txt @@ -379,7 +379,7 @@ django-filter==23.3 # edx-enterprise # lti-consumer-xblock # openedx-blockstore -django-ipware==5.0.2 +django-ipware==6.0.0 # via # -r requirements/edx/base.txt # edx-enterprise @@ -1231,6 +1231,10 @@ python-dateutil==2.8.2 # olxcleaner # ora2 # xblock +python-ipware==2.0.0 + # via + # -r requirements/edx/base.txt + # django-ipware python-memcached==1.59 # via -r requirements/edx/base.txt python-slugify==8.0.1