added logging for device operations

This commit is contained in:
Thomas Nahuel Rusiecki 2024-11-16 15:26:45 -03:00
parent 95fb09bd69
commit db1d38c3d1
2 changed files with 34 additions and 8 deletions

View File

@ -1,4 +1,5 @@
import json
import logging
from django.http import JsonResponse
from django.http import Http404
@ -20,6 +21,8 @@ from device.models import Device
from device.forms import DeviceFormSet
device_logger = logging.getLogger('device_log')
class NewDeviceView(DashboardView, FormView):
template_name = "new_device.html"
title = _("New Device")
@ -182,13 +185,15 @@ class AddUserPropertyView(DashboardView, CreateView):
form.instance.user = self.request.user
form.instance.uuid = self.property.uuid
form.instance.type = Property.Type.USER
messages.success(self.request, _("User property successfully added."))
response = super().form_valid(form)
return response
def get_form_kwargs(self):
pk = self.kwargs.get('pk')
institution = self.request.user.institution
self.property = UserProperty.objects.filter(
self.property = SystemProperty.objects.filter(
owner=institution,
value=pk,
type=Property.Type.SYSTEM
@ -220,20 +225,24 @@ class UpdateUserPropertyView(DashboardView, UpdateView):
return kwargs
def form_valid(self, form):
old_key= self.object.key
old_value = self.object.value
new_key = form.cleaned_data['key']
new_value = form.cleaned_data['value']
form.instance.owner = self.request.user.institution
form.instance.user = self.request.user
form.instance.type = Property.Type.USER
response = super().form_valid(form)
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}."
)
return response
def get_success_url(self):
referer = self.request.META.get('HTTP_REFERER')
if referer:
return referer
else:
return reverse_lazy('device:details', args=[self.object.device.pk])
return self.request.META.get('HTTP_REFERER', reverse_lazy('device:details', args=[self.object.pk]))
class DeleteUserPropertyView(DashboardView, DeleteView):
@ -250,7 +259,9 @@ class DeleteUserPropertyView(DashboardView, DeleteView):
pk=self.pk,
owner=self.request.user.institution
)
old_value = self.object.key
self.object.delete()
device_logger.info(f"Deleted property with key '{old_value}' by user {self.request.user}.")
messages.success(self.request, _("User property deleted successfully."))
# Redirect back to the original URL

View File

@ -210,6 +210,10 @@ LOGGING = {
'()': CustomFormatter,
'format': '%(levelname)s %(asctime)s %(message)s'
},
'verbose': {
'format': '{levelname} {asctime} {module} {message}',
'style': '{',
},
},
"handlers": {
"console": {
@ -217,6 +221,12 @@ LOGGING = {
"class": "logging.StreamHandler",
"formatter": "colored"
},
'device_log_file': {
'level': 'INFO',
'class': 'logging.FileHandler',
'filename': '/var/log/device_changes.log',
'formatter': 'verbose',
},
},
"root": {
"handlers": ["console"],
@ -232,7 +242,12 @@ LOGGING = {
"handlers": ["console"],
"level": "ERROR",
"propagate": False,
}
},
'device_log': {
'handlers': ['device_log_file'],
'level': 'INFO',
'propagate': False,
},
}
}