From f7da3f036e2e133897adb91257d5f47f67ec9fa2 Mon Sep 17 00:00:00 2001 From: Lorena Goldoni <33703137+Lorygold@users.noreply.github.com> Date: Thu, 7 Mar 2024 13:00:02 +0100 Subject: [PATCH] Fixed login.updated field (#99) * Fixed the update of the login.updated field * Added logging to the clear_models_periodically function * Added tests on login.updated field * Updated CHANGELOG.md * Updated check on login.updated field --- 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, 20 insertions(+), 11 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c1be8c2..5e5b31a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,4 +1,8 @@ ## 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 290f8c2..155b8a6 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 """ - 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"], - ) + 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() 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 6e214f7..1df1d4e 100644 --- a/buffalogs/impossible_travel/tasks.py +++ b/buffalogs/impossible_travel/tasks.py @@ -18,16 +18,20 @@ 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) - User.objects.filter(updated__lte=delete_user_time).delete() + deleted_users, _ = User.objects.filter(updated__lte=delete_user_time).delete() + logger.info(f"Deleted {deleted_users} users for periodic cleaning") delete_login_time = now - timedelta(days=app_config.login_max_days) - Login.objects.filter(updated__lte=delete_login_time).delete() + deleted_logins, _ = Login.objects.filter(updated__lte=delete_login_time).delete() + logger.info(f"Deleted {deleted_logins} logins for periodic cleaning") delete_alert_time = now - timedelta(days=app_config.alert_max_days) - Alert.objects.filter(updated__lte=delete_alert_time).delete() + deleted_alerts, _ = Alert.objects.filter(updated__lte=delete_alert_time).delete() + logger.info(f"Deleted {deleted_alerts} alerts for periodic cleaning") delete_ip_time = now - timedelta(days=app_config.ip_max_days) - UsersIP.objects.filter(updated__lte=delete_ip_time).delete() + deleted_usersIP, _ = UsersIP.objects.filter(updated__lte=delete_ip_time).delete() + logger.info(f"Deleted {deleted_usersIP} usersIP for periodic cleaning") @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 c6116c2..72ba2da 100644 --- a/buffalogs/impossible_travel/tests/test_impossible_travel.py +++ b/buffalogs/impossible_travel/tests/test_impossible_travel.py @@ -88,6 +88,7 @@ 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()