statedefinitions edit popup added and url changes

This commit is contained in:
Thomas Nahuel Rusiecki 2024-12-13 16:06:42 -03:00
parent d005d323f4
commit 883db4ea09
4 changed files with 61 additions and 7 deletions

View file

@ -38,6 +38,9 @@
</td> </td>
<td>{{ state_definition.state }}</td> <td>{{ state_definition.state }}</td>
<td class="text-end"> <td class="text-end">
<button type="button" class="btn btn-primary btn-sm me-1" data-bs-toggle="modal" data-bs-target="#editStateModal{{ state_definition.id }}">
<i class="bi bi-pencil"></i>
</button>
<button type="button" id="delete-btn" class="btn btn-danger btn-sm" data-bs-toggle="modal" data-bs-target="#deleteStateModal{{ state_definition.id }}"> <button type="button" id="delete-btn" class="btn btn-danger btn-sm" data-bs-toggle="modal" data-bs-target="#deleteStateModal{{ state_definition.id }}">
<i class="bi bi-trash"></i> <i class="bi bi-trash"></i>
</button> </button>
@ -90,6 +93,40 @@
</div> </div>
</div> </div>
<!-- Edit State Definition Modals -->
{% for state_definition in state_definitions %}
<div class="modal fade" id="editStateModal{{ state_definition.id }}" tabindex="-1" aria-labelledby="editStateModalLabel{{ state_definition.id }}" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<form method="post" action="{% url 'admin:edit_state_definition' state_definition.id %}">
{% csrf_token %}
<div class="modal-header">
<h5 class="modal-title" id="editStateModalLabel{{ state_definition.id }}">
{% trans "Edit State Definition" %}
</h5>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="{% trans 'Close' %}"></button>
</div>
<div class="modal-body">
<div class="mb-3">
<label for="editStateInput{{ state_definition.id }}" class="form-label">{% trans "State" %}</label>
<input type="text" class="form-control" id="editStateInput{{ state_definition.id }}" name="state" maxlength="50" value="{{ state_definition.state }}" required>
<div class="form-text">{% trans "Maximum 50 characters." %}</div>
</div>
<div class="alert alert-warning" role="alert">
{% trans "Existing devices with this state will not have their state names changed." %}
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">{% trans "Cancel" %}</button>
<button type="submit" class="btn btn-primary">{% trans "Save Changes" %}</button>
</div>
</form>
</div>
</div>
</div>
{% endfor %}
<!-- delete state definition Modal --> <!-- delete state definition Modal -->
{% for state_definition in state_definitions %} {% for state_definition in state_definitions %}
<div class="modal fade" id="deleteStateModal{{ state_definition.id }}" tabindex="-1" aria-labelledby="deleteStateModalLabel{{ state_definition.id }}" aria-hidden="true"> <div class="modal fade" id="deleteStateModal{{ state_definition.id }}" tabindex="-1" aria-labelledby="deleteStateModalLabel{{ state_definition.id }}" aria-hidden="true">

View file

@ -10,8 +10,9 @@ urlpatterns = [
path("users/edit/<int:pk>", views.EditUserView.as_view(), name="edit_user"), path("users/edit/<int:pk>", views.EditUserView.as_view(), name="edit_user"),
path("users/delete/<int:pk>", views.DeleteUserView.as_view(), name="delete_user"), path("users/delete/<int:pk>", views.DeleteUserView.as_view(), name="delete_user"),
path("institution/<int:pk>", views.InstitutionView.as_view(), name="institution"), path("institution/<int:pk>", views.InstitutionView.as_view(), name="institution"),
path("states/", views.StatesPanelView.as_view(), name="states"), path("states/", views.StatesPanelView.as_view(), name="states_panel"),
path("states/add", views.AddStateDefinitionView.as_view(), name="add_state_definition"), path("states/add", views.AddStateDefinitionView.as_view(), name="add_state_definition"),
path('states/delete/<int:pk>', views.DeleteStateDefinitionView.as_view(), name='delete_state_definition'), path("states/delete/<int:pk>", views.DeleteStateDefinitionView.as_view(), name='delete_state_definition'),
path('states/update_order/', views.UpdateStateOrderView.as_view(), name='update_state_order'), path("states/update_order/", views.UpdateStateOrderView.as_view(), name='update_state_order'),
path("states/edit/<int:pk>/", views.UpdateStateDefinitionView.as_view(), name='edit_state_definition'),
] ]

View file

@ -153,7 +153,7 @@ class AddStateDefinitionView(AdminView, StateDefinitionContextMixin, CreateView)
template_name = "states_panel.html" template_name = "states_panel.html"
title = _("New State Definition") title = _("New State Definition")
breadcrumb = "Admin / New state" breadcrumb = "Admin / New state"
success_url = reverse_lazy('admin:states') success_url = reverse_lazy('admin:states_panel')
model = StateDefinition model = StateDefinition
fields = ('state',) fields = ('state',)
@ -175,7 +175,7 @@ class AddStateDefinitionView(AdminView, StateDefinitionContextMixin, CreateView)
class DeleteStateDefinitionView(AdminView, StateDefinitionContextMixin, SuccessMessageMixin, DeleteView): class DeleteStateDefinitionView(AdminView, StateDefinitionContextMixin, SuccessMessageMixin, DeleteView):
model = StateDefinition model = StateDefinition
success_url = reverse_lazy('admin:states') success_url = reverse_lazy('admin:states_panel')
def get_success_message(self, cleaned_data): def get_success_message(self, cleaned_data):
device_logger.info(f"<Deleted> StateDefinition with value {self.object.state} by user {self.request.user}.") device_logger.info(f"<Deleted> StateDefinition with value {self.object.state} by user {self.request.user}.")
@ -192,7 +192,7 @@ class DeleteStateDefinitionView(AdminView, StateDefinitionContextMixin, SuccessM
class UpdateStateOrderView(AdminView, TemplateView): class UpdateStateOrderView(AdminView, TemplateView):
success_url = reverse_lazy('admin:states') success_url = reverse_lazy('admin:states_panel')
def post(self, request, *args, **kwargs): def post(self, request, *args, **kwargs):
form = OrderingStateForm(request.POST) form = OrderingStateForm(request.POST)
@ -218,3 +218,19 @@ class UpdateStateOrderView(AdminView, TemplateView):
else: else:
return Http404 return Http404
class UpdateStateDefinitionView(AdminView, UpdateView):
model = StateDefinition
template_name = 'states_panel.html'
fields = ['state']
pk_url_kwarg = 'pk'
def get_queryset(self):
return StateDefinition.objects.filter(institution=self.request.user.institution)
def get_success_url(self):
messages.success(self.request, _("State definition updated successfully."))
return reverse_lazy('admin:states_panel')
def form_valid(self, form):
return super().form_valid(form)

View file

@ -98,7 +98,7 @@
</a> </a>
</li> </li>
<li class="nav-item"> <li class="nav-item">
<a class="nav-link{% if path == 'states' %} active2{% endif %}" href="{% url 'admin:states' %}"> <a class="nav-link{% if path == 'states' %} active2{% endif %}" href="{% url 'admin:states_panel' %}">
{% trans 'States' %} {% trans 'States' %}
</a> </a>
</li> </li>