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 json
import logging
from django.http import JsonResponse from django.http import JsonResponse
from django.http import Http404 from django.http import Http404
@ -20,6 +21,8 @@ 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")
@ -182,13 +185,15 @@ class AddUserPropertyView(DashboardView, CreateView):
form.instance.user = self.request.user form.instance.user = self.request.user
form.instance.uuid = self.property.uuid form.instance.uuid = self.property.uuid
form.instance.type = Property.Type.USER form.instance.type = Property.Type.USER
messages.success(self.request, _("User property successfully added."))
response = super().form_valid(form) response = super().form_valid(form)
return response return response
def get_form_kwargs(self): def get_form_kwargs(self):
pk = self.kwargs.get('pk') pk = self.kwargs.get('pk')
institution = self.request.user.institution institution = self.request.user.institution
self.property = UserProperty.objects.filter( self.property = SystemProperty.objects.filter(
owner=institution, owner=institution,
value=pk, value=pk,
type=Property.Type.SYSTEM type=Property.Type.SYSTEM
@ -220,20 +225,24 @@ class UpdateUserPropertyView(DashboardView, UpdateView):
return kwargs return kwargs
def form_valid(self, form): def form_valid(self, form):
form.instance.owner = self.request .user.institution 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.user = self.request.user
form.instance.type = Property.Type.USER form.instance.type = Property.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}."
)
return response return response
def get_success_url(self): def get_success_url(self):
referer = self.request.META.get('HTTP_REFERER') return self.request.META.get('HTTP_REFERER', reverse_lazy('device:details', args=[self.object.pk]))
if referer:
return referer
else:
return reverse_lazy('device:details', args=[self.object.device.pk])
class DeleteUserPropertyView(DashboardView, DeleteView): class DeleteUserPropertyView(DashboardView, DeleteView):
@ -250,7 +259,9 @@ 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
self.object.delete() 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.")) messages.success(self.request, _("User property deleted successfully."))
# Redirect back to the original URL # Redirect back to the original URL

View File

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