From a15601026211832b9eb950d497d6ce8fe1db50c7 Mon Sep 17 00:00:00 2001 From: Pierre Cadart Date: Thu, 5 Oct 2017 23:47:33 +0000 Subject: [PATCH] =?UTF-8?q?Permet=20de=20reprendre=20la=20m=C3=AAme=20ip?= =?UTF-8?q?=20quand=20on=20=C3=A9dite=20une=20interface?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- machines/forms.py | 4 ++++ machines/templatetags/bootstrap_form_typeahead.py | 8 ++++++-- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/machines/forms.py b/machines/forms.py index 44e80c25..ca167638 100644 --- a/machines/forms.py +++ b/machines/forms.py @@ -60,6 +60,8 @@ class EditInterfaceForm(ModelForm): 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) + # Add it's own address + self.fields['ipv4'].queryset |= IpList.objects.filter(id=self.fields['ipv4'].get_bound_field(self, 'ipv4').value()) if "machine" in self.fields: self.fields['machine'].queryset = Machine.objects.all().select_related('user') @@ -92,6 +94,8 @@ class BaseEditInterfaceForm(EditInterfaceForm): 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)) + # Add it's own address + self.fields['ipv4'].queryset |= IpList.objects.filter(id=self.fields['ipv4'].get_bound_field(self, 'ipv4').value()) else: self.fields['ipv4'].queryset = IpList.objects.filter(interface__isnull=True) diff --git a/machines/templatetags/bootstrap_form_typeahead.py b/machines/templatetags/bootstrap_form_typeahead.py index 528bd767..60a75484 100644 --- a/machines/templatetags/bootstrap_form_typeahead.py +++ b/machines/templatetags/bootstrap_form_typeahead.py @@ -102,20 +102,21 @@ def input_id( f_name ) : def hidden_id( f_name ): return 'typeahead_hidden_'+f_name -def hidden_tag( f_name ): +def hidden_tag( f_bound, f_name ): return render_tag( 'input', attrs={ 'id': hidden_id(f_name), 'name': f_name, 'type': 'hidden', - 'value': '' + 'value': f_bound.value() } ) def typeahead_full_script( f_name, f_value ) : js_content = \ '$("#'+input_id(f_name)+'").ready( function() {\n' + \ + reset_input( f_name, f_value ) + '\n' + \ typeahead_choices( f_value ) + '\n' + \ typeahead_engine () + '\n' + \ '$("#'+input_id(f_name) + '").typeahead(\n' + \ @@ -128,6 +129,9 @@ def typeahead_full_script( f_name, f_value ) : return render_tag( 'script', content=mark_safe( js_content ) ) +def reset_input( f_name, f_value ) : + return '$("#'+input_id(f_name)+'").val("'+f_value.empty_label+'");' + def typeahead_choices( f_value ) : return 'var choices = [' + \ ', '.join([ \