diff --git a/modules/config/config.py b/modules/config/config.py index fe87dee70..81f1688a2 100644 --- a/modules/config/config.py +++ b/modules/config/config.py @@ -20,6 +20,7 @@ Plinth module for configuring timezone, hostname etc. """ from django import forms +from django.contrib import messages from django.core import validators from django.template.response import TemplateResponse from gettext import gettext as _ @@ -100,14 +101,13 @@ def index(request): status = get_status() form = None - messages = [] is_expert = cfg.users.expert(request=request) if request.method == 'POST' and is_expert: form = ConfigurationForm(request.POST, prefix='configuration') # pylint: disable-msg=E1101 if form.is_valid(): - _apply_changes(status, form.cleaned_data, messages) + _apply_changes(request, status, form.cleaned_data) status = get_status() form = ConfigurationForm(initial=status, prefix='configuration') @@ -117,7 +117,6 @@ def index(request): return TemplateResponse(request, 'config.html', {'title': _('General Configuration'), 'form': form, - 'messages_': messages, 'is_expert': is_expert}) @@ -127,27 +126,27 @@ def get_status(): 'time_zone': util.slurp('/etc/timezone').rstrip()} -def _apply_changes(old_status, new_status, messages): +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.append(('error', _('Setting hostname failed'))) + messages.error(request, _('Setting hostname failed')) else: - messages.append(('success', _('Hostname set'))) + messages.success(request, _('Hostname set')) else: - messages.append(('info', _('Hostname is unchanged'))) + 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.append(('error', - _('Error setting time zone - %s') % error)) + messages.error(request, + _('Error setting time zone - %s') % error) else: - messages.append(('success', _('Time zone set'))) + messages.success(request, _('Time zone set')) else: - messages.append(('info', _('Time zone is unchanged'))) + messages.info(request, _('Time zone is unchanged')) def set_hostname(hostname): diff --git a/modules/config/templates/config.html b/modules/config/templates/config.html index 591ad5e5b..4401bdcd3 100644 --- a/modules/config/templates/config.html +++ b/modules/config/templates/config.html @@ -24,8 +24,6 @@ {% if is_expert %} - {% include 'messages.html' %} -
{% csrf_token %} diff --git a/modules/expert_mode/expert_mode.py b/modules/expert_mode/expert_mode.py index 389aa8fe0..e4401d7bd 100644 --- a/modules/expert_mode/expert_mode.py +++ b/modules/expert_mode/expert_mode.py @@ -1,4 +1,5 @@ from django import forms +from django.contrib import messages from django.template.response import TemplateResponse from gettext import gettext as _ @@ -24,13 +25,12 @@ def index(request): status = get_status(request) form = None - messages = [] if request.method == 'POST': form = ExpertsForm(request.POST, prefix='experts') # pylint: disable-msg=E1101 if form.is_valid(): - _apply_changes(request, form.cleaned_data, messages) + _apply_changes(request, form.cleaned_data) status = get_status(request) form = ExpertsForm(initial=status, prefix='experts') else: @@ -38,8 +38,7 @@ def index(request): return TemplateResponse(request, 'expert_mode.html', {'title': _('Expert Mode'), - 'form': form, - 'messages_': messages}) + 'form': form}) def get_status(request): @@ -47,20 +46,20 @@ def get_status(request): return {'expert_mode': cfg.users.expert(request=request)} -def _apply_changes(request, new_status, messages): +def _apply_changes(request, new_status): """Apply expert mode configuration""" - message = ('info', _('Settings unchanged')) + message = (messages.info, _('Settings unchanged')) user = cfg.users.current(request=request) if new_status['expert_mode']: if not 'expert' in user['groups']: user['groups'].append('expert') - message = ('success', _('Expert mode enabled')) + message = (messages.success, _('Expert mode enabled')) else: if 'expert' in user['groups']: user['groups'].remove('expert') - message = ('success', _('Expert mode disabled')) + message = (messages.success, _('Expert mode disabled')) cfg.users.set(user['username'], user) - messages.append(message) + message[0](request, message[1]) diff --git a/modules/expert_mode/templates/expert_mode.html b/modules/expert_mode/templates/expert_mode.html index c6df98b36..0db5565ab 100644 --- a/modules/expert_mode/templates/expert_mode.html +++ b/modules/expert_mode/templates/expert_mode.html @@ -22,8 +22,6 @@ {% block main_block %} - {% include 'messages.html' %} -

The {{ cfg.box_name }} can be administered in two modes, 'basic' and 'expert'. Basic mode hides a lot of features and configuration options that most users will never need to think about. Expert mode diff --git a/modules/first_boot/first_boot.py b/modules/first_boot/first_boot.py index d38f042a5..c9ecf5ff9 100644 --- a/modules/first_boot/first_boot.py +++ b/modules/first_boot/first_boot.py @@ -19,6 +19,7 @@ The Plinth first-connection process has several stages: """ from django import forms +from django.contrib import messages from django.core import validators from django.http.response import HttpResponseRedirect from django.template.response import TemplateResponse @@ -101,13 +102,12 @@ def state0(request): status = get_state0() form = None - messages = [] if request.method == 'POST': form = State0Form(request.POST, prefix='firstboot') # pylint: disable-msg=E1101 if form.is_valid(): - success = _apply_state0(status, form.cleaned_data, messages) + success = _apply_state0(request, status, form.cleaned_data) if success: # Everything is good, permanently mark and move to page 2 @@ -119,8 +119,7 @@ def state0(request): return TemplateResponse(request, 'firstboot_state0.html', {'title': _('First Boot!'), - 'form': form, - 'messages_': messages}) + 'form': form}) def get_state0(): @@ -131,7 +130,7 @@ def get_state0(): 'box_key': database.get('box_key', None)} -def _apply_state0(old_state, new_state, messages): +def _apply_state0(request, old_state, new_state): """Apply changes in state 0 form""" success = True with sqlite_db(cfg.store_file, table="thisbox", autocommit=True) as \ @@ -149,11 +148,11 @@ def _apply_state0(old_state, new_state, messages): error = add_user(new_state['username'], new_state['password'], 'First user, please change', '', True) if error: - messages.append( - ('error', _('User account creation failed: %s') % error)) + messages.error( + request, _('User account creation failed: %s') % error) success = False else: - messages.append(('success', _('User account created'))) + messages.success(request, _('User account created')) return success diff --git a/modules/first_boot/templates/firstboot_state0.html b/modules/first_boot/templates/firstboot_state0.html index 656fe406f..02d47cde6 100644 --- a/modules/first_boot/templates/firstboot_state0.html +++ b/modules/first_boot/templates/firstboot_state0.html @@ -24,8 +24,6 @@

