added add state definition view

This commit is contained in:
Thomas Nahuel Rusiecki 2024-12-01 19:47:40 -03:00
parent 175b6d93dc
commit 4d8f9eac9b
3 changed files with 122 additions and 1 deletions

View file

@ -0,0 +1,92 @@
{% extends "base.html" %}
{% load i18n django_bootstrap5 %}
{% block content %}
<div class="row">
<div class="col">
<h3>{{ subtitle }}</h3>
</div>
<div class="col text-end">
<button type="button" class="btn btn-green-admin" data-bs-toggle="modal" data-bs-target="#addStateModal">
{% trans "Add State Definition" %}
</button>
</div>
</div>
<div class="row mt-4">
<div class="col">
<h4>{% trans "State Definitions" %}</h4>
{% if state_definitions %}
<ul id="sortable" class="list-group mb-4">
{% for state_definition in state_definitions %}
<li class="list-group-item d-flex justify-content-between align-items-center" data-id="{{ state_definition.id }}">
<div class="d-flex align-items-center">
<span class="me-4 display-7">{{ state_definition.order }}</span>
<div>
<strong> {{ state_definition.state }}</strong> <br>
</div>
</div>
<form method="post" action="#">
{% csrf_token %}
<button type="submit" class="btn btn-danger btn-sm">{% trans "Delete" %}</button>
</form>
</li>
{% endfor %}
</ul>
{% else %}
<div class="alert alert-primary text-center mt-5" role="alert">
{% trans "No states found on current organization" %}
</div>
{% endif %}
</div>
</div>
<div class="modal fade" id="addStateModal" tabindex="-1" aria-labelledby="addStateModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="addStateModalLabel">{% trans "Add State Definition" %}</h5>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<div class="modal-body">
<form method="post" action="{%url 'admin:add_state_definition'%}">
{% csrf_token %}
<div class="mb-3">
<label for="stateInput" class="form-label">{% trans "State" %}</label>
<input type="text" class="form-control" id="stateInput" name="state" maxlength="50" required>
<div class="form-text">{% trans "Maximum 50 characters." %}</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">{% trans "Close" %}</button>
<button type="submit" class="btn btn-primary">{% trans "Add state definition" %}</button>
</div>
</form>
</div>
</div>
</div>
</div>
<script>
$(function() {
$("#sortable").sortable({
update: function(event, ui) {
var order = $(this).sortable('toArray', { attribute: 'data-id' });
$.ajax({
url: " ",
method: "POST",
data: {
order: order,
csrfmiddlewaretoken: '{{ csrf_token }}'
},
success: function(response) {
location.reload();
}
});
}
});
$("#sortable").disableSelection();
});
</script>
{% endblock %}

View file

@ -11,4 +11,5 @@ urlpatterns = [
path("users/delete/<int:pk>", views.DeleteUserView.as_view(), name="delete_user"),
path("institution/<int:pk>", views.InstitutionView.as_view(), name="institution"),
path("states/", views.StatesPanelView.as_view(), name="states"),
path("states/add", views.AddStateDefinitionView.as_view(), name="add_state_definition"),
]

View file

@ -1,4 +1,5 @@
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.utils.translation import gettext_lazy as _
@ -8,6 +9,8 @@ from django.views.generic.edit import (
UpdateView,
DeleteView,
)
from django.core.exceptions import ValidationError
from django.db import IntegrityError
from dashboard.mixins import DashboardView, Http403
from user.models import User, Institution
from admin.email import NotifyActivateUserByEmail
@ -135,7 +138,32 @@ class StatesPanelView(AdminView, TemplateView):
context = super().get_context_data(**kwargs)
context.update({
"states": State.objects.filter(institution=self.request.user.institution),
"state_definitions" : StateDefinition.objects.filter(institution=self.request.user.institution).order_by('order')
})
return context
class AddStateDefinitionView(DashboardView, CreateView):
template_name = "states_panel.html"
title = _("New State Definition")
breadcrumb = "Admin / New state"
success_url = reverse_lazy('admin:states')
model = StateDefinition
fields = ('state',)
def form_valid(self, form):
form.instance.institution = self.request.user.institution
form.instance.user = self.request.user
try:
response = super().form_valid(form)
messages.success(self.request, _("State definition successfully added."))
return response
except IntegrityError:
messages.error(self.request, _("State is already defined."))
return self.form_invalid(form)
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context.update({
"state_definitions": StateDefinition.objects.filter(institution=self.request.user.institution).order_by('order'),
})
return context