Add missing files for device logging
This commit is contained in:
parent
0a9cca30cb
commit
0c8b87bb00
5 changed files with 145 additions and 0 deletions
36
manager/logger.py
Normal file
36
manager/logger.py
Normal file
|
@ -0,0 +1,36 @@
|
|||
from django.dispatch import receiver
|
||||
from .signals import siglog
|
||||
|
||||
from .models import Device, Organization, Network, Model, Wifi, DeviceLog
|
||||
|
||||
@receiver(siglog)
|
||||
def logger(sender, **kwargs):
|
||||
if sender == Device:
|
||||
logger = DeviceLog
|
||||
else:
|
||||
return
|
||||
|
||||
logger.objects.create(
|
||||
objid=kwargs["id"],
|
||||
user=kwargs["user"],
|
||||
action=kwargs["action"],
|
||||
oldvalue=kwargs["oldvalue"],
|
||||
newvalue=kwargs["newvalue"]
|
||||
)
|
||||
|
||||
# device = models.CharField("Device ID", max_length=64)
|
||||
# user = models.CharField("User Name", max_length=128, null=True)
|
||||
# date = models.DateTimeField("Action Date", auto_now_add=True)
|
||||
# action = models.IntegerField("Action", choices=ACTION_CHOICES)
|
||||
# oldvalue = models.CharField("Old Value", max_length=256, blank=True, null=True)
|
||||
# newvalue = models.CharField("New Value", max_length=256, blank=True, null=True)
|
||||
|
||||
# ADD = 0
|
||||
# REMOVE = 1
|
||||
# NAME = 2
|
||||
# NET = 3
|
||||
# REBOOT = 4
|
||||
# UPDATE = 5
|
||||
# STATUS = 6
|
||||
# INTERNALIP = 7
|
||||
# WIFI = 8
|
30
manager/migrations/0033_auto_20190118_0846.py
Normal file
30
manager/migrations/0033_auto_20190118_0846.py
Normal file
|
@ -0,0 +1,30 @@
|
|||
# Generated by Django 2.1.5 on 2019-01-18 08:46
|
||||
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('manager', '0032_auto_20190117_1039'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.CreateModel(
|
||||
name='DeviceLog',
|
||||
fields=[
|
||||
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
|
||||
('device', models.CharField(max_length=64, verbose_name='Device ID')),
|
||||
('user', models.CharField(max_length=128, null=True, verbose_name='User Name')),
|
||||
('date', models.DateTimeField(auto_now_add=True, verbose_name='Action Date')),
|
||||
('action', models.IntegerField(choices=[(0, 'ADD'), (1, 'REMOVE'), (2, 'NAME'), (3, 'NET'), (4, 'REBOOT'), (5, 'UPDATE'), (6, 'STATUS'), (7, 'INTERNALIP')], verbose_name='Action')),
|
||||
('oldvalue', models.CharField(blank=True, max_length=256, null=True, verbose_name='Old Value')),
|
||||
('newvalue', models.CharField(blank=True, max_length=256, null=True, verbose_name='New Value')),
|
||||
],
|
||||
),
|
||||
migrations.AlterField(
|
||||
model_name='device',
|
||||
name='wifi',
|
||||
field=models.ManyToManyField(blank=True, to='manager.Wifi'),
|
||||
),
|
||||
]
|
23
manager/migrations/0034_auto_20190118_0937.py
Normal file
23
manager/migrations/0034_auto_20190118_0937.py
Normal file
|
@ -0,0 +1,23 @@
|
|||
# Generated by Django 2.1.5 on 2019-01-18 09:37
|
||||
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('manager', '0033_auto_20190118_0846'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AlterField(
|
||||
model_name='device',
|
||||
name='wifi',
|
||||
field=models.ManyToManyField(blank=True, to='manager.Wifi'),
|
||||
),
|
||||
migrations.AlterField(
|
||||
model_name='devicelog',
|
||||
name='action',
|
||||
field=models.IntegerField(choices=[(0, 'ADD'), (1, 'REMOVE'), (2, 'NAME'), (3, 'NET'), (4, 'REBOOT'), (5, 'UPDATE'), (6, 'STATUS'), (7, 'INTERNALIP'), (8, 'WIFI')], verbose_name='Action'),
|
||||
),
|
||||
]
|
23
manager/migrations/0035_auto_20190118_1219.py
Normal file
23
manager/migrations/0035_auto_20190118_1219.py
Normal file
|
@ -0,0 +1,23 @@
|
|||
# Generated by Django 2.1.5 on 2019-01-18 12:19
|
||||
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('manager', '0034_auto_20190118_0937'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.RenameField(
|
||||
model_name='devicelog',
|
||||
old_name='device',
|
||||
new_name='objid',
|
||||
),
|
||||
migrations.AlterField(
|
||||
model_name='device',
|
||||
name='wifi',
|
||||
field=models.ManyToManyField(blank=True, to='manager.Wifi'),
|
||||
),
|
||||
]
|
33
manager/signals.py
Normal file
33
manager/signals.py
Normal file
|
@ -0,0 +1,33 @@
|
|||
from django.dispatch import Signal
|
||||
from .models import Device, Organization, Network, Model, Wifi, DeviceLog
|
||||
|
||||
siglog = Signal(providing_args=["id", "user", "action", "oldvalue", "newvalue"])
|
||||
|
||||
def sigDevice(device_id, user, action, oldvalue=None, newvalue=None):
|
||||
siglog.send(sender=Device, id=device_id, user=user, action=action, oldvalue=oldvalue, newvalue=newvalue)
|
||||
|
||||
def sigDeleteDevice(device_id, user):
|
||||
sigDevice(device_id, user, DeviceLog.DELETE)
|
||||
|
||||
def sigAddDevice(device_id, user):
|
||||
sigDevice(device_id, user, DeviceLog.ADD)
|
||||
|
||||
def sigRenameDevice(device_id, user, oldname, newname):
|
||||
sigDevice(device_id, user, DeviceLog.NAME, oldname, newname)
|
||||
|
||||
def sigNetDevice(device_id, user, oldnet, newnet):
|
||||
sigDevice(device_id, user, DeviceLog.NET, oldnet, newnet)
|
||||
|
||||
def sigWifiDevice(device_id, user, oldwifis, newwifis):
|
||||
for old in oldwifis:
|
||||
if old not in newwifis:
|
||||
sigDevice(device_id, user, DeviceLog.WIFI, str(old))
|
||||
for new in newwifis:
|
||||
if new not in oldwifis:
|
||||
sigDevice(device_id, user, DeviceLog.WIFI, None, str(new))
|
||||
|
||||
def sigRebootDevice(device_id, user, newstate):
|
||||
sigDevice(device_id, user, DeviceLog.REBOOT, str(not newstate), str(newstate))
|
||||
|
||||
def sigUpdateDevice(device_id, user, newstate):
|
||||
sigDevice(device_id, user, DeviceLog.UPDATE, str(not newstate), str(newstate))
|
Loading…
Reference in a new issue