added state delete

This commit is contained in:
Thomas Nahuel Rusiecki 2024-12-06 20:04:23 -03:00
parent 3ad5699c46
commit 56ed49ec4c
3 changed files with 50 additions and 4 deletions

View file

@ -6,5 +6,6 @@ app_name = 'action'
urlpatterns = [ urlpatterns = [
path("new/", views.NewActionView.as_view(), name="new_action"), path("new/", views.NewActionView.as_view(), name="new_action"),
path('state/<int:pk>/undo/', views.ActionUndoView.as_view(), name='undo_action'),
] ]

View file

@ -2,6 +2,8 @@ from django.views import View
from django.shortcuts import redirect, get_object_or_404 from django.shortcuts import redirect, get_object_or_404
from django.contrib import messages from django.contrib import messages
from action.forms import AddStateForm 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 action.models import State, StateDefinition
from device.models import Device from device.models import Device
import logging import logging
@ -37,3 +39,16 @@ class NewActionView(View):
else: else:
messages.error(request, "There was an error with your submission.") messages.error(request, "There was an error with your submission.")
return redirect(request.META.get('HTTP_REFERER')) 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'))

View file

@ -173,19 +173,49 @@
<th scope="col">{% trans 'Date' %}</th> <th scope="col">{% trans 'Date' %}</th>
<th scope="col">{% trans 'User' %}</th> <th scope="col">{% trans 'User' %}</th>
<th scope="col">{% trans 'State' %}</th> <th scope="col">{% trans 'State' %}</th>
<th scope="col">{% trans 'Actions' %}</th>
</tr> </tr>
</thead> </thead>
<tbody> <tbody>
{% for state_change in device_states %} {% for state_change in device_states %}
<tr {% if forloop.first %}class="table-success"{% endif %}> <tr {% if forloop.first %}class="table-success"{% endif %}>
<td><strong>{{ state_change.state }}</strong></td>
<td>{{ state_change.user.responsable_person|default:state_change.user.username }}</td>
<td>{{ state_change.date|date:"SHORT_DATETIME_FORMAT" }}</td> <td>{{ state_change.date|date:"SHORT_DATETIME_FORMAT" }}</td>
<td>{{ state_change.user.responsable_person|default:state_change.user.username }}</td>
<td><strong>{{ state_change.state }}</strong></td>
<td>
{% if state_change.date|timesince < '1 hour' %}
<a href="#" data-bs-toggle="modal" data-bs-target="#deleteStateModal{{ state_change.id }}" title="{% trans 'Delete State' %}">
<i class="bi bi-trash text-danger"></i>
</a>
<!-- Delete Confirmation Modal -->
<div class="modal fade" id="deleteStateModal{{ state_change.id }}" tabindex="-1" aria-labelledby="deleteStateModalLabel{{ state_change.id }}" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="deleteStateModalLabel{{ state_change.id }}">{% trans 'Confirm Delete' %}</h5>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="{% trans 'Close' %}"></button>
</div>
<div class="modal-body">
<p>{% trans 'Are you sure you want to undo this state?' %}</p>
<p><strong>{{ state_change.state }}</strong> - {{ state_change.date|date:"SHORT_DATETIME_FORMAT" }}</p>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">{% trans 'Cancel' %}</button>
<form method="post" action="{% url 'action:undo_action' state_change.id %}">
{% csrf_token %}
<button type="submit" class="btn btn-danger">{% trans 'Delete' %}</button>
</form>
</div>
</div>
</div>
</div>
{% endif %}
</td>
</tr> </tr>
{% empty %} {% empty %}
<tr> <tr>
<td colspan="3" class="text-center">{% trans 'No state changes recorded.' %}</td> <td colspan="4" class="text-center">{% trans 'No state changes recorded.' %}</td>
</tr> </tr>
{% endfor %} {% endfor %}
</tbody> </tbody>