states and notes view refactoring

This commit is contained in:
Thomas Nahuel Rusiecki 2025-01-08 14:36:06 -03:00 committed by Cayo Puigdefabregas
parent 824c17ab0a
commit 00433d662e

View file

@ -9,12 +9,10 @@ from action.models import State, StateDefinition, Note, DeviceLog
from device.models import Device from device.models import Device
class ChangeStateView(View): class ChangeStateView(FormView):
form_class = ChangeStateForm
def post(self, request, *args, **kwargs): def form_valid(self, form):
form = ChangeStateForm(request.POST)
if form.is_valid():
previous_state = form.cleaned_data['previous_state'] previous_state = form.cleaned_data['previous_state']
new_state = form.cleaned_data['new_state'] new_state = form.cleaned_data['new_state']
snapshot_uuid = form.cleaned_data['snapshot_uuid'] snapshot_uuid = form.cleaned_data['snapshot_uuid']
@ -26,48 +24,53 @@ class ChangeStateView(View):
institution=self.request.user.institution, institution=self.request.user.institution,
) )
message = _("<Created> State '{}'. Previous State: '{}' ".format(new_state, previous_state) ) message = _("<Created> State '{}'. Previous State: '{}'").format(new_state, previous_state)
DeviceLog.objects.create( DeviceLog.objects.create(
snapshot_uuid=snapshot_uuid, snapshot_uuid=snapshot_uuid,
event=message, event=message,
user=self.request.user, user=self.request.user,
institution=self.request.user.institution, institution=self.request.user.institution,
) )
messages.success(self.request, _("State successfully changed from '{}' to '{}'").format(previous_state, new_state))
return super().form_valid(form)
messages.success(request, _("State succesfuly changed from '{}' to '{}' ".format(previous_state, new_state) ) ) def form_invalid(self, form):
else: messages.error(self.request, _("There was an error with your submission."))
messages.error(request, "There was an error with your submission.") return redirect(self.get_success_url())
return redirect(request.META.get('HTTP_REFERER') ) def get_success_url(self):
return self.request.META.get('HTTP_REFERER') or reverse_lazy('device:details')
class AddNoteView(View): class AddNoteView(FormView):
form_class = AddNoteForm
def post(self, request, *args, **kwargs): def form_valid(self, form):
form = AddNoteForm(request.POST) note_text = form.cleaned_data['note']
if form.is_valid():
note = form.cleaned_data['note']
snapshot_uuid = form.cleaned_data['snapshot_uuid'] snapshot_uuid = form.cleaned_data['snapshot_uuid']
Note.objects.create( Note.objects.create(
snapshot_uuid=snapshot_uuid, snapshot_uuid=snapshot_uuid,
description=note, description=note_text,
user=self.request.user, user=self.request.user,
institution=self.request.user.institution, institution=self.request.user.institution,
) )
message = _("<Created> Note: '{}' ".format(note) ) message = _("<Created> Note: '{}'").format(note_text)
DeviceLog.objects.create( DeviceLog.objects.create(
snapshot_uuid=snapshot_uuid, snapshot_uuid=snapshot_uuid,
event=message, event=message,
user=self.request.user, user=self.request.user,
institution=self.request.user.institution, institution=self.request.user.institution,
) )
messages.success(request, _("Note has been added")) messages.success(self.request, _("Note has been added"))
else: return super().form_valid(form)
messages.error(request, "There was an error with your submission.")
return redirect(request.META.get('HTTP_REFERER') ) def form_invalid(self, form):
messages.error(self.request, _("There was an error with your submission."))
return redirect(self.get_success_url())
def get_success_url(self):
return self.request.META.get('HTTP_REFERER') or reverse_lazy('device:details')
class UpdateNoteView(UpdateView): class UpdateNoteView(UpdateView):