added dynamic form fields and fixed support for users without javascript support

This commit is contained in:
Daniel Steglich 2015-03-02 20:57:50 +01:00
parent ffcfccb20f
commit 7e218b06ed
2 changed files with 84 additions and 80 deletions

View File

@ -71,17 +71,19 @@ class TrimmedCharField(forms.CharField):
class ConfigureForm(forms.Form): class ConfigureForm(forms.Form):
"""Form to configure the dynamic DNS client""" """Form to configure the dynamic DNS client"""
enabled = forms.BooleanField(label=_('Enable Dynamic DNS'), enabled = forms.BooleanField(label=_('Enable Dynamic DNS'),
required=False) required=False,widget=forms.CheckboxInput
(attrs={'onclick': 'mod_form();'}))
dynamicdns_service = forms.ChoiceField(label=_('Service type'), dynamicdns_service = forms.ChoiceField(label=_('Service type'),
help_text=_('Please choose a update protocol according to your provider.\ help_text=_('Please choose a update protocol according to your provider.\
We recommend the GnudIP protocol. If your provider does not \ We recommend the GnudIP protocol. If your provider does not \
support the GnudIP protocol or your provider is not listed \ support the GnudIP protocol or your provider is not listed \
you may use the update URL of your provider.'), you may use the update URL of your provider.'),
choices=(('1', 'GnudIP'), choices=(('1', 'GnuDIP'),
('2', 'noip.com'), ('2', 'noip.com'),
('3', 'selfhost.bz'), ('3', 'selfhost.bz'),
('4', 'other Update URL'))) ('4', 'other Update URL')),
widget=forms.Select(attrs={"onChange":'mod_form()'}))
dynamicdns_server = TrimmedCharField( dynamicdns_server = TrimmedCharField(
label=_('GnudIP Server Address'), label=_('GnudIP Server Address'),
@ -232,7 +234,27 @@ def get_status():
if len(details) > 5: if len(details) > 5:
status['dynamicdns_ipurl'] = details[5] status['dynamicdns_ipurl'] = details[5]
else: else:
status['dynamicdns_secret'] = '' status['dynamicdns_ipurl'] = ''
if len(details) > 6:
status['dynamicdns_update_url'] = details[6]
else:
status['dynamicdns_update_url'] = ''
if len(details) > 7:
status['disable_SSL_cert_check'] = details[7]
else:
status['disable_SSL_cert_check'] = ''
if len(details) > 8:
status['use_http_basic_auth'] = details[8]
else:
status['use_http_basic_auth'] = ''
if status['dynamicdns_server'] is not '':
status['dynamicdns_service'] = '1'
else:
status['dynamicdns_service'] = '4'
return status return status
@ -258,6 +280,12 @@ def _apply_changes(request, old_status, new_status):
new_status['dynamicdns_secret'] or \ new_status['dynamicdns_secret'] or \
old_status['dynamicdns_ipurl'] != \ old_status['dynamicdns_ipurl'] != \
new_status['dynamicdns_ipurl'] or \ new_status['dynamicdns_ipurl'] or \
old_status['dynamicdns_update_url'] != \
new_status['dynamicdns_update_url'] or \
old_status['disable_SSL_cert_check'] != \
new_status['disable_SSL_cert_check'] or \
old_status['use_http_basic_auth'] != \
new_status['use_http_basic_auth'] or \
old_status['enabled'] != \ old_status['enabled'] != \
new_status['enabled']: new_status['enabled']:
@ -265,7 +293,10 @@ def _apply_changes(request, old_status, new_status):
'-d', new_status['dynamicdns_domain'], '-d', new_status['dynamicdns_domain'],
'-u', new_status['dynamicdns_user'], '-u', new_status['dynamicdns_user'],
'-p', new_status['dynamicdns_secret'], '-p', new_status['dynamicdns_secret'],
'-I', new_status['dynamicdns_ipurl']]) '-I', new_status['dynamicdns_ipurl'],
'-U', new_status['dynamicdns_update_url'],
'-c', new_status['disable_SSL_cert_check'],
'-b', new_status['use_http_basic_auth']])
if old_status['enabled']: if old_status['enabled']:
_run(['stop']) _run(['stop'])

View File

