Start implement user status logging

This commit is contained in:
Kumi 2019-01-31 19:58:08 +00:00
parent 3af6677da0
commit 7476c08529
4 changed files with 61 additions and 2 deletions

View file

@ -0,0 +1,29 @@
# Generated by Django 2.1.5 on 2019-01-31 19:51
from django.conf import settings
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
dependencies = [
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
('manager', '0039_auto_20190124_1023'),
]
operations = [
migrations.CreateModel(
name='UserStatus',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('last_action', models.DateTimeField(blank=True, null=True)),
('user', models.OneToOneField(on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL)),
],
),
migrations.AlterField(
model_name='device',
name='wifi',
field=models.ManyToManyField(blank=True, to='manager.Wifi'),
),
]

View file

@ -1,6 +1,5 @@
from django.db import models
from django.conf import settings
import uuid
def getRandom():
return str(uuid.uuid4().hex)
@ -162,3 +161,8 @@ class Notification(models.Model):
text = models.CharField("Notification Text", max_length=1024)
expiry = models.DateTimeField("Notification Expiry", null=True, blank=True)
on_login = models.BooleanField("Display Notification on Login Screen", default=False)
class UserStatus(models.Model):
user = models.OneToOneField(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
last_action = models.DateTimeField(null=True, blank=True)

View file

@ -0,0 +1,13 @@
from django import template
from django.conf import settings
from manager.models import UserStatus
from django.utils import timezone
register = template.Library()
@register.simple_tag
def updateUserStatus(user):
try:
UserStatus.objects.filter(user=user)[0].last_action = timezone.now()
except:
pass

View file

@ -8,8 +8,11 @@ from django.core.files import File
from django.db.models.fields.files import FieldFile
from django.contrib.auth.models import User
from django.contrib.auth.decorators import login_required, user_passes_test
from django.db.models.signals import post_save
from django.dispatch import receiver
from django.conf import settings
from .models import Device, Organization, Network, Model, Wifi
from .models import Device, Organization, Network, Model, Wifi, UserStatus
from .signals import *
from distutils.dir_util import copy_tree
@ -587,3 +590,13 @@ def makedevice(request):
)
return redirect("/")
@receiver(post_save, sender=settings.AUTH_USER_MODEL)
def createUserStatus(sender, instance, created, **kwargs):
if created:
UserStatus.objects.create(user=instance)
@receiver(post_save, sender=settings.AUTH_USER_MODEL)
def saveUserStatus(sender, instance, **kwargs):
instance.profile.save()