mirror of
https://github.com/freedombox/FreedomBox.git
synced 2026-05-20 10:34:30 +00:00
upgrades: Use common view for configuration
This commit is contained in:
parent
a0e676c51c
commit
711892486e
@ -50,3 +50,20 @@ def setup(helper, old_version=None):
|
|||||||
"""Install and configure the module."""
|
"""Install and configure the module."""
|
||||||
helper.install(['unattended-upgrades'])
|
helper.install(['unattended-upgrades'])
|
||||||
helper.call('post', actions.superuser_run, 'upgrades', ['enable-auto'])
|
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])
|
||||||
|
|||||||
@ -25,6 +25,7 @@ from . import views
|
|||||||
|
|
||||||
|
|
||||||
urlpatterns = [
|
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'),
|
url(r'^sys/upgrades/upgrade/$', views.upgrade, name='upgrade'),
|
||||||
]
|
]
|
||||||
|
|||||||
@ -27,6 +27,7 @@ import subprocess
|
|||||||
|
|
||||||
from .forms import ConfigureForm
|
from .forms import ConfigureForm
|
||||||
from plinth import actions
|
from plinth import actions
|
||||||
|
from plinth import views
|
||||||
from plinth.errors import ActionError
|
from plinth.errors import ActionError
|
||||||
from plinth.modules import upgrades
|
from plinth.modules import upgrades
|
||||||
|
|
||||||
@ -39,26 +40,43 @@ LOG_FILE = '/var/log/unattended-upgrades/unattended-upgrades.log'
|
|||||||
LOCK_FILE = '/var/log/dpkg/lock'
|
LOCK_FILE = '/var/log/dpkg/lock'
|
||||||
|
|
||||||
|
|
||||||
def index(request):
|
class ConfigurationView(views.ConfigurationView):
|
||||||
"""Serve the configuration form."""
|
"""Serve configuration page."""
|
||||||
status = get_status()
|
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':
|
return super().get_context_data(**kwargs)
|
||||||
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 TemplateResponse(request, 'upgrades_configure.html',
|
def get_template_names(self):
|
||||||
{'title': upgrades.title,
|
"""Return the list of template names for the view."""
|
||||||
'description': upgrades.description,
|
return ['upgrades_configure.html']
|
||||||
'form': form,
|
|
||||||
'subsubmenu': subsubmenu})
|
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():
|
def is_package_manager_busy():
|
||||||
@ -97,36 +115,3 @@ def upgrade(request):
|
|||||||
'subsubmenu': subsubmenu,
|
'subsubmenu': subsubmenu,
|
||||||
'is_busy': is_busy,
|
'is_busy': is_busy,
|
||||||
'log': get_log()})
|
'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'))
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user