From 15b7b65a688f49bf867fa250fcca5340ef4b6a77 Mon Sep 17 00:00:00 2001 From: Lorena Goldoni <33703137+Lorygold@users.noreply.github.com> Date: Thu, 7 Mar 2024 14:53:33 +0100 Subject: [PATCH] Revert "1.2.11" --- CHANGELOG.md | 4 ---- .../impossible_travel/modules/impossible_travel.py | 14 +++++++------- buffalogs/impossible_travel/tasks.py | 12 ++++-------- .../tests/test_impossible_travel.py | 1 - 4 files changed, 11 insertions(+), 20 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5e5b31a..c1be8c2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,8 +1,4 @@ ## 1.2.x -### 1.2.11 -#### Bugfix -* Fixed the update of the login.updated field -* Added logging for the clear_models_periodically function ### 1.2.10 #### Changes * Added settings into the Config model (instead of into the settings.py file) diff --git a/buffalogs/impossible_travel/modules/impossible_travel.py b/buffalogs/impossible_travel/modules/impossible_travel.py index 155b8a6..290f8c2 100644 --- a/buffalogs/impossible_travel/modules/impossible_travel.py +++ b/buffalogs/impossible_travel/modules/impossible_travel.py @@ -66,13 +66,13 @@ def update_model(self, db_user, new_login): :param new_user_agent: user_agent of last login :type new_user_agent: string """ - login = db_user.login_set.get(user_agent=new_login["agent"], country=new_login["country"], index=new_login["index"]) - login.timestamp = new_login["timestamp"] - login.latitude = new_login["lat"] - login.longitude = new_login["lon"] - login.event_id = new_login["id"] - login.ip = new_login["ip"] - login.save() + db_user.login_set.filter(user_agent=new_login["agent"], country=new_login["country"], index=new_login["index"]).update( + timestamp=new_login["timestamp"], + latitude=new_login["lat"], + longitude=new_login["lon"], + event_id=new_login["id"], + ip=new_login["ip"], + ) def add_new_login(self, db_user, new_login_field): """Add new login if there isn't previous login on db relative to that user diff --git a/buffalogs/impossible_travel/tasks.py b/buffalogs/impossible_travel/tasks.py index 1df1d4e..6e214f7 100644 --- a/buffalogs/impossible_travel/tasks.py +++ b/buffalogs/impossible_travel/tasks.py @@ -18,20 +18,16 @@ def clear_models_periodically(): app_config = Config.objects.get(id=1) now = timezone.now() delete_user_time = now - timedelta(days=app_config.user_max_days) - deleted_users, _ = User.objects.filter(updated__lte=delete_user_time).delete() - logger.info(f"Deleted {deleted_users} users for periodic cleaning") + User.objects.filter(updated__lte=delete_user_time).delete() delete_login_time = now - timedelta(days=app_config.login_max_days) - deleted_logins, _ = Login.objects.filter(updated__lte=delete_login_time).delete() - logger.info(f"Deleted {deleted_logins} logins for periodic cleaning") + Login.objects.filter(updated__lte=delete_login_time).delete() delete_alert_time = now - timedelta(days=app_config.alert_max_days) - deleted_alerts, _ = Alert.objects.filter(updated__lte=delete_alert_time).delete() - logger.info(f"Deleted {deleted_alerts} alerts for periodic cleaning") + Alert.objects.filter(updated__lte=delete_alert_time).delete() delete_ip_time = now - timedelta(days=app_config.ip_max_days) - deleted_usersIP, _ = UsersIP.objects.filter(updated__lte=delete_ip_time).delete() - logger.info(f"Deleted {deleted_usersIP} usersIP for periodic cleaning") + UsersIP.objects.filter(updated__lte=delete_ip_time).delete() @shared_task(name="BuffalogsUpdateRiskLevelTask") diff --git a/buffalogs/impossible_travel/tests/test_impossible_travel.py b/buffalogs/impossible_travel/tests/test_impossible_travel.py index 72ba2da..c6116c2 100644 --- a/buffalogs/impossible_travel/tests/test_impossible_travel.py +++ b/buffalogs/impossible_travel/tests/test_impossible_travel.py @@ -88,7 +88,6 @@ def test_update_model(self): self.assertEqual(new_login_db.latitude, new_login_fields["lat"]) self.assertEqual(new_login_db.longitude, new_login_fields["lon"]) self.assertEqual(new_login_db.timestamp, new_login_fields["timestamp"]) - self.assertLessEqual((timezone.now() - new_login_db.updated).seconds, 1) def test_add_new_login(self): new_time = timezone.now()