Skip to content

Commit

Permalink
Optimize multiple checks with caching (GH-35)
Browse files Browse the repository at this point in the history
  • Loading branch information
ArtyomVancyan authored Jun 20, 2023
2 parents 86439f5 + d3de6d1 commit 70b1239
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 6 deletions.
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ classifiers =
Framework :: Django :: 2.2
Framework :: Django :: 3.1
Framework :: Django :: 3.2
Framework :: Django :: 4.0
Framework :: Django :: 4.1
Framework :: Django :: 4.2
Programming Language :: Python
Programming Language :: Python :: 3
Programming Language :: Python :: 3.6
Expand Down
2 changes: 1 addition & 1 deletion src/django_forbid/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "0.1.5"
__version__ = "0.1.6"
11 changes: 8 additions & 3 deletions src/django_forbid/skills/forbid_device.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,22 +23,27 @@ def __call__(self, request):

devices = Settings.get("DEVICES", [])
device_type = request.session.get("DEVICE")
http_ua = request.META.get("HTTP_USER_AGENT")
verified_ua = request.session.get("VERIFIED_UA", "")

# Skip if DEVICES empty.
if not devices:
# Skips if DEVICES empty or user agent is verified.
if not devices or verified_ua == http_ua:
return self.get_response(request)

if not device_type:
http_ua = request.META.get("HTTP_USER_AGENT")
device_detector = DeviceDetector(http_ua)
device_detector = device_detector.parse()
device = device_detector.device_type()
device_type = device_aliases.get(device, device)
request.session["DEVICE"] = device_type

if Access(devices).grants(device_type):
request.session["VERIFIED_UA"] = http_ua
return self.get_response(request)

# Erases the user agent from the session.
request.session["VERIFIED_UA"] = ""

# Redirects to the FORBIDDEN_DEV URL if set.
if Settings.has("OPTIONS.URL.FORBIDDEN_DEV"):
return redirect(Settings.get("OPTIONS.URL.FORBIDDEN_DEV"))
Expand Down
3 changes: 2 additions & 1 deletion src/django_forbid/skills/forbid_location.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ def __call__(self, request):
client_ip = address.split(",")[0].strip()
verified_ip = request.session.get("VERIFIED_IP", "")

if verified_ip and verified_ip == client_ip:
# Skips if user IP is verified.
if verified_ip == client_ip:
return self.get_response(request)

try:
Expand Down

0 comments on commit 70b1239

Please sign in to comment.