added logging for states, user properties and notes

This commit is contained in:
Thomas Nahuel Rusiecki 2024-12-19 20:09:29 -03:00
parent 12711c2d5f
commit 7d3b448d2a
3 changed files with 43 additions and 24 deletions

View file

@ -5,14 +5,11 @@ from action.forms import ChangeStateForm, AddNoteForm
from django.views.generic.edit import DeleteView, CreateView, FormView from django.views.generic.edit import DeleteView, CreateView, FormView
from django.urls import reverse_lazy from django.urls import reverse_lazy
from django.utils.translation import gettext_lazy as _ from django.utils.translation import gettext_lazy as _
from action.models import State, StateDefinition, Note from action.models import State, StateDefinition, Note, DeviceLog
from device.models import Device from device.models import Device
import logging import logging
device_logger = logging.getLogger('device_log')
class ChangeStateView(View): class ChangeStateView(View):
def post(self, request, *args, **kwargs): def post(self, request, *args, **kwargs):
@ -30,9 +27,13 @@ class ChangeStateView(View):
institution=self.request.user.institution, institution=self.request.user.institution,
) )
device_logger.info(f"<Updated> State to '{new_state}', from '{previous_state}' ) by user {self.request.user}.")
message = _("State changed from '{}' to '{}'.".format(previous_state, new_state) ) message = _("State changed from '{}' to '{}'.".format(previous_state, new_state) )
DeviceLog.objects.create(
snapshot_uuid=snapshot_uuid,
event=message,
user=self.request.user,
institution=self.request.user.institution,
)
messages.success(request,message) messages.success(request,message)
else: else:
messages.error(request, "There was an error with your submission.") messages.error(request, "There was an error with your submission.")
@ -48,9 +49,7 @@ class UndoStateView(DeleteView):
return super().delete(request, *args, **kwarg) return super().delete(request, *args, **kwarg)
def get_success_url(self): def get_success_url(self):
messages.info(self.request, f"Action to state: {self.object.state} has been deleted.") messages.info(self.request, f"Action to state: {self.object.state} has been deleted.")
device_logger.info(f"<Deleted> State '{self.object.state}', for device '{self.object.snapshot_uuid}') by user {self.request.user}.")
return self.request.META.get('HTTP_REFERER', reverse_lazy('device:details', args=[self.object.snapshot_uuid])) return self.request.META.get('HTTP_REFERER', reverse_lazy('device:details', args=[self.object.snapshot_uuid]))
@ -68,6 +67,14 @@ class AddNoteView(View):
user=self.request.user, user=self.request.user,
institution=self.request.user.institution, institution=self.request.user.institution,
) )
message = _("Note: '{}' ".format(note) )
DeviceLog.objects.create(
snapshot_uuid=snapshot_uuid,
event=message,
user=self.request.user,
institution=self.request.user.institution,
)
messages.success(request, _("Note has been added")) messages.success(request, _("Note has been added"))
else: else:
messages.error(request, "There was an error with your submission.") messages.error(request, "There was an error with your submission.")

View file

@ -65,7 +65,7 @@
<a href="#log" class="nav-link" data-bs-toggle="tab" data-bs-target="#log">{% trans 'Log' %}</a> <a href="#log" class="nav-link" data-bs-toggle="tab" data-bs-target="#log">{% trans 'Log' %}</a>
</li> </li>
<li class="nav-item"> <li class="nav-item">
<a href="#user_prope rties" class="nav-link" data-bs-toggle="tab" data-bs-target="#user_properties">{% trans 'User properties' %}</a> <a href="#user_properties" class="nav-link" data-bs-toggle="tab" data-bs-target="#user_properties">{% trans 'User properties' %}</a>
</li> </li>
<li class="nav-item"> <li class="nav-item">
<a href="#documents" class="nav-link" data-bs-toggle="tab" data-bs-target="#documents">{% trans 'Documents' %}</a> <a href="#documents" class="nav-link" data-bs-toggle="tab" data-bs-target="#documents">{% trans 'Documents' %}</a>

View file

