Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed login.updated field #99

Merged
merged 5 commits into from
Mar 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
14 changes: 7 additions & 7 deletions buffalogs/impossible_travel/modules/impossible_travel.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 8 additions & 4 deletions buffalogs/impossible_travel/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
Loading