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 394f02c133
commit ec81f05372

View file

@ -9,65 +9,68 @@ 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) previous_state = form.cleaned_data['previous_state']
new_state = form.cleaned_data['new_state']
if form.is_valid(): snapshot_uuid = form.cleaned_data['snapshot_uuid']
previous_state = form.cleaned_data['previous_state']
new_state = form.cleaned_data['new_state']
snapshot_uuid = form.cleaned_data['snapshot_uuid']
State.objects.create( State.objects.create(
snapshot_uuid=snapshot_uuid, snapshot_uuid=snapshot_uuid,
state=new_state, state=new_state,
user=self.request.user, user=self.request.user,
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']
snapshot_uuid = form.cleaned_data['snapshot_uuid']
Note.objects.create(
snapshot_uuid=snapshot_uuid,
description=note_text,
user=self.request.user,
institution=self.request.user.institution,
)
if form.is_valid(): message = _("<Created> Note: '{}'").format(note_text)
note = form.cleaned_data['note'] DeviceLog.objects.create(
snapshot_uuid = form.cleaned_data['snapshot_uuid'] snapshot_uuid=snapshot_uuid,
Note.objects.create( event=message,
snapshot_uuid=snapshot_uuid, user=self.request.user,
description=note, institution=self.request.user.institution,
user=self.request.user, )
institution=self.request.user.institution, messages.success(self.request, _("Note has been added"))
) return super().form_valid(form)
message = _("<Created> Note: '{}' ".format(note) ) def form_invalid(self, form):
DeviceLog.objects.create( messages.error(self.request, _("There was an error with your submission."))
snapshot_uuid=snapshot_uuid, return redirect(self.get_success_url())
event=message,
user=self.request.user,
institution=self.request.user.institution,
)
messages.success(request, _("Note has been added"))
else:
messages.error(request, "There was an error with your submission.")
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 UpdateNoteView(UpdateView): class UpdateNoteView(UpdateView):