Skip to content

Commit b1b697a

Browse files
Performance optimization (GH-30)
2 parents c266c71 + 15f439f commit b1b697a

File tree

6 files changed

+37
-26
lines changed

6 files changed

+37
-26
lines changed

src/django_forbid/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
__version__ = "0.1.2"
1+
__version__ = "0.1.3"

src/django_forbid/middleware.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import re
2+
13
from .skills.forbid_device import ForbidDeviceMiddleware
24
from .skills.forbid_location import ForbidLocationMiddleware
35
from .skills.forbid_network import ForbidNetworkMiddleware
@@ -14,8 +16,11 @@ class ForbidMiddleware:
1416

1517
def __init__(self, get_response):
1618
self.get_response = get_response
19+
self.regex = re.compile(r"\w+/(?:html|xhtml\+xml|xml)")
1720

1821
def __call__(self, request):
19-
for skill in __skills__:
20-
self.get_response = skill(self.get_response)
21-
return self.get_response(request)
22+
get_response = self.get_response
23+
if self.regex.search(request.META.get("HTTP_ACCEPT")):
24+
for skill in __skills__:
25+
get_response = skill(get_response)
26+
return get_response(request)

src/django_forbid/skills/forbid_location.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ def __call__(self, request):
2020
address = request.META.get("REMOTE_ADDR")
2121
address = request.META.get("HTTP_X_FORWARDED_FOR", address)
2222
client_ip = address.split(",")[0].strip()
23+
verified_ip = request.session.get("VERIFIED_IP", "")
24+
25+
if verified_ip and verified_ip == client_ip:
26+
return self.get_response(request)
2327

2428
try:
2529
city = geoip.city(client_ip)
@@ -47,8 +51,12 @@ def __call__(self, request):
4751
request.session["GEOIP2_TZ"] = timezone
4852

4953
if granted:
54+
request.session["VERIFIED_IP"] = client_ip
5055
return self.get_response(request)
5156

57+
# Erases the timezone from the session.
58+
request.session["VERIFIED_IP"] = ""
59+
5260
# Redirects to the FORBIDDEN_LOC URL if set.
5361
if Settings.has("OPTIONS.URL.FORBIDDEN_LOC"):
5462
return redirect(Settings.get("OPTIONS.URL.FORBIDDEN_LOC"))

src/django_forbid/skills/forbid_network.py

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import json
2-
import re
32

43
from django.http import HttpResponse
54
from django.http import HttpResponseForbidden
@@ -31,20 +30,11 @@ def forbidden_page():
3130
geoip2_tz = request.session.get("GEOIP2_TZ")
3231
verified_tz = request.session.get("VERIFIED_TZ", "")
3332

34-
if any([
35-
verified_tz == geoip2_tz,
36-
# Checks if VPN is False or not set.
37-
not Settings.get("OPTIONS.VPN", False),
38-
# Checks if the request is an AJAX request.
39-
not re.search(
40-
r"\w+\/(?:html|xhtml\+xml|xml)",
41-
request.META.get("HTTP_ACCEPT"),
42-
),
43-
]):
33+
# Checks if the user's timezone match with the last accessed one.
34+
if verified_tz == geoip2_tz or not Settings.get("OPTIONS.VPN", False):
4435
return self.get_response(request)
45-
4636
# Checks if GEOIP2_TZ and VERIFIED_TZ don't exist.
47-
if all([verified_tz, geoip2_tz != "N/A"]):
37+
elif verified_tz and geoip2_tz != "N/A":
4838
return forbidden_page()
4939

5040
if all(map(request.session.has_key, ("GEOIP2_TZ", *response_attributes))):

tests/test_network_middleware.py

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
from tests import WSGIRequest
77

88

9-
def skips(get_response, ip_address, ajax=False):
10-
detector = Detector(get_response, ajax=ajax)
9+
def skips(get_response, ip_address):
10+
detector = Detector(get_response)
1111
response = detector.request_resource(ip_address)
1212
return response.status_code == 200
1313

@@ -84,10 +84,3 @@ def test_should_allow_users_only_from_great_britain_with_shared_session(get_resp
8484
assert forbids_shared_session(detector, IP.ip_cobain)
8585
# Turn off VPN - back to London
8686
assert not forbids_shared_session(detector, IP.ip_london)
87-
88-
89-
@override_settings(DJANGO_FORBID={"OPTIONS": {"VPN": True}})
90-
def test_should_allow_ajax_requests(get_response):
91-
"""It should give access to the user when request is done by AJAX"""
92-
for ip_address in IP.all:
93-
assert skips(get_response, ip_address, True)

tests/test_primary_middleware.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,14 @@
99
request = wsgi.get()
1010

1111

12+
def skips_ajax(get_response, ip_address):
13+
wsgi_ajax = WSGIRequest(True)
14+
request_ajax = wsgi_ajax.get()
15+
request_ajax.META["HTTP_X_FORWARDED_FOR"] = ip_address
16+
response = ForbidMiddleware(get_response)(request_ajax)
17+
return response.status_code == 200
18+
19+
1220
def forbids(get_response, request):
1321
response = ForbidMiddleware(get_response)(request)
1422
client_ip = request.META["HTTP_X_FORWARDED_FOR"]
@@ -80,3 +88,10 @@ def test_should_allow_users_only_from_great_britain_with_shared_session(get_resp
8088
# Turn off VPN - back to London
8189
request.META["HTTP_X_FORWARDED_FOR"] = IP.ip_london
8290
assert not forbids(get_response, request)
91+
92+
93+
@override_settings(DJANGO_FORBID={"OPTIONS": {"VPN": True}})
94+
def test_should_allow_ajax_requests(get_response):
95+
"""It should give access to the user when request is done by AJAX"""
96+
for ip_address in IP.all:
97+
assert skips_ajax(get_response, ip_address)

0 commit comments

Comments
 (0)