upgrades: Show better error messages

- Reduce the number of specialized messages to ease localization and clear way
for generalized configuration change handler.

Tests:

- Update the one or two configuration setting at the same time and notice that a
single message is shown.

- When no setting is changed and form is submitted, 'settings unchanged' message
is shown.

- Raise error when enabling/disable auto updates and notice a proper HTML error
shown. When other setting is also updated, then one error and one success
message is shown.

Signed-off-by: Sunil Mohan Adapa <sunil@medhas.org>
Reviewed-by: James Valleroy <jvalleroy@mailbox.org>
This commit is contained in:
Sunil Mohan Adapa 2024-03-18 14:42:14 -07:00 committed by James Valleroy
parent 64d754701f
commit f240c4203c
No known key found for this signature in database
GPG Key ID: 77C0C75E7B650808

View File

@ -15,7 +15,7 @@ from django.views.generic.edit import FormView
from plinth import __version__
from plinth.modules import first_boot, upgrades
from plinth.privileged import packages as packages_privileged
from plinth.views import AppView
from plinth.views import AppView, messages_error
from . import privileged
from .forms import BackportsFirstbootForm, ConfigureForm, UpdateFirstbootForm
@ -56,6 +56,8 @@ class UpgradesConfigurationView(AppView):
old_status = form.initial
new_status = form.cleaned_data
is_changed = False
if old_status['auto_upgrades_enabled'] \
!= new_status['auto_upgrades_enabled']:
@ -64,29 +66,21 @@ class UpgradesConfigurationView(AppView):
privileged.enable_auto()
else:
privileged.disable_auto()
except Exception as exception:
error = exception.args[2]
messages.error(
self.request,
_('Error when configuring unattended-upgrades: {error}').
format(error=error))
if new_status['auto_upgrades_enabled']:
messages.success(self.request, _('Automatic upgrades enabled'))
else:
messages.success(self.request,
_('Automatic upgrades disabled'))
is_changed = True
except Exception as exception:
messages_error(self.request,
_('Error when configuring unattended-upgrades'),
exception)
if old_status['dist_upgrade_enabled'] \
!= new_status['dist_upgrade_enabled']:
upgrades.set_dist_upgrade_enabled(
new_status['dist_upgrade_enabled'])
if new_status['dist_upgrade_enabled']:
messages.success(self.request,
_('Distribution upgrade enabled'))
else:
messages.success(self.request,
_('Distribution upgrade disabled'))
is_changed = True
if is_changed:
messages.success(self.request, _('Configuration updated.'))
return super().form_valid(form)