diff --git a/plinth/modules/upgrades/__init__.py b/plinth/modules/upgrades/__init__.py index 1f58ad821..e8e84ac38 100644 --- a/plinth/modules/upgrades/__init__.py +++ b/plinth/modules/upgrades/__init__.py @@ -50,3 +50,20 @@ def setup(helper, old_version=None): """Install and configure the module.""" helper.install(['unattended-upgrades']) helper.call('post', actions.superuser_run, 'upgrades', ['enable-auto']) + + +def get_status(): + """Return the current status.""" + return {'auto_upgrades_enabled': 'is_enabled'} + + +def is_enabled(): + """Return whether the module is enabled.""" + output = actions.run('upgrades', ['check-auto']) + return 'True' in output.split() + + +def enable(should_enable): + """Enable/disable the module.""" + option = 'enable-auto' if should_enable else 'disable-auto' + actions.superuser_run('upgrades', [option]) diff --git a/plinth/modules/upgrades/urls.py b/plinth/modules/upgrades/urls.py index 9457c17dc..eba0e70b8 100644 --- a/plinth/modules/upgrades/urls.py +++ b/plinth/modules/upgrades/urls.py @@ -25,6 +25,7 @@ from . import views urlpatterns = [ - url(r'^sys/upgrades/$', views.index, name='index'), + url(r'^sys/upgrades/$', + views.ConfigurationView.as_view(module_name='upgrades'), name='index'), url(r'^sys/upgrades/upgrade/$', views.upgrade, name='upgrade'), ] diff --git a/plinth/modules/upgrades/views.py b/plinth/modules/upgrades/views.py index 81b0a4ba3..37b63bc82 100644 --- a/plinth/modules/upgrades/views.py +++ b/plinth/modules/upgrades/views.py @@ -27,6 +27,7 @@ import subprocess from .forms import ConfigureForm from plinth import actions +from plinth import views from plinth.errors import ActionError from plinth.modules import upgrades @@ -39,26 +40,43 @@ LOG_FILE = '/var/log/unattended-upgrades/unattended-upgrades.log' LOCK_FILE = '/var/log/dpkg/lock' -def index(request): - """Serve the configuration form.""" - status = get_status() +class ConfigurationView(views.ConfigurationView): + """Serve configuration page.""" + form_class = ConfigureForm - form = None + def get_context_data(self, **kwargs): + """Return the context data for rendering the template view.""" + if 'subsubmenu' not in kwargs: + kwargs['subsubmenu'] = subsubmenu - if request.method == 'POST': - form = ConfigureForm(request.POST, prefix='upgrades') - if form.is_valid(): - _apply_changes(request, status, form.cleaned_data) - status = get_status() - form = ConfigureForm(initial=status, prefix='upgrades') - else: - form = ConfigureForm(initial=status, prefix='upgrades') + return super().get_context_data(**kwargs) - return TemplateResponse(request, 'upgrades_configure.html', - {'title': upgrades.title, - 'description': upgrades.description, - 'form': form, - 'subsubmenu': subsubmenu}) + def get_template_names(self): + """Return the list of template names for the view.""" + return ['upgrades_configure.html'] + + def apply_changes(self, old_status, new_status): + """Apply the form changes.""" + if old_status['auto_upgrades_enabled'] \ + == new_status['auto_upgrades_enabled']: + return False + + try: + upgrades.enable(new_status['auto_upgrades_enabled']) + except ActionError as exception: + error = exception.args[2] + messages.error( + self.request, + _('Error when configuring unattended-upgrades: {error}') + .format(error=error)) + return True + + if new_status['auto_upgrades_enabled']: + messages.success(self.request, _('Automatic upgrades enabled')) + else: + messages.success(self.request, _('Automatic upgrades disabled')) + + return True def is_package_manager_busy(): @@ -97,36 +115,3 @@ def upgrade(request): 'subsubmenu': subsubmenu, 'is_busy': is_busy, 'log': get_log()}) - - -def get_status(): - """Return the current status.""" - output = actions.run('upgrades', ['check-auto']) - return {'auto_upgrades_enabled': 'True' in output.split()} - - -def _apply_changes(request, old_status, new_status): - """Apply the form changes.""" - if old_status['auto_upgrades_enabled'] \ - == new_status['auto_upgrades_enabled']: - messages.info(request, _('Setting unchanged')) - return - - if new_status['auto_upgrades_enabled']: - option = 'enable-auto' - else: - option = 'disable-auto' - - try: - actions.superuser_run('upgrades', [option]) - except ActionError as exception: - error = exception.args[2] - messages.error( - request, _('Error when configuring unattended-upgrades: {error}') - .format(error=error)) - return - - if option == 'enable-auto': - messages.success(request, _('Automatic upgrades enabled')) - else: - messages.success(request, _('Automatic upgrades disabled'))