From ab7d9133fa795e3e5576e8cdb1477bb7e8bc606b Mon Sep 17 00:00:00 2001 From: Artyom Vancyan Date: Sun, 18 Jun 2023 15:03:10 +0400 Subject: [PATCH 1/4] Cache user agent for better performance --- src/django_forbid/skills/forbid_device.py | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/src/django_forbid/skills/forbid_device.py b/src/django_forbid/skills/forbid_device.py index c6fc6e7..3608aec 100644 --- a/src/django_forbid/skills/forbid_device.py +++ b/src/django_forbid/skills/forbid_device.py @@ -23,13 +23,14 @@ 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() @@ -37,8 +38,12 @@ def __call__(self, request): 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")) From 0e415cbc3da2b62b768d716a999f8050bf52b90f Mon Sep 17 00:00:00 2001 From: Artyom Vancyan Date: Sun, 18 Jun 2023 15:08:54 +0400 Subject: [PATCH 2/4] Remove unnecessary check --- src/django_forbid/skills/forbid_location.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/django_forbid/skills/forbid_location.py b/src/django_forbid/skills/forbid_location.py index 4fdb413..7a5f7e5 100644 --- a/src/django_forbid/skills/forbid_location.py +++ b/src/django_forbid/skills/forbid_location.py @@ -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: From e1ae074ab9103301becd4ec44b1b1740ec736cfe Mon Sep 17 00:00:00 2001 From: Artyom Vancyan Date: Sun, 18 Jun 2023 15:10:01 +0400 Subject: [PATCH 3/4] Upgrade the version to `0.1.6` --- src/django_forbid/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/django_forbid/__init__.py b/src/django_forbid/__init__.py index 1276d02..0a8da88 100644 --- a/src/django_forbid/__init__.py +++ b/src/django_forbid/__init__.py @@ -1 +1 @@ -__version__ = "0.1.5" +__version__ = "0.1.6" From d3de6d16f19d97c6ea3d27cadfd340f12a012498 Mon Sep 17 00:00:00 2001 From: Artyom Vancyan Date: Sun, 18 Jun 2023 15:14:14 +0400 Subject: [PATCH 4/4] Update Django version classifiers --- setup.cfg | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/setup.cfg b/setup.cfg index e9723da..6250003 100644 --- a/setup.cfg +++ b/setup.cfg @@ -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