config: Move the method get_hostname to __init__.py

- formatting and optimization of imports

Closes #1232

Signed-off-by: Joseph Nuthalapati <njoseph@thoughtworks.com>
Reviewed-by: Sunil Mohan Adapa <sunil@medhas.org>
This commit is contained in:
Joseph Nuthalapati 2018-02-12 13:54:20 +05:30 committed by Sunil Mohan Adapa
parent 074ab4a736
commit 5edf7d00fa
No known key found for this signature in database
GPG Key ID: 43EA1CFF0AA7C5F2
2 changed files with 38 additions and 36 deletions

View File

@ -41,6 +41,11 @@ def get_domainname():
return '.'.join(fqdn.split('.')[1:]) return '.'.join(fqdn.split('.')[1:])
def get_hostname():
"""Return the hostname"""
return socket.gethostname()
def init(): def init():
"""Initialize the module""" """Initialize the module"""
menu = main_menu.get('system') menu = main_menu.get('system')

View File

@ -14,34 +14,28 @@
# You should have received a copy of the GNU Affero General Public License # You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>. # along with this program. If not, see <http://www.gnu.org/licenses/>.
# #
""" """
Plinth views for basic system configuration Plinth views for basic system configuration
""" """
from django.utils import translation
from django.utils.translation import ugettext as _
from django.template.response import TemplateResponse
from django.contrib import messages
from plinth.modules import config
from .forms import ConfigurationForm
from plinth.signals import pre_hostname_change, post_hostname_change
from plinth import actions
from plinth.signals import domain_added, domain_removed, domainname_change
from plinth.modules import firewall
from plinth.modules.names import SERVICES
import logging import logging
import socket
from django.contrib import messages
from django.template.response import TemplateResponse
from django.utils import translation
from django.utils.translation import ugettext as _
from plinth import actions
from plinth.modules import config, firewall
from plinth.modules.names import SERVICES
from plinth.signals import (domain_added, domain_removed, domainname_change,
post_hostname_change, pre_hostname_change)
from .forms import ConfigurationForm
LOGGER = logging.getLogger(__name__) LOGGER = logging.getLogger(__name__)
def get_hostname():
"""Return the hostname"""
return socket.gethostname()
def get_language(request): def get_language(request):
"""Return the current language setting""" """Return the current language setting"""
# TODO: Store the language per user in kvstore, # TODO: Store the language per user in kvstore,
@ -64,21 +58,23 @@ def index(request):
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(request) status = get_status(request)
form = ConfigurationForm(initial=status, form = ConfigurationForm(initial=status, prefix='configuration')
prefix='configuration')
else: else:
form = ConfigurationForm(initial=status, prefix='configuration') form = ConfigurationForm(initial=status, prefix='configuration')
return TemplateResponse(request, 'config.html', return TemplateResponse(request, 'config.html', {
{'title': _('General Configuration'), 'title': _('General Configuration'),
'form': form}) 'form': form
})
def get_status(request): def get_status(request):
"""Return the current status""" """Return the current status"""
return {'hostname': get_hostname(), return {
'domainname': config.get_domainname(), 'hostname': config.get_hostname(),
'language': get_language(request)} 'domainname': config.get_domainname(),
'language': get_language(request)
}
def _apply_changes(request, old_status, new_status): def _apply_changes(request, old_status, new_status):
@ -87,7 +83,8 @@ def _apply_changes(request, old_status, new_status):
try: try:
set_hostname(new_status['hostname']) set_hostname(new_status['hostname'])
except Exception as exception: except Exception as exception:
messages.error(request, _('Error setting hostname: {exception}') messages.error(request,
_('Error setting hostname: {exception}')
.format(exception=exception)) .format(exception=exception))
else: else:
messages.success(request, _('Hostname set')) messages.success(request, _('Hostname set'))
@ -96,7 +93,8 @@ def _apply_changes(request, old_status, new_status):
try: try:
set_domainname(new_status['domainname']) set_domainname(new_status['domainname'])
except Exception as exception: except Exception as exception:
messages.error(request, _('Error setting domain name: {exception}') messages.error(request,
_('Error setting domain name: {exception}')
.format(exception=exception)) .format(exception=exception))
else: else:
messages.success(request, _('Domain name set')) messages.success(request, _('Domain name set'))
@ -107,7 +105,8 @@ def _apply_changes(request, old_status, new_status):
translation.activate(language) translation.activate(language)
request.session[translation.LANGUAGE_SESSION_KEY] = language request.session[translation.LANGUAGE_SESSION_KEY] = language
except Exception as exception: except Exception as exception:
messages.error(request, _('Error setting language: {exception}') messages.error(request,
_('Error setting language: {exception}')
.format(exception=exception)) .format(exception=exception))
else: else:
messages.success(request, _('Language changed')) messages.success(request, _('Language changed'))
@ -115,23 +114,21 @@ def _apply_changes(request, old_status, new_status):
def set_hostname(hostname): def set_hostname(hostname):
"""Sets machine hostname to hostname""" """Sets machine hostname to hostname"""
old_hostname = get_hostname() old_hostname = config.get_hostname()
domainname = config.get_domainname() domainname = config.get_domainname()
# Hostname should be ASCII. If it's unicode but passed our # Hostname should be ASCII. If it's unicode but passed our
# valid_hostname check, convert to ASCII. # valid_hostname check, convert to ASCII.
hostname = str(hostname) hostname = str(hostname)
pre_hostname_change.send_robust(sender='config', pre_hostname_change.send_robust(sender='config', old_hostname=old_hostname,
old_hostname=old_hostname,
new_hostname=hostname) new_hostname=hostname)
LOGGER.info('Changing hostname to - %s', hostname) LOGGER.info('Changing hostname to - %s', hostname)
actions.superuser_run('hostname-change', [hostname]) actions.superuser_run('hostname-change', [hostname])
post_hostname_change.send_robust(sender='config', post_hostname_change.send_robust(
old_hostname=old_hostname, sender='config', old_hostname=old_hostname, new_hostname=hostname)
new_hostname=hostname)
LOGGER.info('Setting domain name after hostname change - %s', domainname) LOGGER.info('Setting domain name after hostname change - %s', domainname)
actions.superuser_run('domainname-change', [domainname]) actions.superuser_run('domainname-change', [domainname])