Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions openedx/core/djangoapps/geoinfo/middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Expand Down
4 changes: 3 additions & 1 deletion requirements/edx/base.txt
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ django-filter==23.3
# edx-enterprise
# lti-consumer-xblock
# openedx-blockstore
django-ipware==5.0.1
django-ipware==6.0.0
# via
# -r requirements/edx/kernel.in
# edx-enterprise
Expand Down Expand Up @@ -930,6 +930,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
Expand Down
7 changes: 6 additions & 1 deletion requirements/edx/development.txt
Original file line number Diff line number Diff line change
Expand Up @@ -481,7 +481,7 @@ django-filter==23.3
# edx-enterprise
# lti-consumer-xblock
# openedx-blockstore
django-ipware==5.0.1
django-ipware==6.0.0
# via
# -r requirements/edx/doc.txt
# -r requirements/edx/testing.txt
Expand Down Expand Up @@ -1651,6 +1651,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
Expand Down
6 changes: 5 additions & 1 deletion requirements/edx/doc.txt
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,7 @@ django-filter==23.3
# edx-enterprise
# lti-consumer-xblock
# openedx-blockstore
django-ipware==5.0.1
django-ipware==6.0.0
# via
# -r requirements/edx/base.txt
# edx-enterprise
Expand Down Expand Up @@ -1110,6 +1110,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
Expand Down
6 changes: 5 additions & 1 deletion requirements/edx/testing.txt
Original file line number Diff line number Diff line change
Expand Up @@ -382,7 +382,7 @@ django-filter==23.3
# edx-enterprise
# lti-consumer-xblock
# openedx-blockstore
django-ipware==5.0.1
django-ipware==6.0.0
# via
# -r requirements/edx/base.txt
# edx-enterprise
Expand Down Expand Up @@ -1243,6 +1243,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
Expand Down