devicehub-django/action/views.py

83 lines
2.9 KiB
Python
Raw Normal View History

2024-12-06 02:09:17 +00:00
from django.views import View
from django.shortcuts import redirect, get_object_or_404
from django.contrib import messages
2024-12-19 21:17:09 +00:00
from action.forms import ChangeStateForm, AddNoteForm
from django.views.generic.edit import DeleteView, CreateView, FormView
2024-12-06 23:04:23 +00:00
from django.urls import reverse_lazy
2024-12-19 21:17:09 +00:00
from django.utils.translation import gettext_lazy as _
from action.models import State, StateDefinition, Note, DeviceLog
2024-12-06 02:09:17 +00:00
from device.models import Device
import logging
2024-12-06 22:48:31 +00:00
2024-12-19 21:17:09 +00:00
class ChangeStateView(View):
2024-12-06 02:09:17 +00:00
def post(self, request, *args, **kwargs):
2024-12-19 21:17:09 +00:00
form = ChangeStateForm(request.POST)
2024-12-06 02:09:17 +00:00
if form.is_valid():
2024-12-19 21:17:09 +00:00
previous_state = form.cleaned_data['previous_state']
new_state = form.cleaned_data['new_state']
2024-12-06 02:09:17 +00:00
snapshot_uuid = form.cleaned_data['snapshot_uuid']
2024-12-19 21:17:09 +00:00
State.objects.create(
2024-12-06 02:09:17 +00:00
snapshot_uuid=snapshot_uuid,
2024-12-19 21:17:09 +00:00
state=new_state,
user=self.request.user,
institution=self.request.user.institution,
2024-12-06 02:09:17 +00:00
)
2024-12-19 21:17:09 +00:00
message = _("State changed from '{}' to '{}'.".format(previous_state, new_state) )
DeviceLog.objects.create(
snapshot_uuid=snapshot_uuid,
event=message,
user=self.request.user,
institution=self.request.user.institution,
)
2024-12-19 21:17:09 +00:00
messages.success(request,message)
2024-12-06 02:09:17 +00:00
else:
messages.error(request, "There was an error with your submission.")
2024-12-19 21:17:09 +00:00
return redirect(request.META.get('HTTP_REFERER') )
2024-12-06 23:04:23 +00:00
2024-12-11 20:35:55 +00:00
2024-12-19 21:17:09 +00:00
class UndoStateView(DeleteView):
model = State
2024-12-06 23:04:23 +00:00
def delete(self, request, *args, **kwargs):
self.object = self.get_object()
2024-12-16 18:21:09 +00:00
return super().delete(request, *args, **kwarg)
2024-12-09 20:26:31 +00:00
def get_success_url(self):
2024-12-16 18:21:09 +00:00
messages.info(self.request, f"Action to state: {self.object.state} has been deleted.")
2024-12-09 20:26:31 +00:00
return self.request.META.get('HTTP_REFERER', reverse_lazy('device:details', args=[self.object.snapshot_uuid]))
2024-12-19 21:17:09 +00:00
class AddNoteView(View):
def post(self, request, *args, **kwargs):
form = AddNoteForm(request.POST)
if form.is_valid():
note = form.cleaned_data['note']
snapshot_uuid = form.cleaned_data['snapshot_uuid']
Note.objects.create(
snapshot_uuid=snapshot_uuid,
description=note,
user=self.request.user,
institution=self.request.user.institution,
)
message = _("Note: '{}' ".format(note) )
DeviceLog.objects.create(
snapshot_uuid=snapshot_uuid,
event=message,
user=self.request.user,
institution=self.request.user.institution,
)
2024-12-19 21:17:09 +00:00
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') )