refactoring, made the code more readable, use consistent indentions, whitespaces and linebreaks next to brackets

This commit is contained in:
Daniel Steglich 2015-03-12 17:07:21 +01:00
parent e94c85c357
commit 337fa56b15
3 changed files with 96 additions and 90 deletions

View File

@ -30,6 +30,13 @@ from plinth import package
LOGGER = logging.getLogger(__name__) LOGGER = logging.getLogger(__name__)
EMPTYSTRING = 'none' EMPTYSTRING = 'none'
SERVICE = {
"GnuDIP": "1",
"noip": "2",
"selfhost": "3",
"freedns": "4",
"other": "5",
}
subsubmenu = [{'url': reverse_lazy('dynamicdns:index'), subsubmenu = [{'url': reverse_lazy('dynamicdns:index'),
'text': _('About')}, 'text': _('About')},
@ -52,11 +59,9 @@ def init():
def index(request): def index(request):
"""Serve dynamic DNS page""" """Serve dynamic DNS page"""
index_subsubmenu = subsubmenu
return TemplateResponse(request, 'dynamicdns.html', return TemplateResponse(request, 'dynamicdns.html',
{'title': _('dynamicdns'), {'title': _('dynamicdns'),
'subsubmenu': index_subsubmenu}) 'subsubmenu': subsubmenu})
class TrimmedCharField(forms.CharField): class TrimmedCharField(forms.CharField):
@ -182,7 +187,7 @@ class ConfigureForm(forms.Form):
old_dynamicdns_secret = self.initial['dynamicdns_secret'] old_dynamicdns_secret = self.initial['dynamicdns_secret']
"""clear the fields which are not in use""" """clear the fields which are not in use"""
if service_type == '1': if service_type == SERVICE['GnuDIP']:
dynamicdns_update_url = "" dynamicdns_update_url = ""
else: else:
dynamicdns_server = "" dynamicdns_server = ""
@ -215,13 +220,13 @@ def configure(request):
form = None form = None
if request.method == 'POST': if request.method == 'POST':
form = ConfigureForm(request.POST, initial=status, prefix='dynamicdns') form = ConfigureForm(request.POST, initial=status)
if form.is_valid(): if form.is_valid():
_apply_changes(request, status, form.cleaned_data) _apply_changes(request, status, form.cleaned_data)
status = get_status() status = get_status()
form = ConfigureForm(initial=status, prefix='dynamicdns') form = ConfigureForm(initial=status)
else: else:
form = ConfigureForm(initial=status, prefix='dynamicdns') form = ConfigureForm(initial=status)
return TemplateResponse(request, 'dynamicdns_configure.html', return TemplateResponse(request, 'dynamicdns_configure.html',
{'title': _('Configure dynamicdns Client'), {'title': _('Configure dynamicdns Client'),
@ -323,11 +328,11 @@ def get_status():
status['use_http_basic_auth'] = False status['use_http_basic_auth'] = False
if not status['dynamicdns_server'] and not status['dynamicdns_update_url']: if not status['dynamicdns_server'] and not status['dynamicdns_update_url']:
status['service_type'] = '1' status['service_type'] = SERVICE['GnuDIP']
elif not status['dynamicdns_server'] and status['dynamicdns_update_url']: elif not status['dynamicdns_server'] and status['dynamicdns_update_url']:
status['service_type'] = '5' status['service_type'] = SERVICE['other']
else: else:
status['service_type'] = '1' status['service_type'] = SERVICE['GnuDIP']
return status return status
@ -349,7 +354,7 @@ def _apply_changes(request, old_status, new_status):
if new_status['dynamicdns_server'] == '': if new_status['dynamicdns_server'] == '':
new_status['dynamicdns_server'] = EMPTYSTRING new_status['dynamicdns_server'] = EMPTYSTRING
if new_status['service_type'] == '1': if new_status['service_type'] == SERVICE['GnuDIP']:
new_status['dynamicdns_update_url'] = EMPTYSTRING new_status['dynamicdns_update_url'] = EMPTYSTRING
else: else:
new_status['dynamicdns_server'] = EMPTYSTRING new_status['dynamicdns_server'] = EMPTYSTRING

View File

@ -37,5 +37,10 @@
target='_blank'>gnudip.datasystems24.net</a> or you may find free update target='_blank'>gnudip.datasystems24.net</a> or you may find free update
URL based services on URL based services on
<a href='http://freedns.afraid.org/' target='_blank'> <a href='http://freedns.afraid.org/' target='_blank'>
freedns.afraid.org</a></p> freedns.afraid.org</a>
</br> </br>
If your freedombox is connected behind some NAT router, don't forget
to add portforwarding (i.e. forward some standard ports like 80 and 443)
to your freedombox device.
</p>
{% endblock %} {% endblock %}

View File

@ -24,12 +24,12 @@
<form class="form" method="post"> <form class="form" method="post">
{% csrf_token %} {% csrf_token %}
<div id='dynamicdns-no-js'> <noscript>
You have disabled Javascript. Dynamic form mode is disabled and You have disabled Javascript. Dynamic form mode is disabled and
some helper functions may not work <br> some helper functions may not work <br>
(but the main functionality should work) (but the main functionality should work)
<br><hr> <br><hr>
</div> </noscript>
{{ form|bootstrap }} {{ form|bootstrap }}
@ -50,118 +50,114 @@
var FREEDNS = 'https://freedns.afraid.org/dynamic/update.php?' + var FREEDNS = 'https://freedns.afraid.org/dynamic/update.php?' +
'_YOURAPIKEYHERE_' '_YOURAPIKEYHERE_'
//hide javascript warning
$('#dynamicdns-no-js').hide();
//hide ALL form fields //hide ALL form fields
$('.form-group').hide(); $('.form-group').hide();
//show the enable checkbox //show the enable checkbox
$('#id_dynamicdns-enabled').closest('.form-group').show(); $('#id_enabled').closest('.form-group').show();
if ($('#id_dynamicdns-enabled').prop('checked')) { if ($('#id_enabled').prop('checked')) {
//show all form fields //show all form fields
show_all(); show_all();
//set the selectbox to the last configured value //set the selectbox to the last configured value
select_service(); select_service();
} }
$('#id_dynamicdns-enabled').change(function() { $('#id_enabled').change(function() {
if ($('#id_dynamicdns-enabled').prop('checked')) { if ($('#id_enabled').prop('checked')) {
show_all(); show_all();
if ($("#id_dynamicdns-service_type option:selected").text() == "GnuDIP") { if ($("#id_service_type option:selected").text() == "GnuDIP") {
set_gnudip_mode() set_gnudip_mode()
}else{ } else {
set_update_url_mode(); set_update_url_mode();
} }
} else { } else {
$('.form-group').hide(); $('.form-group').hide();
$('#id_dynamicdns-enabled').closest('.form-group').show(); $('#id_enabled').closest('.form-group').show();
} }
}); });
$('#id_dynamicdns-service_type').change(function() { $('#id_service_type').change(function() {
if ($("#id_dynamicdns-service_type option:selected").text() == "GnuDIP") { var service_type = $("#id_service_type option:selected").text();
set_gnudip_mode() if ( service_type == "GnuDIP" ) {
}else{ set_gnudip_mode()
} else {
set_update_url_mode(); set_update_url_mode();
if ($("#id_dynamicdns-service_type option:selected").text() == "noip.com") { if ( service_type == "noip.com" ) {
$('#id_dynamicdns-dynamicdns_update_url').val(NOIP); $( '#id_dynamicdns_update_url' ).val( NOIP );
$('#id_dynamicdns-use_http_basic_auth').prop('checked', true); $( '#id_use_http_basic_auth' ).prop( 'checked', true);
}else{ } else {
$('#id_dynamicdns-use_http_basic_auth').prop('checked', false); $( '#id_use_http_basic_auth' ).prop( 'checked', false);
} }
if ($("#id_dynamicdns-service_type option:selected").text() == "selfhost.bz") { if ( service_type == "selfhost.bz" ) {
$('#id_dynamicdns-dynamicdns_update_url').val(SELFHOST); $( '#id_dynamicdns_update_url' ).val(SELFHOST);
} }
if ($("#id_dynamicdns-service_type option:selected").text() == "freedns.afraid.org") { if ( service_type == "freedns.afraid.org" ) {
$('#id_dynamicdns-dynamicdns_update_url').val(FREEDNS); $('#id_dynamicdns_update_url').val(FREEDNS);
} }
if ($("#id_dynamicdns-service_type option:selected").text() == "other update URL") { if ( service_type == "other update URL" ) {
$('#id_dynamicdns-dynamicdns_update_url').val(''); $('#id_dynamicdns_update_url').val('');
} }
} }
}); });
$('#id_dynamicdns-showpw').change(function() { $('#id_showpw').change(function() {
//changing type attribute from password to text is prevented by //changing type attribute from password to text is prevented by
//most browsers make a new form field works for me //most browsers make a new form field works for me
if ($('#id_dynamicdns-showpw').prop('checked')) { if ($('#id_showpw').prop('checked')) {
$('#id_dynamicdns-dynamicdns_secret').replaceWith( $('#id_dynamicdns_secret').replaceWith(
$('#id_dynamicdns-dynamicdns_secret').clone().attr( $('#id_dynamicdns_secret').clone().attr(
'type', 'text')); 'type', 'text'));
}else{ } else {
$('#id_dynamicdns-dynamicdns_secret').replaceWith( $('#id_dynamicdns_secret').replaceWith(
$('#id_dynamicdns-dynamicdns_secret').clone().attr( $('#id_dynamicdns_secret').clone().attr(
'type', 'password')); 'type', 'password'));
} }
}); });
function select_service() function select_service() {
{ var update_url = $("#id_dynamicdns_update_url").val()
if ( $("#id_dynamicdns-dynamicdns_server").val().length == 0 ) { if ( $("#id_dynamicdns_server").val().length == 0 ) {
set_update_url_mode() set_update_url_mode()
if($("#id_dynamicdns-dynamicdns_update_url").val() == NOIP){ if ( update_url == NOIP) {
$("#id_dynamicdns-service_type").val(2); $("#id_service_type").val(2);
}else if($("#id_dynamicdns-dynamicdns_update_url").val() == SELFHOST){ } else if ( update_url == SELFHOST) {
$("#id_dynamicdns-service_type").val(3); $("#id_service_type").val(3);
}else if ($("#id_dynamicdns-dynamicdns_update_url").val() == FREEDNS){ } else if ( update_url == FREEDNS) {
$("#id_dynamicdns-service_type").val(4); $("#id_service_type").val(4);
}else{ } else {
$("#id_dynamicdns-service_type").val(5); $("#id_service_type").val(5);
} }
}else{ } else {
$("#id_dynamicdns-service_type").val(1); $("#id_service_type").val(1);
set_gnudip_mode(); set_gnudip_mode();
} }
} }
function set_gnudip_mode() function set_gnudip_mode() {
{ $('#id_dynamicdns_update_url').closest('.form-group').hide();
$('#id_dynamicdns-dynamicdns_update_url').closest('.form-group').hide(); $('#id_disable_SSL_cert_check').closest('.form-group').hide();
$('#id_dynamicdns-disable_SSL_cert_check').closest('.form-group').hide(); $('#id_use_http_basic_auth').closest('.form-group').hide();
$('#id_dynamicdns-use_http_basic_auth').closest('.form-group').hide(); $('#id_dynamicdns_server').closest('.form-group').show();
$('#id_dynamicdns-dynamicdns_server').closest('.form-group').show();
} }
function set_update_url_mode() function set_update_url_mode() {
{ $('#id_dynamicdns_update_url').closest('.form-group').show();
$('#id_dynamicdns-dynamicdns_update_url').closest('.form-group').show(); $('#id_disable_SSL_cert_check').closest('.form-group').show();
$('#id_dynamicdns-disable_SSL_cert_check').closest('.form-group').show(); $('#id_use_http_basic_auth').closest('.form-group').show();
$('#id_dynamicdns-use_http_basic_auth').closest('.form-group').show(); $('#id_dynamicdns_server').closest('.form-group').hide();
$('#id_dynamicdns-dynamicdns_server').closest('.form-group').hide();
} }
function show_all() function show_all() {
{ $('#id_enabled').closest('.form-group').show();
$('#id_dynamicdns-enabled').closest('.form-group').show(); $('#id_service_type').closest('.form-group').show();
$('#id_dynamicdns-service_type').closest('.form-group').show(); $('#id_dynamicdns_server').closest('.form-group').show();
$('#id_dynamicdns-dynamicdns_server').closest('.form-group').show(); $('#id_dynamicdns_update_url').closest('.form-group').show();
$('#id_dynamicdns-dynamicdns_update_url').closest('.form-group').show(); $('#id_disable_SSL_cert_check').closest('.form-group').show();
$('#id_dynamicdns-disable_SSL_cert_check').closest('.form-group').show(); $('#id_use_http_basic_auth').closest('.form-group').show();
$('#id_dynamicdns-use_http_basic_auth').closest('.form-group').show(); $('#id_dynamicdns_domain').closest('.form-group').show();
$('#id_dynamicdns-dynamicdns_domain').closest('.form-group').show(); $('#id_dynamicdns_user').closest('.form-group').show();
$('#id_dynamicdns-dynamicdns_user').closest('.form-group').show(); $('#id_dynamicdns_secret').closest('.form-group').show();
$('#id_dynamicdns-dynamicdns_secret').closest('.form-group').show(); $('#id_showpw').closest('.form-group').show();
$('#id_dynamicdns-showpw').closest('.form-group').show(); $('#id_dynamicdns_ipurl').closest('.form-group').show();
$('#id_dynamicdns-dynamicdns_ipurl').closest('.form-group').show();
} }
})(jQuery); })(jQuery);
</script> </script>