Welcome to Your FreedomBox!

- {% include 'messages.html' %} -

Welcome. It looks like this FreedomBox isn't set up yet. We'll need to ask you a just few questions to get started.

diff --git a/modules/owncloud/owncloud.py b/modules/owncloud/owncloud.py index f2b1afff7..153c13e2a 100644 --- a/modules/owncloud/owncloud.py +++ b/modules/owncloud/owncloud.py @@ -1,4 +1,5 @@ from django import forms +from django.contrib import messages from django.template.response import TemplateResponse from gettext import gettext as _ @@ -34,13 +35,12 @@ def index(request): status = get_status() form = None - messages = [] if request.method == 'POST': form = OwnCloudForm(request.POST, prefix='owncloud') # pylint: disable-msg=E1101 if form.is_valid(): - _apply_changes(status, form.cleaned_data, messages) + _apply_changes(request, status, form.cleaned_data) status = get_status() form = OwnCloudForm(initial=status, prefix='owncloud') else: @@ -48,8 +48,7 @@ def index(request): return TemplateResponse(request, 'owncloud.html', {'title': _('ownCloud'), - 'form': form, - 'messages_': messages}) + 'form': form}) def get_status(): @@ -61,17 +60,17 @@ def get_status(): return {'enabled': 'enable' in output.split()} -def _apply_changes(old_status, new_status, messages): +def _apply_changes(request, old_status, new_status): """Apply the changes""" if old_status['enabled'] == new_status['enabled']: - messages.append(('info', _('Setting unchanged'))) + messages.info(request, _('Setting unchanged')) return if new_status['enabled']: - messages.append(('success', _('ownCloud enabled'))) + messages.success(request, _('ownCloud enabled')) option = 'enable' else: - messages.append(('success', _('ownCloud disabled'))) + messages.success(request, _('ownCloud disabled')) option = 'noenable' actions.superuser_run('owncloud-setup', [option], async=True) diff --git a/modules/owncloud/templates/owncloud.html b/modules/owncloud/templates/owncloud.html index 1e7efebd0..303846327 100644 --- a/modules/owncloud/templates/owncloud.html +++ b/modules/owncloud/templates/owncloud.html @@ -22,8 +22,6 @@ {% block main_block %} - {% include 'messages.html' %} - {% csrf_token %} diff --git a/modules/packages/packages.py b/modules/packages/packages.py index 8db985c85..8776da9da 100644 --- a/modules/packages/packages.py +++ b/modules/packages/packages.py @@ -1,4 +1,5 @@ from django import forms +from django.contrib import messages from django.template.response import TemplateResponse from gettext import gettext as _ @@ -51,13 +52,12 @@ def index(request): status = get_status() form = None - messages = [] if request.method == 'POST': form = PackagesForm(request.POST, prefix='packages') # pylint: disable-msg=E1101 if form.is_valid(): - _apply_changes(status, form.cleaned_data, messages) + _apply_changes(request, status, form.cleaned_data) status = get_status() form = PackagesForm(initial=status, prefix='packages') else: @@ -65,8 +65,7 @@ def index(request): return TemplateResponse(request, 'packages.html', {'title': _('Add/Remove Plugins'), - 'form': form, - 'messages_': messages}) + 'form': form}) def get_status(): @@ -78,7 +77,7 @@ def get_status(): for module in modules_available} -def _apply_changes(old_status, new_status, messages): +def _apply_changes(request, old_status, new_status): """Apply form changes""" for field, enabled in new_status.items(): if not field.endswith('_enabled'): @@ -96,13 +95,13 @@ def _apply_changes(old_status, new_status, messages): # TODO: need to get plinth to load the module we just # enabled if error: - messages.append( - ('error', _('Error enabling module - {module}').format( - module=module))) + messages.error( + request, _('Error enabling module - {module}').format( + module=module)) else: - messages.append( - ('success', _('Module enabled - {module}').format( - module=module))) + messages.success( + request, _('Module enabled - {module}').format( + module=module)) else: output, error = actions.superuser_run( 'module-manager', ['disable', cfg.python_root, module]) @@ -111,11 +110,10 @@ def _apply_changes(old_status, new_status, messages): # TODO: need a smoother way for plinth to unload the # module if error: - messages.append( - ('error', - _('Error disabling module - {module}').format( - module=module))) + messages.error( + request, _('Error disabling module - {module}').format( + module=module)) else: - messages.append( - ('success', _('Module disabled - {module}').format( - module=module))) + messages.success( + request, _('Module disabled - {module}').format( + module=module)) diff --git a/modules/packages/templates/packages.html b/modules/packages/templates/packages.html index d8d7d6164..d6eb85e22 100644 --- a/modules/packages/templates/packages.html +++ b/modules/packages/templates/packages.html @@ -22,8 +22,6 @@ {% block main_block %} - {% include 'messages.html' %} -

