Browse Source

désallouer une IP nécessite une confirmation POST

Élie Bouttier 8 years ago
parent
commit
7d126a22f4

+ 9 - 0
services/static/js/deallocate_modal.js

@@ -0,0 +1,9 @@
+$('#confirm-deallocate').on('show.bs.modal', function(e) {
+    var action = $(e.relatedTarget).data('action');
+    var ip = $(e.relatedTarget).data('ip');
+    var service = $(e.relatedTarget).data('service');
+
+    $('#confirm-deallocate-form').attr('action', action);
+    $('.confirm-deallocate-ip').html(ip);
+    $('.confirm-deallocate-service').html(service);
+});

+ 9 - 2
services/templates/services/ipresource_detail.html

@@ -1,6 +1,6 @@
 {% extends 'base.html' %}
 
-{% load bootstrap3 %}
+{% load bootstrap3 services %}
 
 {% block content %}
 <div class="panel panel-primary">
@@ -25,7 +25,7 @@
             <td>{{ allocation.end|default:'–' }}</td>
             <td class="text-right">
                 {% if allocation.active %}
-                <a href="{% url 'deallocate' allocation.pk %}" class="btn btn-xs btn-danger">
+                <a href="#" data-toggle="modal" data-target="#confirm-deallocate" data-action="{% url 'deallocate' allocation.pk %}" data-ip="{{ allocation.resource }}" data-service="{{ allocation.service }}" class="btn btn-xs btn-danger">
                     <span class="glyphicon glyphicon-remove"></span>&nbsp;Désallouer
                 </a>
                 {% endif %}
@@ -40,4 +40,11 @@
     </div>
     {% endfor %}
 </div>
+
+{% deallocate_modal_div %}
+{% endblock %}
+
+{% block js_end %}
+{{ block.super }}
+{% deallocate_modal_js %}
 {% endblock %}

+ 9 - 2
services/templates/services/ipresource_list.html

@@ -1,6 +1,6 @@
 {% extends 'base.html' %}
 
-{% load bootstrap3 %}
+{% load bootstrap3 services %}
 
 {% block content %}
 <div class="panel panel-primary">
@@ -23,7 +23,7 @@
             <td>{% if service %}<a href="{% url 'service-detail' service.pk %}">{{ service }}</a>{% else %}–{% endif %}</td>
             <td class="text-right">
                 {% if service %}
-                <a href="{% url 'deallocate' ip.allocation.pk %}" class="btn btn-xs btn-danger">
+                <a href="#" data-toggle="modal" data-target="#confirm-deallocate" data-action="{% url 'deallocate' ip.allocation.pk %}" data-ip="{{ ip }}" data-service="{{ ip.allocation.service }}" class="btn btn-xs btn-danger">
                     <span class="glyphicon glyphicon-remove"></span>&nbsp;Désallouer
                 </a>
                 {% else %}
@@ -39,4 +39,11 @@
     {% endif %}
     {% endfor %}
 </div>
+
+{% deallocate_modal_div %}
+{% endblock %}
+
+{% block js_end %}
+{{ block.super }}
+{% deallocate_modal_js %}
 {% endblock %}

+ 19 - 0
services/templates/services/tags/deallocate_modal_div.html

@@ -0,0 +1,19 @@
+<div class="modal" id="confirm-deallocate" tabindex="-1" role="dialog" aria-hidden="true">
+  <div class="modal-dialog modal-sm">
+    <div class="panel panel-danger">
+      <div class="panel-heading" id="confirm-deallocate-title">
+        Désallouer <span class="confirm-deallocate-ip"></span> ?
+      </div>
+      <div class="panel-body">
+        <form action="" method="post" role="form" id="confirm-deallocate-form" class="text-center">
+          {% csrf_token %}
+          <p>
+            Êtes vous sûr de vouloir désallouer l’IP <strong class="confirm-deallocate-ip"></strong> actuellement affecté au service <strong class="confirm-deallocate-service"></strong> ?
+          </p>
+          <button type="submit" class="btn btn-danger">{% block confirm-ok %}Confirmer{% endblock %}</button>
+          <button type="button" class="btn btn-default" data-dismiss="modal">Annuler</button>
+        </form>
+      </div>
+    </div>
+  </div>
+</div>

+ 2 - 0
services/templates/services/tags/deallocate_modal_js.html

@@ -0,0 +1,2 @@
+{% load staticfiles %}
+<script src="{% static 'js/deallocate_modal.js' %}"></script>

+ 10 - 0
services/templatetags/services.py

@@ -14,3 +14,13 @@ def active(services):
 @register.filter
 def inactive(services):
     return services.filter(active=False)
+
+
+@register.inclusion_tag('services/tags/deallocate_modal_div.html')
+def deallocate_modal_div():
+    return {}
+
+
+@register.inclusion_tag('services/tags/deallocate_modal_js.html')
+def deallocate_modal_js():
+    return {}

+ 2 - 0
services/views.py

@@ -111,6 +111,8 @@ class ResourceAllocationMixin(PermissionRequiredMixin):
 
 
 class ResourceAllocationDeallocate(ResourceAllocationMixin, SingleObjectMixin, RedirectView):
+    http_method_names = ['post']
+
     def get_object(self, queryset=None):
         obj = super().get_object(queryset)
         if not obj.active: