From e00c804a5a0a919084fbae5d02a510acf6b1a47a Mon Sep 17 00:00:00 2001 From: Bai Date: Mon, 9 Oct 2023 14:39:13 +0800 Subject: [PATCH] =?UTF-8?q?perf:=20=E4=BC=98=E5=8C=96=E6=A0=A1=E9=AA=8C?= =?UTF-8?q?=E7=99=BB=E5=BD=95=E5=9F=8E=E5=B8=82=E5=90=8D1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apps/authentication/utils.py | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/apps/authentication/utils.py b/apps/authentication/utils.py index b53afa423..9cbc95bf2 100644 --- a/apps/authentication/utils.py +++ b/apps/authentication/utils.py @@ -20,19 +20,22 @@ def check_different_city_login_if_need(user, request): return ip = get_request_ip(request) or '0.0.0.0' - if not (ip and validate_ip(ip)): - city = DEFAULT_CITY - else: - city = get_ip_city(ip) or DEFAULT_CITY - city_white = [_('LAN'), 'LAN'] is_private = ipaddress.ip_address(ip).is_private - if not is_private: - last_user_login = UserLoginLog.objects.exclude(city__in=city_white) \ - .filter(username=user.username, status=True).first() + if is_private: + return + last_user_login = UserLoginLog.objects.exclude( + city__in=city_white + ).filter(username=user.username, status=True).first() + if not last_user_login: + return - if last_user_login and last_user_login.city != city: - DifferentCityLoginMessage(user, ip, city).publish_async() + city = get_ip_city(ip) + last_city = get_ip_city(last_user_login.ip) + if city == last_city: + return + + DifferentCityLoginMessage(user, ip, city).publish_async() def build_absolute_uri(request, path=None):