8
0
Fork 0
mirror of https://gitlab2.federez.net/re2o/re2o synced 2024-11-30 08:32:26 +00:00

Met l'ip actuelle et la bonne value dans le form d'edition d'interface

This commit is contained in:
Maël Kervella 2017-10-07 18:52:49 +00:00
parent b603c1fe7c
commit a8d0c914ca
2 changed files with 12 additions and 8 deletions

View file

@ -65,7 +65,7 @@ class EditInterfaceForm(ModelForm):
self.fields['ipv4'].empty_label = "Assignation automatique de l'ipv4" 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')) self.fields['ipv4'].queryset = IpList.objects.filter(interface__isnull=True).annotate(mtype_id=F('ip_type__machinetype__id'))
# Add it's own address # 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: if "machine" in self.fields:
self.fields['machine'].queryset = Machine.objects.all().select_related('user') 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['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')) 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 # 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: 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(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 AliasForm(ModelForm):
class Meta: class Meta:

View file

@ -180,6 +180,7 @@ def bootstrap_form_typeahead(django_form, typeahead_fields, *args, **kwargs):
typeahead_js( typeahead_js(
f_name, f_name,
f_value, f_value,
f_bound,
t_choices, t_choices,
t_engine, t_engine,
t_match_func, 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 ) : t_choices, t_engine, t_match_func, t_update_on ) :
""" The whole script to use """ """ The whole script to use """
@ -239,7 +240,7 @@ def typeahead_js( f_name, f_value,
'$("#'+input_id(f_name) + '").typeahead(\n' + \ '$("#'+input_id(f_name) + '").typeahead(\n' + \
default_datasets( f_name, match_func ) + '\n' + \ default_datasets( f_name, match_func ) + '\n' + \
');\n' + \ ');\n' + \
reset_input( f_name, f_value ) + '\n' + \ reset_input( f_name, f_bound ) + '\n' + \
'};\n' + \ '};\n' + \
'$("#'+input_id(f_name) + '").bind(\n' + \ '$("#'+input_id(f_name) + '").bind(\n' + \
'"typeahead:select", ' + \ '"typeahead:select", ' + \
@ -254,10 +255,13 @@ def typeahead_js( f_name, f_value,
return render_tag( 'script', content=mark_safe( js_content ) ) 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 """ """ The JS script to reset the fields values """
return '$("#'+input_id(f_name)+'").typeahead("val","");\n' \ return '$("#'+input_id(f_name)+'").typeahead(' \
'$("#'+hidden_id(f_name)+'").val("");' '"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 ) : def default_choices( f_value ) :
""" The JS script creating the variable choices_<fieldname> """ """ The JS script creating the variable choices_<fieldname> """