8
0
Fork 0
mirror of https://gitlab2.federez.net/re2o/re2o synced 2024-12-23 07:23:46 +00:00

Affiche uniquement les ips associées au machine type choisi

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'
This commit is contained in:
Maël Kervella 2017-10-06 23:16:19 +00:00
parent 9fcc0ce735
commit 966a60905b
2 changed files with 43 additions and 4 deletions

View file

@ -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):

View file

@ -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):