From 743e8d314a9daa01442a92577a58ab7384f59020 Mon Sep 17 00:00:00 2001 From: Thomas Rusiecki Date: Wed, 8 Jan 2025 17:11:33 -0300 Subject: [PATCH] added logging for evidence tag changes --- evidence/forms.py | 35 ++++++++++++++++++++---------- evidence/templates/ev_details.html | 2 +- evidence/urls.py | 1 + evidence/views.py | 33 ++++++++++++++++++++++++++++ 4 files changed, 59 insertions(+), 12 deletions(-) diff --git a/evidence/forms.py b/evidence/forms.py index 0e0a89a..baae3de 100644 --- a/evidence/forms.py +++ b/evidence/forms.py @@ -10,6 +10,7 @@ from device.models import Device from evidence.parse import Build from evidence.models import SystemProperty, UserProperty from utils.save_snapshots import move_json, save_in_disk +from action.models import DeviceLog class UploadForm(forms.Form): @@ -98,19 +99,31 @@ class UserTagForm(forms.Form): return if self.instance: + old_value = self.instance.value if not self.tag: + message =_(" Evidence Tag. Old Value: '{}'").format(old_value) self.instance.delete() - self.instance.value = self.tag - self.instance.save() - return - - SystemProperty.objects.create( - uuid=self.uuid, - key='CUSTOM_ID', - value=self.tag, - owner=self.user.institution, - user=self.user - ) + else: + self.instance.value = self.tag + self.instance.save() + if old_value != self.tag: + message=_(" Evidence Tag. Old Value: '{}'. New Value: '{}'").format(old_value, self.tag) + else: + message =_(" Evidence Tag. Value: '{}'").format(self.tag) + SystemProperty.objects.create( + uuid=self.uuid, + key='CUSTOM_ID', + value=self.tag, + owner=self.user.institution, + user=self.user + ) + + DeviceLog.objects.create( + snapshot_uuid=self.uuid, + event= message, + user=self.user, + institution=self.user.institution + ) class ImportForm(forms.Form): diff --git a/evidence/templates/ev_details.html b/evidence/templates/ev_details.html index 4442af1..dee5c63 100644 --- a/evidence/templates/ev_details.html +++ b/evidence/templates/ev_details.html @@ -94,7 +94,7 @@ {% if form.tag.value %} {% endif %} diff --git a/evidence/urls.py b/evidence/urls.py index 0d03f61..c8eea40 100644 --- a/evidence/urls.py +++ b/evidence/urls.py @@ -20,4 +20,5 @@ urlpatterns = [ path("", views.EvidenceView.as_view(), name="details"), path("/eraseserver", views.EraseServerView.as_view(), name="erase_server"), path("/download", views.DownloadEvidenceView.as_view(), name="download"), + path("tag//delete", views.DeleteEvidenceTagView.as_view(), name="delete_tag"), ] diff --git a/evidence/views.py b/evidence/views.py index 5cab1ae..2d1d338 100644 --- a/evidence/views.py +++ b/evidence/views.py @@ -12,6 +12,7 @@ from django.views.generic.edit import ( FormView, ) +from action.models import DeviceLog from dashboard.mixins import DashboardView, Http403 from evidence.models import SystemProperty, UserProperty, Evidence from evidence.forms import ( @@ -184,3 +185,35 @@ class EraseServerView(DashboardView, FormView): def get_success_url(self): success_url = reverse_lazy('evidence:details', args=[self.pk]) return success_url + + +class DeleteEvidenceTagView(DashboardView, DeleteView): + model = SystemProperty + + def get_queryset(self): + # only those with 'CUSTOM_ID' + return SystemProperty.objects.filter(owner=self.request.user.institution, key='CUSTOM_ID') + + def get(self, request, *args, **kwargs): + self.object = self.get_object() + + message = _(" Evidence Tag: {}").format(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() + + messages.info(self.request, _("Evicende Tag deleted successfully.")) + return self.handle_success() + + def handle_success(self): + return redirect(self.get_success_url()) + + def get_success_url(self): + return self.request.META.get( + 'HTTP_REFERER', + reverse_lazy('evidence:details', args=[self.object.uuid]) + )