mirror of
https://github.com/freedombox/FreedomBox.git
synced 2026-11-29 08:25:10 +00:00
refactored actions to use exceptions instead of return-values
This commit is contained in:
1 parent
a40d42eb60
commit
0d5636a900
12 files changed
+62
-72
No files matched your search
+13
-12
@@ -32,6 +32,7 @@ import socket
|
||||
import actions
|
||||
import cfg
|
||||
import util
|
||||
from errors import ActionError
|
||||
|
||||
|
||||
LOGGER = logging.getLogger(__name__)
|
||||
@@ -133,20 +134,22 @@ def get_status():
|
||||
def _apply_changes(request, old_status, new_status):
|
||||
"""Apply the form changes"""
|
||||
if old_status['hostname'] != new_status['hostname']:
|
||||
if not set_hostname(new_status['hostname']):
|
||||
messages.error(request, _('Setting hostname failed'))
|
||||
try:
|
||||
set_hostname(new_status['hostname'])
|
||||
except (ActionError, ValueError) as err:
|
||||
messages.error(request, _('Error setting hostname: %s') %
|
||||
err.message)
|
||||
else:
|
||||
messages.success(request, _('Hostname set'))
|
||||
else:
|
||||
messages.info(request, _('Hostname is unchanged'))
|
||||
|
||||
if old_status['time_zone'] != new_status['time_zone']:
|
||||
output, error = actions.superuser_run('timezone-change',
|
||||
[new_status['time_zone']])
|
||||
del output # Unused
|
||||
if error:
|
||||
messages.error(request,
|
||||
_('Error setting time zone - %s') % error)
|
||||
try:
|
||||
actions.superuser_run('timezone-change', [new_status['time_zone']])
|
||||
except (ActionError, ValueError) as err:
|
||||
messages.error(request, _('Error setting time zone: %s') %
|
||||
err.message)
|
||||
else:
|
||||
messages.success(request, _('Time zone set'))
|
||||
else:
|
||||
@@ -164,7 +167,5 @@ def set_hostname(hostname):
|
||||
actions.superuser_run('xmpp-pre-hostname-change')
|
||||
actions.superuser_run('hostname-change', hostname)
|
||||
actions.superuser_run('xmpp-hostname-change', hostname, async=True)
|
||||
except OSError:
|
||||
return False
|
||||
|
||||
return True
|
||||
except OSError as err:
|
||||
raise ActionError(err.message)
|
||||
@@ -25,6 +25,7 @@ from gettext import gettext as _
|
||||
|
||||
import actions
|
||||
import cfg
|
||||
from errors import ActionError
|
||||
|
||||
|
||||
def init():
|
||||
@@ -43,7 +44,12 @@ def index(request):
|
||||
@login_required
|
||||
def test(request):
|
||||
"""Run diagnostics and the output page"""
|
||||
output, error = actions.superuser_run("diagnostic-test")
|
||||
output = ""
|
||||
error = ""
|
||||
try:
|
||||
output = actions.superuser_run("diagnostic-test")
|
||||
except ActionError as err:
|
||||
error = err.message
|
||||
return TemplateResponse(request, 'diagnostics_test.html',
|
||||
{'title': _('Diagnostic Test'),
|
||||
'diagnostics_output': output,
|
||||
|
||||
@@ -140,16 +140,11 @@ def on_service_enabled(sender, service_id, enabled, **kwargs):
|
||||
def _run(arguments, superuser=False):
|
||||
"""Run an given command and raise exception if there was an error"""
|
||||
command = 'firewall'
|
||||
|
||||
LOGGER.info('Running command - %s, %s, %s', command, arguments, superuser)
|
||||
|
||||
if superuser:
|
||||
output, error = actions.superuser_run(command, arguments)
|
||||
output = actions.superuser_run(command, arguments)
|
||||
else:
|
||||
output, error = actions.run(command, arguments)
|
||||
|
||||
if error:
|
||||
raise Exception('Error setting/getting firewalld confguration - %s'
|
||||
% error)
|
||||
output = actions.run(command, arguments)
|
||||
|
||||
return output
|
||||
@@ -53,10 +53,7 @@ def index(request):
|
||||
|
||||
def get_status():
|
||||
"""Return the current status"""
|
||||
output, error = actions.run('owncloud-setup', 'status')
|
||||
if error:
|
||||
raise Exception('Error getting ownCloud status: %s' % error)
|
||||
|
||||
output = actions.run('owncloud-setup', 'status')
|
||||
return {'enabled': 'enable' in output.split()}
|
||||
|
||||
|
||||
|
||||
@@ -6,23 +6,18 @@ from gettext import gettext as _
|
||||
|
||||
import actions
|
||||
import cfg
|
||||
from errors import ActionError
|
||||
|
||||
|
||||
def get_modules_available():
|
||||
"""Return list of all modules"""
|
||||
output, error = actions.run('module-manager', ['list-available'])
|
||||
if error:
|
||||
raise Exception('Error getting modules: %s' % error)
|
||||
|
||||
output = actions.run('module-manager', ['list-available'])
|
||||
return output.split()
|
||||
|
||||
|
||||
def get_modules_enabled():
|
||||
"""Return list of all modules"""
|
||||
output, error = actions.run('module-manager', ['list-enabled'])
|
||||
if error:
|
||||
raise Exception('Error getting enabled modules - %s' % error)
|
||||
|
||||
output = actions.run('module-manager', ['list-enabled'])
|
||||
return output.split()
|
||||
|
||||
|
||||
@@ -88,13 +83,12 @@ def _apply_changes(request, old_status, new_status):
|
||||
|
||||
module = field.split('_enabled')[0]
|
||||
if enabled:
|
||||
output, error = actions.superuser_run(
|
||||
'module-manager', ['enable', cfg.python_root, module])
|
||||
del output # Unused
|
||||
|
||||
# TODO: need to get plinth to load the module we just
|
||||
# enabled
|
||||
if error:
|
||||
try:
|
||||
actions.superuser_run(
|
||||
'module-manager', ['enable', cfg.python_root, module])
|
||||
except ActionError:
|
||||
# TODO: need to get plinth to load the module we just
|
||||
# enabled
|
||||
messages.error(
|
||||
request, _('Error enabling module - {module}').format(
|
||||
module=module))
|
||||
@@ -103,13 +97,12 @@ def _apply_changes(request, old_status, new_status):
|
||||
request, _('Module enabled - {module}').format(
|
||||
module=module))
|
||||
else:
|
||||
output, error = actions.superuser_run(
|
||||
'module-manager', ['disable', cfg.python_root, module])
|
||||
del output # Unused
|
||||
|
||||
# TODO: need a smoother way for plinth to unload the
|
||||
# module
|
||||
if error:
|
||||
try:
|
||||
actions.superuser_run(
|
||||
'module-manager', ['disable', cfg.python_root, module])
|
||||
except ActionError:
|
||||
# TODO: need a smoother way for plinth to unload the
|
||||
# module
|
||||
messages.error(
|
||||
request, _('Error disabling module - {module}').format(
|
||||
module=module))
|
||||
|
||||
@@ -196,16 +196,10 @@ def _apply_changes(request, old_status, new_status):
|
||||
def _run(arguments, superuser=True):
|
||||
"""Run an given command and raise exception if there was an error"""
|
||||
command = 'pagekite-configure'
|
||||
|
||||
LOGGER.info('Running command - %s, %s, %s', command, arguments, superuser)
|
||||
|
||||
if superuser:
|
||||
output, error = actions.superuser_run(command, arguments)
|
||||
output = actions.superuser_run(command, arguments)
|
||||
else:
|
||||
output, error = actions.run(command, arguments)
|
||||
|
||||
if error:
|
||||
raise Exception('Error setting/getting PageKite confguration - %s'
|
||||
% error)
|
||||
|
||||
output = actions.run(command, arguments)
|
||||
return output
|
||||
+1
-2
@@ -36,8 +36,7 @@ def init():
|
||||
@login_required
|
||||
def index(request):
|
||||
"""Service the index page"""
|
||||
output, error = actions.superuser_run("tor-get-ports")
|
||||
del error # Unused
|
||||
output = actions.superuser_run("tor-get-ports")
|
||||
|
||||
port_info = output.split("\n")
|
||||
tor_ports = {}
|
||||
|
||||
+3
-12
@@ -88,10 +88,7 @@ def configure(request):
|
||||
|
||||
def get_status():
|
||||
"""Return the current status"""
|
||||
output, error = actions.run('xmpp-setup', 'status')
|
||||
if error:
|
||||
raise Exception('Error getting status: %s' % error)
|
||||
|
||||
output = actions.run('xmpp-setup', 'status')
|
||||
return {'inband_enabled': 'inband_enable' in output.split()}
|
||||
|
||||
|
||||
@@ -111,11 +108,7 @@ def _apply_changes(request, old_status, new_status):
|
||||
option = 'noinband_enable'
|
||||
|
||||
LOGGER.info('Option - %s', option)
|
||||
|
||||
_output, error = actions.superuser_run('xmpp-setup', [option])
|
||||
del _output # Unused
|
||||
if error:
|
||||
raise Exception('Error running command - %s' % error)
|
||||
actions.superuser_run('xmpp-setup', [option])
|
||||
|
||||
|
||||
class RegisterForm(forms.Form): # pylint: disable-msg=W0232
|
||||
@@ -151,10 +144,8 @@ def register(request):
|
||||
|
||||
def _register_user(request, data):
|
||||
"""Register a new XMPP user"""
|
||||
output, error = actions.superuser_run(
|
||||
output = actions.superuser_run(
|
||||
'xmpp-register', [data['username'], data['password']])
|
||||
if error:
|
||||
raise Exception('Error registering user - %s' % error)
|
||||
|
||||
if 'successfully registered' in output:
|
||||
messages.success(request, _('Registered account for %s') %
|
||||
|
||||
Reference in new issue
Block a user