devicehub-django/action/views.py

139 lines
4.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
2025-01-03 06:24:08 +00:00
from django.views.generic.edit import DeleteView, CreateView, UpdateView, 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
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-20 19:11:59 +00:00
message = _("<Created> State '{}'. Previous State: '{}' ".format(new_state, previous_state) )
DeviceLog.objects.create(
snapshot_uuid=snapshot_uuid,
event=message,
user=self.request.user,
institution=self.request.user.institution,
)
messages.success(request, _("State succesfuly changed from '{}' to '{}' ".format(previous_state, new_state) ) )
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 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,
)
2024-12-20 19:11:59 +00:00
message = _("<Created> 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') )
2025-01-03 06:24:08 +00:00
2025-01-04 05:24:33 +00:00
2025-01-03 06:24:08 +00:00
class UpdateNoteView(UpdateView):
model = Note
fields = ['description']
2025-01-04 04:54:20 +00:00
template_name = "blank.html"
2025-01-03 06:24:08 +00:00
pk_url_kwarg = 'pk'
def form_valid(self, form):
old_description = self.get_object().description
new_description = self.object.description
snapshot_uuid = self.object.snapshot_uuid
if old_description != new_description:
message = _("<Updated> Note. Old Description: '{}'. New Description: '{}'").format(old_description, new_description)
DeviceLog.objects.create(
snapshot_uuid=snapshot_uuid,
event=message,
user=self.request.user,
institution=self.request.user.institution,
)
2025-01-04 05:24:33 +00:00
messages.success(self.request, "Note has been updated.")
2025-01-03 06:24:08 +00:00
return super().form_valid(form)
2025-01-04 05:24:33 +00:00
def form_invalid(self, form):
new_description = form.cleaned_data.get('description', '').strip()
if not new_description:
messages.error(self.request, _("Note cannot be empty."))
super().form_invalid(form)
return redirect(self.get_success_url())
2025-01-03 06:24:08 +00:00
def get_success_url(self):
return self.request.META.get('HTTP_REFERER', reverse_lazy('device:details'))
2025-01-04 04:54:20 +00:00
class DeleteNoteView(View):
model = Note
def post(self, request, *args, **kwargs):
self.pk = kwargs['pk']
referer = request.META.get('HTTP_REFERER')
if not referer:
raise Http404("No referer header found")
self.object = get_object_or_404(
self.model,
pk=self.pk,
institution=self.request.user.institution
)
description = self.object.description
snapshot_uuid= self.object.snapshot_uuid
if request.user != self.object.user and not request.user.is_admin:
messages.error(request, _("You do not have permission to delete this note."))
return redirect(referer)
message = _("<Deleted> Note. Description: '{}'. ").format(description)
DeviceLog.objects.create(
snapshot_uuid=snapshot_uuid,
event=message,
user=request.user,
institution=request.user.institution,
)
messages.warning(self.request, _("Note '{}' deleted successfully.").format(description))
self.object.delete()
return redirect(referer)