From a8d0c914ca4556ccb2cb29d4958071c089481d55 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ma=C3=ABl=20Kervella?= Date: Sat, 7 Oct 2017 18:52:49 +0000 Subject: [PATCH] Met l'ip actuelle et la bonne value dans le form d'edition d'interface --- machines/forms.py | 6 +++--- machines/templatetags/bootstrap_form_typeahead.py | 14 +++++++++----- 2 files changed, 12 insertions(+), 8 deletions(-) diff --git a/machines/forms.py b/machines/forms.py index 940b5041..48498d36 100644 --- a/machines/forms.py +++ b/machines/forms.py @@ -65,7 +65,7 @@ class EditInterfaceForm(ModelForm): self.fields['ipv4'].empty_label = "Assignation automatique de l'ipv4" self.fields['ipv4'].queryset = IpList.objects.filter(interface__isnull=True).annotate(mtype_id=F('ip_type__machinetype__id')) # Add it's own address - self.fields['ipv4'].queryset |= IpList.objects.filter(id=self.instance.id).annotate(mtype_id=F('ip_type__machinetype__id')) + self.fields['ipv4'].queryset |= IpList.objects.filter(interface=self.instance).annotate(mtype_id=F('ip_type__machinetype__id')) if "machine" in self.fields: self.fields['machine'].queryset = Machine.objects.all().select_related('user') @@ -99,10 +99,10 @@ class BaseEditInterfaceForm(EditInterfaceForm): 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)).annotate(mtype_id=F('ip_type__machinetype__id')) # Add it's own address - self.fields['ipv4'].queryset |= IpList.objects.filter(id=self.instance.id).annotate(mtype_id=F('ip_type__machinetype__id')) + self.fields['ipv4'].queryset |= IpList.objects.filter(interface=self.instance).annotate(mtype_id=F('ip_type__machinetype__id')) else: self.fields['ipv4'].queryset = IpList.objects.filter(interface__isnull=True).annotate(mtype_id=F('ip_type__machinetype__id')) - self.fields['ipv4'].queryset |= IpList.objects.filter(id=self.instance.id).annotate(mtype_id=F('ip_type__machinetype__id')) + self.fields['ipv4'].queryset |= IpList.objects.filter(interface=self.instance).annotate(mtype_id=F('ip_type__machinetype__id')) class AliasForm(ModelForm): class Meta: diff --git a/machines/templatetags/bootstrap_form_typeahead.py b/machines/templatetags/bootstrap_form_typeahead.py index f4cd357d..39a3e33c 100644 --- a/machines/templatetags/bootstrap_form_typeahead.py +++ b/machines/templatetags/bootstrap_form_typeahead.py @@ -180,6 +180,7 @@ def bootstrap_form_typeahead(django_form, typeahead_fields, *args, **kwargs): typeahead_js( f_name, f_value, + f_bound, t_choices, t_engine, t_match_func, @@ -215,7 +216,7 @@ def hidden_tag( f_bound, f_name ): } ) -def typeahead_js( f_name, f_value, +def typeahead_js( f_name, f_value, f_bound, t_choices, t_engine, t_match_func, t_update_on ) : """ The whole script to use """ @@ -239,7 +240,7 @@ def typeahead_js( f_name, f_value, '$("#'+input_id(f_name) + '").typeahead(\n' + \ default_datasets( f_name, match_func ) + '\n' + \ ');\n' + \ - reset_input( f_name, f_value ) + '\n' + \ + reset_input( f_name, f_bound ) + '\n' + \ '};\n' + \ '$("#'+input_id(f_name) + '").bind(\n' + \ '"typeahead:select", ' + \ @@ -254,10 +255,13 @@ def typeahead_js( f_name, f_value, return render_tag( 'script', content=mark_safe( js_content ) ) -def reset_input( f_name, f_value ) : +def reset_input( f_name, f_bound ) : """ The JS script to reset the fields values """ - return '$("#'+input_id(f_name)+'").typeahead("val","");\n' \ - '$("#'+hidden_id(f_name)+'").val("");' + return '$("#'+input_id(f_name)+'").typeahead(' \ + '"val", ' \ + 'engine_'+f_name+'.get('+str(f_bound.value())+')[0].value' \ + ');\n' \ + '$("#'+hidden_id(f_name)+'").val('+str(f_bound.value())+');' def default_choices( f_value ) : """ The JS script creating the variable choices_ """