From 1cc864bc0fe0365a3527a11429ce0f7868583b33 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ma=C3=ABl=20Kervella?= Date: Sat, 7 Oct 2017 00:35:48 +0000 Subject: [PATCH] Utilise typeahead dynamique sur tous les forms avec ip --- machines/forms.py | 10 +++++----- machines/views.py | 8 ++++++-- 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/machines/forms.py b/machines/forms.py index 434e7970..5c8aef37 100644 --- a/machines/forms.py +++ b/machines/forms.py @@ -61,9 +61,9 @@ class EditInterfaceForm(ModelForm): self.fields['type'].empty_label = "Séléctionner un type de machine" if "ipv4" in self.fields: self.fields['ipv4'].empty_label = "Assignation automatique de l'ipv4" - 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')) # Add it's own address - self.fields['ipv4'].queryset |= IpList.objects.filter(id=self.fields['ipv4'].get_bound_field(self, 'ipv4').value()) + self.fields['ipv4'].queryset |= IpList.objects.filter(id=self.fields['ipv4'].get_bound_field(self, 'ipv4').value()).annotate(type=F('ip_type__machinetype__id')) if "machine" in self.fields: self.fields['machine'].queryset = Machine.objects.all().select_related('user') @@ -95,11 +95,11 @@ class BaseEditInterfaceForm(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')) # Add it's own address - self.fields['ipv4'].queryset |= IpList.objects.filter(id=self.fields['ipv4'].get_bound_field(self, 'ipv4').value()) + self.fields['ipv4'].queryset |= IpList.objects.filter(id=self.fields['ipv4'].get_bound_field(self, 'ipv4').value()).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 AliasForm(ModelForm): class Meta: diff --git a/machines/views.py b/machines/views.py index 34ebd14a..49e9d1c4 100644 --- a/machines/views.py +++ b/machines/views.py @@ -195,7 +195,9 @@ def edit_interface(request, interfaceid): reversion.set_comment("Champs modifié(s) : %s" % ', '.join(field for field in domain_form.changed_data)) messages.success(request, "La machine a été modifiée") return redirect("/users/profil/" + str(interface.machine.user.id)) - return form({'machineform': machine_form, 'interfaceform': interface_form, 'domainform': domain_form}, 'machines/machine.html', request) + i_choices = { 'ipv4': generate_ipv4_choices( interface_form.fields['ipv4'] ) } + i_match_func = { 'ipv4': generate_ipv4_match_func() } + return form({'machineform': machine_form, 'interfaceform': interface_form, 'domainform': domain_form, 'i_choices': i_choices, 'i_match_func': i_match_func}, 'machines/machine.html', request) @login_required def del_machine(request, machineid): @@ -251,7 +253,9 @@ def new_interface(request, machineid): reversion.set_comment("Création") messages.success(request, "L'interface a été ajoutée") return redirect("/users/profil/" + str(machine.user.id)) - return form({'interfaceform': interface_form, 'domainform': domain_form}, 'machines/machine.html', request) + i_choices = { 'ipv4': generate_ipv4_choices( interface_form.fields['ipv4'] ) } + i_match_func = { 'ipv4': generate_ipv4_match_func() } + return form({'interfaceform': interface_form, 'domainform': domain_form, 'i_choices': i_choices, 'i_match_func': i_match_func}, 'machines/machine.html', request) @login_required def del_interface(request, interfaceid):