@ -27,15 +27,21 @@
{% include 'bootstrapform/field.html' with field=form.enabled %} {% include 'bootstrapform/field.html' with field=form.enabled %}
<div id='dynamicdns-post-enabled-form' <div id='dynamicdns-post-enabled-form'>
style='display: {{ form.enabled.value|yesno:'block,none' }};'>
<h3>Dynamic DNS type</h3> <h3>Dynamic DNS type</h3>
<div id='dynamicdns-no-js'>
You have disabled Javascript. Dynamic form mode is disabled. Please fill
only "GnudIP Server Address" or "Update URL" field.</br><hr>
</div>
<div id='div-dynamicdns-dropdown' style="display:none">
{% include 'bootstrapform/field.html' with field=form.dynamicdns_service %} {% include 'bootstrapform/field.html' with field=form.dynamicdns_service %}
<div id='dynamicdns-gnudip'> </div>
<div id='div-dynamicdns-gnudip'>
{% include 'bootstrapform/field.html' with field=form.dynamicdns_server %} {% include 'bootstrapform/field.html' with field=form.dynamicdns_server %}
</div> </div>
<div id='dynamicdns-updateurl'> <div id='div-dynamicdns-updateurl'>
{% include 'bootstrapform/field.html' with field=form.dynamicdns_update_url %} {% include 'bootstrapform/field.html' with field=form.dynamicdns_update_url %}
{% include 'bootstrapform/field.html' with field=form.disable_SSL_cert_check %} {% include 'bootstrapform/field.html' with field=form.disable_SSL_cert_check %}
{% include 'bootstrapform/field.html' with field=form.use_http_basic_auth %} {% include 'bootstrapform/field.html' with field=form.use_http_basic_auth %}
@ -59,85 +65,52 @@
{% block page_js %} {% block page_js %}
<script type="text/javascript"> <script type="text/javascript">
window.onload = function() {mod_form()}
window.onload = function() {hide()}
(function($) {
$('#id_dynamicdns-enabled').change(function() {
if ($('#id_dynamicdns-enabled').prop('checked')) {
$('#dynamicdns-post-enabled-form').show('slow');
} else {
$('#dynamicdns-post-enabled-form').hide('slow');
}
});
$('#id_dynamicdns-dynamicdns_service').change(function() {
//$(this).find('option:selected').text() == GnudIP
if ($(this).val() == 1){
$('#dynamicdns-updateurl').hide();
$('#dynamicdns-gnudip').show('slow');
$('#id_dynamicdns-dynamicdns_update_url').val("");
}
if ($(this).val() == 2){
$('#dynamicdns-updateurl').show('slow');
$('#dynamicdns-gnudip').hide();
$('#id_dynamicdns-dynamicdns_server').val("");
$('#id_dynamicdns-dynamicdns_update_url').val("https://noip.com/update.php?user=$user&pass=$pass&domain=$domain&IP=foo$ip");
}
if ($(this).val() == 3){
$('#dynamicdns-updateurl').show('slow');
$('#dynamicdns-gnudip').hide();
$('#id_dynamicdns-dynamicdns_server').val("");
$('#id_dynamicdns-dynamicdns_update_url').val("https://selfhost.bz/update.php?user=$user&pass=$pass&domain=$domain&IP=foo$ip");
}
if ($(this).val() == 4){
$('#dynamicdns-updateurl').show('slow');
$('#dynamicdns-gnudip').hide();
$('#id_dynamicdns-dynamicdns_server').val("");
$('#id_dynamicdns-dynamicdns_update_url').val("https://example.com/update.php?user=$user&pass=$pass&domain=$domain&IP=foo$ip");
}
});
})(jQuery);
function show_pass() function show_pass()
{ {
if(document.getElementById('id_dynamicdns-showpw').checked){ if(document.getElementById('id_dynamicdns-showpw').checked){
document.getElementById("id_dynamicdns-dynamicdns_secret").type='text';} document.getElementById('id_dynamicdns-dynamicdns_secret').type='text';}
else else
document.getElementById("id_dynamicdns-dynamicdns_secret").type='password'; document.getElementById('id_dynamicdns-dynamicdns_secret').type='password';
} }
function hide() function mod_form()
{ {
if ($('#id_dynamicdns-enabled').prop('checked')) { document.getElementById('dynamicdns-no-js').style.display = 'none';
//$(this).find('option:selected').text() == GnudIP document.getElementById('div-dynamicdns-dropdown').style.display = 'block';
if ($(this).val() == 1){
$('#dynamicdns-updateurl').hide(); if ( document.getElementById('id_dynamicdns-enabled').checked ) {
$('#id_dynamicdns-dynamicdns_update_url').val(""); document.getElementById('dynamicdns-post-enabled-form').style.display = 'block';
} var dropdown = document.getElementById('id_dynamicdns-dynamicdns_service');
if ($(this).val() == 2){ var service_type = dropdown.options[dropdown.selectedIndex].value;
$('#dynamicdns-gnudip').hide(); if (service_type == 1){
$('#id_dynamicdns-dynamicdns_server').val(""); document.getElementById('div-dynamicdns-updateurl').style.display = 'none';
$('#id_dynamicdns-dynamicdns_update_url').val("https://noip.com/update.php?user=$user&pass=$pass&domain=$domain&IP=foo$ip"); document.getElementById('div-dynamicdns-gnudip').style.display = 'block';
} document.getElementById('id_dynamicdns-dynamicdns_update_url').value='';
if ($(this).val() == 3){ }
$('#dynamicdns-gnudip').hide(); if (service_type == 2){
$('#id_dynamicdns-dynamicdns_server').val(""); document.getElementById('div-dynamicdns-gnudip').style.display = 'none';
$('#id_dynamicdns-dynamicdns_update_url').val("https://selfhost.bz/update.php?user=$user&pass=$pass&domain=$domain&IP=foo$ip"); document.getElementById('div-dynamicdns-updateurl').style.display = 'block';
} document.getElementById('id_dynamicdns-dynamicdns_update_url').value='noip';
if ($(this).val() == 4){ document.getElementById('id_dynamicdns-dynamicdns_server').value='';
$('#dynamicdns-gnudip').hide(); }
$('#id_dynamicdns-dynamicdns_server').val(""); if (service_type == 3){
$('#id_dynamicdns-dynamicdns_update_url').val("https://example.com/update.php?user=$user&pass=$pass&domain=$domain&IP=foo$ip"); document.getElementById('div-dynamicdns-gnudip').style.display = 'none';
} document.getElementById('div-dynamicdns-updateurl').style.display = 'block';
} else { document.getElementById('id_dynamicdns-dynamicdns_update_url').value='selfhost';
$('#dynamicdns-post-enabled-form').hide('slow'); document.getElementById('id_dynamicdns-dynamicdns_server').value='';
} }
} if (service_type == 4){
document.getElementById('div-dynamicdns-gnudip').style.display = 'none';
document.getElementById('div-dynamicdns-updateurl').style.display = 'block';
document.getElementById('id_dynamicdns-dynamicdns_update_url').value='';
document.getElementById('id_dynamicdns-dynamicdns_server').value='';
}
}else{
document.getElementById('dynamicdns-post-enabled-form').style.display = 'none';
}
}
</script> </script>
{% endblock %} {% endblock %}