diff --git a/admin/templates/states_panel.html b/admin/templates/states_panel.html
index 77d36ea..e19e2ca 100644
--- a/admin/templates/states_panel.html
+++ b/admin/templates/states_panel.html
@@ -17,7 +17,6 @@
-
{% trans "Are you sure you want to delete this state definition?" %}
+
{% trans "Are you sure you want to delete this state definition?" %}
+
{% trans "Any change in order will not be saved" %}
{{ state_definition.order }}
@@ -116,19 +118,21 @@
diff --git a/admin/urls.py b/admin/urls.py
index 52042ca..6c7b3e9 100644
--- a/admin/urls.py
+++ b/admin/urls.py
@@ -13,4 +13,5 @@ urlpatterns = [
path("states/", views.StatesPanelView.as_view(), name="states"),
path("states/add", views.AddStateDefinitionView.as_view(), name="add_state_definition"),
path('states/delete/', views.DeleteStateDefinitionView.as_view(), name='delete_state_definition'),
+ path('states/update_order/', views.UpdateStateOrderView.as_view(), name='update_state_order'),
]
diff --git a/admin/views.py b/admin/views.py
index e62b5d6..2e69413 100644
--- a/admin/views.py
+++ b/admin/views.py
@@ -1,7 +1,7 @@
from smtplib import SMTPException
from django.contrib import messages
from django.urls import reverse_lazy
-from django.shortcuts import get_object_or_404
+from django.shortcuts import get_object_or_404, redirect, Http404
from django.utils.translation import gettext_lazy as _
from django.contrib.messages.views import SuccessMessageMixin
from django.views.generic.base import TemplateView, ContextMixin
@@ -11,8 +11,9 @@ from django.views.generic.edit import (
DeleteView,
)
from django.core.exceptions import ValidationError
-from django.db import IntegrityError
+from django.db import IntegrityError, transaction
from dashboard.mixins import DashboardView, Http403
+from admin.forms import OrderingStateForm
from user.models import User, Institution
from admin.email import NotifyActivateUserByEmail
from action.models import StateDefinition
@@ -181,3 +182,26 @@ class DeleteStateDefinitionView(AdminView, StateDefinitionContextMixin, SuccessM
raise Http404
return super().delete(request, *args, **kwargs)
+
+class UpdateStateOrderView(AdminView, TemplateView):
+ success_url = reverse_lazy('admin:states')
+
+ def post(self, request, *args, **kwargs):
+ form = OrderingStateForm(request.POST)
+
+ if form.is_valid():
+ ordered_ids = form.cleaned_data["ordering"].split(',')
+
+ with transaction.atomic():
+ current_order = 1
+ for lookup_id in ordered_ids:
+ state_definition = StateDefinition.objects.get(id=lookup_id)
+ state_definition.order = current_order
+ state_definition.save()
+ current_order += 1
+
+ messages.success(self.request, _("Order changed succesfuly."))
+ return redirect(self.success_url)
+ else:
+ return Http404
+