From 474860f97413a4106176a3b9ca27f3ba3ff1de7b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ma=C3=ABl=20Kervella?= Date: Sat, 7 Oct 2017 16:58:09 +0000 Subject: [PATCH] =?UTF-8?q?Ajoute=20des=20param=C3=A8tres=20dans=20le=20bf?= =?UTF-8?q?t=20tag?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Les paramètres concernant bft sont maintenant spécifiés via un dictionnaire pour alleger un peu le code. Ajout des paramètres customisant l'engine et la possibilité de reload quand un autre elt a changé --- machines/templates/machines/machine.html | 16 +++--------- .../templatetags/bootstrap_form_typeahead.py | 26 ++++++++++++++----- machines/views.py | 6 ++--- 3 files changed, 26 insertions(+), 22 deletions(-) diff --git a/machines/templates/machines/machine.html b/machines/templates/machines/machine.html index 6982ae1a..4b308a11 100644 --- a/machines/templates/machines/machine.html +++ b/machines/templates/machines/machine.html @@ -48,19 +48,11 @@ with this program; if not, write to the Free Software Foundation, Inc., {% endif %} {% if interfaceform %}

Interface

- {% if i_choices %} - {% if i_match_func %} - {% bootstrap_form_typeahead interfaceform 'ipv4' choices=i_choices match_func=i_match_func %} - {% else %} - {% bootstrap_form_typeahead interfaceform 'ipv4' choices=i_choices %} - {% endif %} - {% else %} - {% if i_match_func %} - {% bootstrap_form_typeahead interfaceform 'ipv4' match_func=i_match_func %} - {% else %} + {% if i_bft_param %} + {% bootstrap_form_typeahead interfaceform 'ipv4' bft_param=i_bft_param %} + {% else %} {% bootstrap_form_typeahead interfaceform 'ipv4' %} - {% endif %} - {% endif %} + {% endif %} {% endif %} {% if domainform %}

Domaine

diff --git a/machines/templatetags/bootstrap_form_typeahead.py b/machines/templatetags/bootstrap_form_typeahead.py index 8a929fbf..45680fa1 100644 --- a/machines/templatetags/bootstrap_form_typeahead.py +++ b/machines/templatetags/bootstrap_form_typeahead.py @@ -84,12 +84,14 @@ def bootstrap_form_typeahead(django_form, typeahead_fields, *args, **kwargs): {% bootstrap_form_typeahead form 'ipv4' choices='[...]' %} """ - t_fields = typeahead_fields.split(',') - exclude = kwargs.get('exclude', None) + params = kwargs.get('bft_param', {}) + exclude = params.get('exclude', None) exclude = exclude.split(',') if exclude else [] - t_choices = kwargs.get('choices', {}) - t_match_func = kwargs.get('match_func', {}) + t_choices = params.get('choices', {}) + t_engine = params.get('engine', {}) + t_match_func = params.get('match_func', {}) + t_update_on = params.get('update_on', {}) hidden = [h.name for h in django_form.hidden_fields()] form = '' @@ -116,7 +118,9 @@ def bootstrap_form_typeahead(django_form, typeahead_fields, *args, **kwargs): f_name, f_value, t_choices, - t_match_func + t_engine, + t_match_func, + t_update_on ) ) else: @@ -145,19 +149,25 @@ def hidden_tag( f_bound, f_name ): } ) -def typeahead_js( f_name, f_value, t_choices, t_match_func ) : +def typeahead_js( f_name, f_value, + t_choices, t_engine, t_match_func, t_update_on ) : choices = mark_safe(t_choices[f_name]) if f_name in t_choices.keys() \ else default_choices( f_value ) + engine = mark_safe(t_engine[f_name]) if f_name in t_engine.keys() \ + else default_engine ( f_name ) + match_func = mark_safe(t_match_func[f_name]) \ if f_name in t_match_func.keys() \ else default_match_func( f_name ) + update_on = t_update_on[f_name] if f_name in t_update_on.keys() else [] + js_content = \ 'var choices_'+f_name+' = ' + choices + ';\n' + \ 'var setup_'+f_name+' = function() {\n' + \ - 'var engine_'+f_name+' = ' + default_engine() + ';\n' + \ + 'var engine_'+f_name+' = ' + engine + ';\n' + \ '$("#'+input_id(f_name) + '").typeahead("destroy");\n' + \ '$("#'+input_id(f_name) + '").typeahead(\n' + \ default_datasets( f_name, match_func ) + '\n' + \ @@ -171,6 +181,8 @@ def typeahead_js( f_name, f_value, t_choices, t_match_func ) : '"typeahead:change", ' + \ typeahead_change( f_name ) + '\n' + \ ');\n' + for u_id in update_on : + js_content += '$("#'+u_id+'").change( setup_'+f_name+' );\n' js_content += '$("#'+input_id(f_name)+'").ready( setup_'+f_name+' );\n' return render_tag( 'script', content=mark_safe( js_content ) ) diff --git a/machines/views.py b/machines/views.py index 1aceda3f..ddc5b18f 100644 --- a/machines/views.py +++ b/machines/views.py @@ -159,7 +159,7 @@ def new_machine(request, userid): return redirect("/users/profil/" + str(user.id)) 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) + return form({'machineform': machine, 'interfaceform': interface, 'domainform': domain, 'i_bft_param': {'choices': i_choices, 'match_func': i_match_func}}, 'machines/machine.html', request) @login_required def edit_interface(request, interfaceid): @@ -198,7 +198,7 @@ def edit_interface(request, interfaceid): return redirect("/users/profil/" + str(interface.machine.user.id)) 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) + return form({'machineform': machine_form, 'interfaceform': interface_form, 'domainform': domain_form, 'i_bft_param': {'choices': i_choices, 'match_func': i_match_func}}, 'machines/machine.html', request) @login_required def del_machine(request, machineid): @@ -256,7 +256,7 @@ def new_interface(request, machineid): return redirect("/users/profil/" + str(machine.user.id)) 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) + return form({'interfaceform': interface_form, 'domainform': domain_form, 'i_bft_param': { 'choices': i_choices, 'match_func': i_match_func }}, 'machines/machine.html', request) @login_required def del_interface(request, interfaceid):