@ -14,7 +14,7 @@ from django.views.generic.edit import (
DeleteView, DeleteView,
) )
from django.views.generic.base import TemplateView from django.views.generic.base import TemplateView
from action.models import StateDefinition, State from action.models import StateDefinition, State, DeviceLog
from dashboard.mixins import DashboardView, Http403 from dashboard.mixins import DashboardView, Http403
from evidence.models import UserProperty, SystemProperty from evidence.models import UserProperty, SystemProperty
from lot.models import LotTag from lot.models import LotTag
@ -22,8 +22,6 @@ from device.models import Device
from device.forms import DeviceFormSet from device.forms import DeviceFormSet
device_logger = logging.getLogger('device_log')
class NewDeviceView(DashboardView, FormView): class NewDeviceView(DashboardView, FormView):
template_name = "new_device.html" template_name = "new_device.html"
title = _("New Device") title = _("New Device")
@ -117,6 +115,7 @@ class DetailsView(DashboardView, TemplateView):
'lot_tags': lot_tags, 'lot_tags': lot_tags,
"state_definitions": StateDefinition.objects.filter(institution=self.request.user.institution).order_by('order'), "state_definitions": StateDefinition.objects.filter(institution=self.request.user.institution).order_by('order'),
"device_states": State.objects.filter(snapshot_uuid=uuid).order_by('-date'), "device_states": State.objects.filter(snapshot_uuid=uuid).order_by('-date'),
"device_logs": DeviceLog.objects.filter(snapshot_uuid=uuid).order_by('-date'),
}) })
return context return context
@ -191,12 +190,15 @@ class AddUserPropertyView(DashboardView, CreateView):
form.instance.uuid = self.property.uuid form.instance.uuid = self.property.uuid
form.instance.type = UserProperty.Type.USER form.instance.type = UserProperty.Type.USER
messages.success(self.request, _("User property successfully added.")) message = _("New annotation: {}: {}".format(form.instance.key, form.instance.value))
DeviceLog.objects.create(
device_logger.info( snapshot_uuid=form.instance.uuid,
f"Created user property (key='{form.instance.key}', value='{form.instance.value}') by user {self.request.user}, for evidence uuid: {self.property.uuid}." event=message,
user=self.request.user,
institution=self.request.user.institution
) )
messages.success(self.request, _("User property successfully added."))
response = super().form_valid(form) response = super().form_valid(form)
return response return response
@ -243,10 +245,14 @@ class UpdateUserPropertyView(DashboardView, UpdateView):
form.instance.user = self.request.user form.instance.user = self.request.user
form.instance.type = UserProperty.Type.USER form.instance.type = UserProperty.Type.USER
response = super().form_valid(form) response = super().form_valid(form)
messages.success(self.request, _("User property updated successfully.")) messages.success(self.request, _("User property updated successfully."))
device_logger.info(
f"Updated property from (key='{old_key}', value='{old_value}') to (key='{new_key}', value='{new_value}') by user {self.request.user}." message = _("Update annotation: {}: {} to {}:{}".format(old_key, old_value, new_key, new_value ))
DeviceLog.objects.create(
snapshot_uuid=form.instance.uuid,
event=message,
user=self.request.user,
institution=self.request.user.institution
) )
return response return response
@ -268,10 +274,16 @@ class DeleteUserPropertyView(DashboardView, DeleteView):
pk=self.pk, pk=self.pk,
owner=self.request.user.institution owner=self.request.user.institution
) )
old_value = self.object.key message = _("deleted annotation: {}:{}".format(self.object.key, self.object.value ))
DeviceLog.objects.create(
snapshot_uuid=self.object.uuid,
event=message,
user=self.request.user,
institution=self.request.user.institution
)
self.object.delete() self.object.delete()
device_logger.info(f"Deleted property with key '{old_value}' by user {self.request.user}.") messages.info(self.request, _("User property deleted successfully."))
messages.success(self.request, _("User property deleted successfully."))
# Redirect back to the original URL # Redirect back to the original URL
return redirect(referer) return redirect(referer)