See #212: record user last activity date

This commit is contained in:
Eliot Berriot 2018-06-17 17:53:40 +02:00
parent 307959dfa6
commit 2e4f862387
No known key found for this signature in database
GPG key ID: DD6965E2476E5C27
7 changed files with 93 additions and 0 deletions

View file

@ -2,6 +2,7 @@
from __future__ import absolute_import, unicode_literals
import binascii
import datetime
import os
import uuid
@ -9,6 +10,7 @@ from django.conf import settings
from django.contrib.auth.models import AbstractUser
from django.db import models
from django.urls import reverse
from django.utils import timezone
from django.utils.encoding import python_2_unicode_compatible
from django.utils.translation import ugettext_lazy as _
@ -75,6 +77,8 @@ class User(AbstractUser):
default=False,
)
last_activity = models.DateTimeField(default=None, null=True, blank=True)
def __str__(self):
return self.username
@ -117,3 +121,16 @@ class User(AbstractUser):
def get_activity_url(self):
return settings.FUNKWHALE_URL + "/@{}".format(self.username)
def record_activity(self):
"""
Simply update the last_activity field if current value is too old
than a threshold. This is useful to keep a track of inactive accounts.
"""
current = self.last_activity
delay = 60 * 15 # fifteen minutes
now = timezone.now()
if current is None or current < now - datetime.timedelta(seconds=delay):
self.last_activity = now
self.save(update_fields=["last_activity"])