added state delete
This commit is contained in:
parent
3ad5699c46
commit
56ed49ec4c
|
@ -6,5 +6,6 @@ app_name = 'action'
|
|||
urlpatterns = [
|
||||
|
||||
path("new/", views.NewActionView.as_view(), name="new_action"),
|
||||
path('state/<int:pk>/undo/', views.ActionUndoView.as_view(), name='undo_action'),
|
||||
|
||||
]
|
||||
|
|
|
@ -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'))
|
||||
|
|
|
@ -173,19 +173,49 @@
|
|||
<th scope="col">{% trans 'Date' %}</th>
|
||||
<th scope="col">{% trans 'User' %}</th>
|
||||
<th scope="col">{% trans 'State' %}</th>
|
||||
<th scope="col">{% trans 'Actions' %}</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
{% for state_change in device_states %}
|
||||
<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.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>
|
||||
{% empty %}
|
||||
<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>
|
||||
{% endfor %}
|
||||
</tbody>
|
||||
|
|
Loading…
Reference in a new issue