Upgrade django-ipware (#24827)
This commit is contained in:
@@ -10,7 +10,7 @@ import logging
|
||||
|
||||
from django.conf import settings
|
||||
from django.core.cache import cache
|
||||
from ipware.ip import get_ip
|
||||
from ipware.ip import get_client_ip
|
||||
from rest_framework import status
|
||||
from rest_framework.response import Response
|
||||
|
||||
@@ -197,7 +197,7 @@ def get_embargo_response(request, course_id, user):
|
||||
|
||||
"""
|
||||
redirect_url = redirect_if_blocked(
|
||||
course_id, user=user, ip_address=get_ip(request), url=request.path)
|
||||
course_id, user=user, ip_address=get_client_ip(request)[0], url=request.path)
|
||||
if redirect_url:
|
||||
return Response(
|
||||
status=status.HTTP_403_FORBIDDEN,
|
||||
|
||||
@@ -34,7 +34,7 @@ from django.core.exceptions import MiddlewareNotUsed
|
||||
from django.urls import reverse
|
||||
from django.utils.deprecation import MiddlewareMixin
|
||||
from django.shortcuts import redirect
|
||||
from ipware.ip import get_ip
|
||||
from ipware.ip import get_client_ip
|
||||
|
||||
from openedx.core.lib.request_utils import course_id_from_url
|
||||
|
||||
@@ -83,7 +83,7 @@ class EmbargoMiddleware(MiddlewareMixin):
|
||||
if pattern.match(request.path) is not None:
|
||||
return None
|
||||
|
||||
ip_address = get_ip(request)
|
||||
ip_address = get_client_ip(request)[0]
|
||||
ip_filter = IPFilter.current()
|
||||
|
||||
if ip_filter.enabled and ip_address in ip_filter.blacklist_ips:
|
||||
|
||||
@@ -9,14 +9,13 @@ Usage:
|
||||
decorator `django.utils.decorators.decorator_from_middleware(middleware_class)`
|
||||
|
||||
"""
|
||||
|
||||
|
||||
import logging
|
||||
import geoip2.database
|
||||
|
||||
from django.conf import settings
|
||||
from django.utils.deprecation import MiddlewareMixin
|
||||
from ipware.ip import get_real_ip
|
||||
from ipware.ip import get_client_ip
|
||||
from ipware.utils import is_public_ip
|
||||
|
||||
log = logging.getLogger(__name__)
|
||||
|
||||
@@ -31,13 +30,13 @@ class CountryMiddleware(MiddlewareMixin):
|
||||
|
||||
Store country code in session.
|
||||
"""
|
||||
new_ip_address = get_real_ip(request)
|
||||
new_ip_address = get_client_ip(request)[0]
|
||||
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:
|
||||
elif new_ip_address != old_ip_address and is_public_ip(new_ip_address):
|
||||
reader = geoip2.database.Reader(settings.GEOIP_PATH)
|
||||
try:
|
||||
response = reader.country(new_ip_address)
|
||||
|
||||
@@ -107,20 +107,6 @@ class CountryMiddlewareTests(TestCase):
|
||||
assert 'CN' == request.session.get('country_code')
|
||||
assert '117.79.83.100' == request.session.get('ip_address')
|
||||
|
||||
def test_ip_address_is_none(self):
|
||||
# IP address is not defined in request.
|
||||
request = self.request_factory.get('/somewhere')
|
||||
request.user = self.anonymous_user
|
||||
# Run process_request to set up the session in the request
|
||||
# to be able to override it.
|
||||
self.session_middleware.process_request(request)
|
||||
request.session['country_code'] = 'CN'
|
||||
request.session['ip_address'] = '117.79.83.1'
|
||||
self.country_middleware.process_request(request)
|
||||
# No country code exists after request processing.
|
||||
assert 'country_code' not in request.session
|
||||
assert 'ip_address' not in request.session
|
||||
|
||||
def test_ip_address_is_ipv6(self):
|
||||
request = self.request_factory.get(
|
||||
'/somewhere',
|
||||
|
||||
@@ -1,15 +1,13 @@
|
||||
"""
|
||||
Code to get ip from request.
|
||||
"""
|
||||
|
||||
|
||||
from uuid import uuid4
|
||||
|
||||
from ipware.ip import get_ip
|
||||
from ipware.ip import get_client_ip
|
||||
|
||||
|
||||
def real_ip(group, request): # pylint: disable=unused-argument
|
||||
return get_ip(request)
|
||||
return get_client_ip(request)[0]
|
||||
|
||||
|
||||
def request_post_email(group, request) -> str: # pylint: disable=unused-argument
|
||||
|
||||
Reference in New Issue
Block a user