From 5088403cf94df2e5fa074cef81822a0511787cb1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ma=C3=ABl=20Kervella?= Date: Fri, 6 Oct 2017 23:16:19 +0000 Subject: [PATCH] =?UTF-8?q?Affiche=20uniquement=20les=20ips=20associ=C3=A9?= =?UTF-8?q?es=20au=20machine=20type=20choisi?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Utilise la customisation du tag bootstrap_form_typeahead pour ajouter un champs correspondant au type de machine dans les données et filtrer les match pour ne garder que les résultat qui ont le bon champs 'type' --- machines/forms.py | 6 +++--- machines/views.py | 41 ++++++++++++++++++++++++++++++++++++++++- 2 files changed, 43 insertions(+), 4 deletions(-) diff --git a/machines/forms.py b/machines/forms.py index ca167638..0f6554cf 100644 --- a/machines/forms.py +++ b/machines/forms.py @@ -25,7 +25,7 @@ from __future__ import unicode_literals from django.forms import ModelForm, Form, ValidationError from django import forms from .models import Domain, Machine, Interface, IpList, MachineType, Extension, Mx, Text, Ns, Service, Vlan, Nas, IpType -from django.db.models import Q +from django.db.models import Q, F from django.core.validators import validate_email from users.models import User @@ -75,9 +75,9 @@ class AddInterfaceForm(EditInterfaceForm): self.fields['ipv4'].empty_label = "Assignation automatique de l'ipv4" if not infra: self.fields['type'].queryset = MachineType.objects.filter(ip_type__in=IpType.objects.filter(need_infra=False)) - self.fields['ipv4'].queryset = IpList.objects.filter(interface__isnull=True).filter(ip_type__in=IpType.objects.filter(need_infra=False)) + self.fields['ipv4'].queryset = IpList.objects.filter(interface__isnull=True).filter(ip_type__in=IpType.objects.filter(need_infra=False)).annotate(type=F('ip_type__machinetype__id')) else: - self.fields['ipv4'].queryset = IpList.objects.filter(interface__isnull=True) + self.fields['ipv4'].queryset = IpList.objects.filter(interface__isnull=True).annotate(type=F('ip_type__machinetype__id')) class NewInterfaceForm(EditInterfaceForm): class Meta(EditInterfaceForm.Meta): diff --git a/machines/views.py b/machines/views.py index 4268519b..f70a95e4 100644 --- a/machines/views.py +++ b/machines/views.py @@ -74,6 +74,43 @@ def form(ctx, template, request): c.update(csrf(request)) return render(request, template, c) +def generate_ipv4_choices( field ) : + return '[{key: "", value: "' + str(field.empty_label) + '", type: -1},' + \ + ', '.join([ \ + '{key: ' + str(ip.id) + ',' \ + ' value: "' + str(ip.ipv4) + '",' \ + ' type: ' + str(ip.type) + '}' \ + for ip in field.queryset \ + ]) + \ + '];' + +def generate_ipv4_match_func() : + return 'function(q, sync) {' \ + 'var select = function (array, nb, filter) {' \ + 'var i=0; var res=[];' \ + 'while (nb >= 0 && i < array.length) {' \ + 'if (filter(array[i])) {' \ + 'res.push(array[i]);' \ + 'nb -= 1;' \ + '}' \ + 'i += 1;' \ + '}' \ + 'return res;' \ + '};' \ + 'var filter = function (elt) {' \ + 'return elt.type == -1 || elt.type == $("#id_type").val();' \ + '};' \ + 'var cb = function (a) { sync(a.filter(filter)); };' \ + 'if (q === "") {' \ + 'sync( engine.get( select(choices, 10, filter).map(' \ + 'function (elt) { return elt.key; }' \ + ') ) );' \ + '} else {' \ + 'engine.search(q, cb);' \ + '}' \ + '}' + + @login_required def new_machine(request, userid): try: @@ -117,7 +154,9 @@ def new_machine(request, userid): reversion.set_comment("Création") messages.success(request, "La machine a été créée") return redirect("/users/profil/" + str(user.id)) - return form({'machineform': machine, 'interfaceform': interface, 'domainform': domain}, 'machines/machine.html', request) + i_choices = { 'ipv4': generate_ipv4_choices( interface.fields['ipv4'] ) } + i_match_func = { 'ipv4': generate_ipv4_match_func() } + return form({'machineform': machine, 'interfaceform': interface, 'domainform': domain, 'i_choices': i_choices, 'i_match_func': i_match_func}, 'machines/machine.html', request) @login_required def edit_interface(request, interfaceid):