aptitude purge modules

aptitude install modules

diff --git a/modules/pagekite/pagekite.py b/modules/pagekite/pagekite.py index 8ad48587b..0f1329973 100644 --- a/modules/pagekite/pagekite.py +++ b/modules/pagekite/pagekite.py @@ -20,6 +20,7 @@ Plinth module for configuring PageKite service """ from django import forms +from django.contrib import messages from django.core import validators from django.template import RequestContext from django.template.loader import render_to_string @@ -104,13 +105,12 @@ def configure(request): status = get_status() form = None - messages = [] if request.method == 'POST': form = ConfigureForm(request.POST, prefix='pagekite') # pylint: disable-msg=E1101 if form.is_valid(): - _apply_changes(status, form.cleaned_data, messages) + _apply_changes(request, status, form.cleaned_data) status = get_status() form = ConfigureForm(initial=status, prefix='pagekite') else: @@ -118,8 +118,7 @@ def configure(request): return TemplateResponse(request, 'pagekite_configure.html', {'title': _('Configure PageKite'), - 'form': form, - 'messages_': messages}) + 'form': form}) def get_status(): @@ -154,7 +153,7 @@ def get_status(): return status -def _apply_changes(old_status, new_status, messages): +def _apply_changes(request, old_status, new_status): """Apply the changes to PageKite configuration""" cfg.log.info('New status is - %s' % new_status) @@ -164,29 +163,28 @@ def _apply_changes(old_status, new_status, messages): if old_status['enabled'] != new_status['enabled']: if new_status['enabled']: _run(['set-status', 'enable']) - messages.append(('success', _('PageKite enabled'))) + messages.success(request, _('PageKite enabled')) else: _run(['set-status', 'disable']) - messages.append(('success', _('PageKite disabled'))) + messages.success(request, _('PageKite disabled')) if old_status['kite_name'] != new_status['kite_name'] or \ old_status['kite_secret'] != new_status['kite_secret']: _run(['set-kite', '--kite-name', new_status['kite_name'], '--kite-secret', new_status['kite_secret']]) - messages.append(('success', _('Kite details set'))) + messages.success(request, _('Kite details set')) for service in ['http', 'ssh']: if old_status[service + '_enabled'] != \ new_status[service + '_enabled']: if new_status[service + '_enabled']: _run(['set-service-status', service, 'enable']) - messages.append(('success', _('Service enabled: {service}') - .format(service=service))) + messages.success(request, _('Service enabled: {service}') + .format(service=service)) else: _run(['set-service-status', service, 'disable']) - messages.append(('success', - _('Service disabled: {service}') - .format(service=service))) + messages.success(request, _('Service disabled: {service}') + .format(service=service)) if old_status != new_status: _run(['start']) diff --git a/modules/pagekite/templates/pagekite_configure.html b/modules/pagekite/templates/pagekite_configure.html index dd31c47de..9bcc25fe1 100644 --- a/modules/pagekite/templates/pagekite_configure.html +++ b/modules/pagekite/templates/pagekite_configure.html @@ -31,8 +31,6 @@ {% else %} - {% include 'messages.html' %} - {% csrf_token %} diff --git a/modules/users/templates/users_add.html b/modules/users/templates/users_add.html index 4afbc68c9..3df856188 100644 --- a/modules/users/templates/users_add.html +++ b/modules/users/templates/users_add.html @@ -22,8 +22,6 @@ {% block main_block %} - {% include 'messages.html' %} - {% csrf_token %} diff --git a/modules/users/templates/users_edit.html b/modules/users/templates/users_edit.html index da1ef4136..82905b6b9 100644 --- a/modules/users/templates/users_edit.html +++ b/modules/users/templates/users_edit.html @@ -22,8 +22,6 @@ {% block main_block %} - {% include 'messages.html' %} - {% csrf_token %} diff --git a/modules/users/users.py b/modules/users/users.py index 0103c7c53..8c4f5081b 100644 --- a/modules/users/users.py +++ b/modules/users/users.py @@ -1,4 +1,5 @@ from django import forms +from django.contrib import messages from django.core import validators from django.template import RequestContext from django.template.loader import render_to_string @@ -54,36 +55,32 @@ and alphabet'), def add(request): """Serve the form""" form = None - messages = [] if request.method == 'POST': form = UserAddForm(request.POST, prefix='user') # pylint: disable-msg=E1101 if form.is_valid(): - _add_user(form.cleaned_data, messages) + _add_user(request, form.cleaned_data) form = UserAddForm(prefix='user') else: form = UserAddForm(prefix='user') return TemplateResponse(request, 'users_add.html', {'title': _('Add User'), - 'form': form, - 'messages_': messages}) + 'form': form}) -def _add_user(data, messages): +def _add_user(request, data): """Add a user""" if cfg.users.exists(data['username']): - messages.append( - ('error', _('User "{username}" already exists').format( - username=data['username']))) + messages.error(request, _('User "{username}" already exists').format( + username=data['username'])) return add_user(data['username'], data['password'], data['full_name'], data['email'], False) - messages.append( - ('success', _('User "{username}" added').format( - username=data['username']))) + messages.success(request, _('User "{username}" added').format( + username=data['username'])) class UserEditForm(forms.Form): # pylint: disable-msg=W0232 @@ -106,24 +103,22 @@ class UserEditForm(forms.Form): # pylint: disable-msg=W0232 def edit(request): """Serve the edit form""" form = None - messages = [] if request.method == 'POST': form = UserEditForm(request.POST, prefix='user') # pylint: disable-msg=E1101 if form.is_valid(): - _apply_edit_changes(request, form.cleaned_data, messages) + _apply_edit_changes(request, form.cleaned_data) form = UserEditForm(prefix='user') else: form = UserEditForm(prefix='user') return TemplateResponse(request, 'users_edit.html', {'title': _('Edit or Delete User'), - 'form': form, - 'messages_': messages}) + 'form': form}) -def _apply_edit_changes(request, data, messages): +def _apply_edit_changes(request, data): """Apply form changes""" for field, value in data.items(): if not value: @@ -139,19 +134,17 @@ def _apply_edit_changes(request, data, messages): (requesting_user, username)) if username == cfg.users.current(request=request, name=True): - messages.append( - ('error', - _('Can not delete current account - "%s"') % username)) + messages.error( + request, _('Can not delete current account - "%s"') % username) continue if not cfg.users.exists(username): - messages.append(('error', - _('User "%s" does not exist') % username)) + messages.error(request, _('User "%s" does not exist') % username) continue try: cfg.users.remove(username) - messages.append(('success', _('User "%s" deleted') % username)) + messages.success(request, _('User "%s" deleted') % username) except IOError as exception: - messages.append(('error', _('Error deleting "%s" - %s') % - (username, exception))) + messages.error(request, _('Error deleting "%s" - %s') % + (username, exception)) diff --git a/modules/xmpp/templates/xmpp_configure.html b/modules/xmpp/templates/xmpp_configure.html index 718d7d353..509cdabeb 100644 --- a/modules/xmpp/templates/xmpp_configure.html +++ b/modules/xmpp/templates/xmpp_configure.html @@ -22,8 +22,6 @@ {% block main_block %} - {% include 'messages.html' %} - {% csrf_token %} diff --git a/modules/xmpp/templates/xmpp_register.html b/modules/xmpp/templates/xmpp_register.html index bfb7c3de4..ed2c2ed6a 100644 --- a/modules/xmpp/templates/xmpp_register.html +++ b/modules/xmpp/templates/xmpp_register.html @@ -22,8 +22,6 @@ {% block main_block %} - {% include 'messages.html' %} - {% csrf_token %} diff --git a/modules/xmpp/xmpp.py b/modules/xmpp/xmpp.py index 2974607bb..47ee9c517 100644 --- a/modules/xmpp/xmpp.py +++ b/modules/xmpp/xmpp.py @@ -1,4 +1,5 @@ from django import forms +from django.contrib import messages from django.template import RequestContext from django.template.loader import render_to_string from django.template.response import TemplateResponse @@ -62,13 +63,12 @@ def configure(request): status = get_status() form = None - messages = [] if request.method == 'POST': form = ConfigureForm(request.POST, prefix='xmpp') # pylint: disable-msg=E1101 if form.is_valid(): - _apply_changes(status, form.cleaned_data, messages) + _apply_changes(request, status, form.cleaned_data) status = get_status() form = ConfigureForm(initial=status, prefix='xmpp') else: @@ -80,7 +80,6 @@ def configure(request): return TemplateResponse(request, 'xmpp_configure.html', {'title': _('Configure XMPP Server'), 'form': form, - 'messages_': messages, 'sidebar_right': sidebar_right}) @@ -93,19 +92,19 @@ def get_status(): return {'inband_enabled': 'inband_enable' in output.split()} -def _apply_changes(old_status, new_status, messages): +def _apply_changes(request, old_status, new_status): """Apply the form changes""" cfg.log.info('Status - %s, %s' % (old_status, new_status)) if old_status['inband_enabled'] == new_status['inband_enabled']: - messages.append(('info', _('Setting unchanged'))) + messages.info(request, _('Setting unchanged')) return if new_status['inband_enabled']: - messages.append(('success', _('Inband registration enabled'))) + messages.success(request, _('Inband registration enabled')) option = 'inband_enable' else: - messages.append(('success', _('Inband registration disabled'))) + messages.success(request, _('Inband registration disabled')) option = 'noinband_enable' cfg.log.info('Option - %s' % option) @@ -128,13 +127,12 @@ class RegisterForm(forms.Form): # pylint: disable-msg=W0232 def register(request): """Serve the registration form""" form = None - messages = [] if request.method == 'POST': form = RegisterForm(request.POST, prefix='xmpp') # pylint: disable-msg=E1101 if form.is_valid(): - _register_user(form.cleaned_data, messages) + _register_user(request, form.cleaned_data) form = RegisterForm(prefix='xmpp') else: form = RegisterForm(prefix='xmpp') @@ -145,11 +143,10 @@ def register(request): return TemplateResponse(request, 'xmpp_register.html', {'title': _('Register XMPP Account'), 'form': form, - 'messages_': messages, 'sidebar_right': sidebar_right}) -def _register_user(data, messages): +def _register_user(request, data): """Register a new XMPP user""" output, error = actions.superuser_run( 'xmpp-register', [data['username'], data['password']]) @@ -157,10 +154,9 @@ def _register_user(data, messages): raise Exception('Error registering user - %s' % error) if 'successfully registered' in output: - messages.append(('success', - _('Registered account for %s' % - data['username']))) + messages.success(request, _('Registered account for %s') % + data['username']) else: - messages.append(('error', - _('Failed to register account for %s: %s') % - (data['username'], output))) + messages.error(request, + _('Failed to register account for %s: %s') % + (data['username'], output)) diff --git a/plinth.py b/plinth.py index 2f1521396..19d0398c5 100755 --- a/plinth.py +++ b/plinth.py @@ -135,7 +135,8 @@ def configure_django(): DEBUG=cfg.debug, ALLOWED_HOSTS=['127.0.0.1', 'localhost'], TEMPLATE_DIRS=template_directories, - INSTALLED_APPS=['bootstrapform'], + INSTALLED_APPS=['bootstrapform', + 'django.contrib.messages'], ROOT_URLCONF='urls', SESSION_ENGINE='django.contrib.sessions.backends.file', SESSION_FILE_PATH=sessions_directory, diff --git a/templates/base.html b/templates/base.html index e26f099b3..91b0d3ce2 100644 --- a/templates/base.html +++ b/templates/base.html @@ -99,6 +99,9 @@ {{ title }} {% endblock %} + + {% include 'messages.html' %} + {% block main_block %} {{ main|safe }} {% endblock %} diff --git a/templates/messages.html b/templates/messages.html index 4aac25d88..34559a873 100644 --- a/templates/messages.html +++ b/templates/messages.html @@ -17,8 +17,8 @@ # {% endcomment %} -{% for severity, message in messages_ %} -
+{% for message in messages %} +
× {{ message }}