From 56ed49ec4cae94594dc6a817a2798b936b814a6a Mon Sep 17 00:00:00 2001 From: Thomas Rusiecki Date: Fri, 6 Dec 2024 20:04:23 -0300 Subject: [PATCH] added state delete --- action/urls.py | 1 + action/views.py | 15 ++++++++++++++ device/templates/details.html | 38 +++++++++++++++++++++++++++++++---- 3 files changed, 50 insertions(+), 4 deletions(-) diff --git a/action/urls.py b/action/urls.py index c6aa6d8..9c002dd 100644 --- a/action/urls.py +++ b/action/urls.py @@ -6,5 +6,6 @@ app_name = 'action' urlpatterns = [ path("new/", views.NewActionView.as_view(), name="new_action"), + path('state//undo/', views.ActionUndoView.as_view(), name='undo_action'), ] diff --git a/action/views.py b/action/views.py index 36f3152..c185ad8 100644 --- a/action/views.py +++ b/action/views.py @@ -2,6 +2,8 @@ from django.views import View from django.shortcuts import redirect, get_object_or_404 from django.contrib import messages from action.forms import AddStateForm +from django.views.generic.edit import DeleteView +from django.urls import reverse_lazy from action.models import State, StateDefinition from device.models import Device import logging @@ -37,3 +39,16 @@ class NewActionView(View): else: messages.error(request, "There was an error with your submission.") return redirect(request.META.get('HTTP_REFERER')) + +class ActionUndoView(DeleteView): + model = State + success_url = reverse_lazy('state_list') + + def delete(self, request, *args, **kwargs): + self.object = self.get_object() + time_since_creation = timezone.now() - self.object.date + if time_since_creation.total_seconds() <= 3600: # 1 hour is 3600 seconds + return super().delete(request, *args, **kwargs) + else: + messages.error(request, "You can undo an action within one hour of its creation.") + return redirect(request.META.get('HTTP_REFERER')) diff --git a/device/templates/details.html b/device/templates/details.html index 51ef5b5..8ee3ec4 100644 --- a/device/templates/details.html +++ b/device/templates/details.html @@ -173,19 +173,49 @@ {% trans 'Date' %} {% trans 'User' %} {% trans 'State' %} + {% trans 'Actions' %} {% for state_change in device_states %} - {{ state_change.state }} - - {{ state_change.user.responsable_person|default:state_change.user.username }} {{ state_change.date|date:"SHORT_DATETIME_FORMAT" }} + {{ state_change.user.responsable_person|default:state_change.user.username }} + {{ state_change.state }} + + {% if state_change.date|timesince < '1 hour' %} + + + + + + + {% endif %} + {% empty %} - {% trans 'No state changes recorded.' %} + {% trans 'No state changes recorded.' %} {% endfor %}