From 9ab0e13ab9f74ccb7063e28d20fa7debb893570d Mon Sep 17 00:00:00 2001 From: Joseph Nuthalapati Date: Fri, 9 Mar 2018 13:42:54 +0530 Subject: [PATCH] snapshots: Refactoring and indentation changes - Made updating snapshot configuration more efficient by running a single snapper command instead of one per configuration. - Set default configuration for NUMBER_LIMIT only once at the time of installation. Snapshot version has been incremented to support this. Signed-off-by: Joseph Nuthalapati --- actions/snapshot | 3 +- plinth/modules/snapshot/__init__.py | 27 ++++++--- plinth/modules/snapshot/forms.py | 25 ++++---- .../modules/snapshot/templates/snapshot.html | 12 ++-- .../snapshot/templates/snapshot_manage.html | 18 +++--- plinth/modules/snapshot/views.py | 58 ++++++++++--------- 6 files changed, 80 insertions(+), 63 deletions(-) diff --git a/actions/snapshot b/actions/snapshot index 051100ec0..39e6aaf2c 100755 --- a/actions/snapshot +++ b/actions/snapshot @@ -79,7 +79,8 @@ def _set_default_config(): 'snapper', 'set-config', 'TIMELINE_CREATE=yes', 'TIMELINE_LIMIT_HOURLY=10', 'TIMELINE_LIMIT_MONTHLY=2', 'TIMELINE_LIMIT_WEEKLY=2', 'TIMELINE_LIMIT_YEARLY=0', - 'TIMELINE_LIMIT_DAILY=3', 'NUMBER_MIN_AGE=1296000' + 'TIMELINE_LIMIT_DAILY=3', 'NUMBER_MIN_AGE=1296000', 'NUMBER_LIMIT=0', + 'NUMBER_LIMIT_IMPORTANT=4-10' ] subprocess.run(command, check=True) diff --git a/plinth/modules/snapshot/__init__.py b/plinth/modules/snapshot/__init__.py index b37e2f782..66fee3ace 100644 --- a/plinth/modules/snapshot/__init__.py +++ b/plinth/modules/snapshot/__init__.py @@ -25,7 +25,7 @@ from django.utils.translation import ugettext_lazy as _ from plinth import actions from plinth.menu import main_menu -version = 2 +version = 3 managed_packages = ['snapper'] @@ -61,12 +61,23 @@ def setup(helper, old_version=None): def get_configuration(): output = actions.superuser_run('snapshot', ['get-config']) output = json.loads(output) + + def get_boolean_choice(status): + return ('yes', 'Enabled') if status else ('no', 'Disabled') + return { - 'enable_timeline_snapshots': output['TIMELINE_CREATE'] == 'yes', - 'hourly_limit': output['TIMELINE_LIMIT_HOURLY'], - 'daily_limit': output['TIMELINE_LIMIT_DAILY'], - 'weekly_limit': output['TIMELINE_LIMIT_WEEKLY'], - 'yearly_limit': output['TIMELINE_LIMIT_YEARLY'], - 'monthly_limit': output['TIMELINE_LIMIT_MONTHLY'], - 'number_min_age': round(int(output['NUMBER_MIN_AGE']) / 86400), + 'enable_timeline_snapshots': + get_boolean_choice(output['TIMELINE_CREATE'] == 'yes'), + 'hourly_limit': + output['TIMELINE_LIMIT_HOURLY'], + 'daily_limit': + output['TIMELINE_LIMIT_DAILY'], + 'weekly_limit': + output['TIMELINE_LIMIT_WEEKLY'], + 'yearly_limit': + output['TIMELINE_LIMIT_YEARLY'], + 'monthly_limit': + output['TIMELINE_LIMIT_MONTHLY'], + 'number_min_age': + round(int(output['NUMBER_MIN_AGE']) / 86400), } diff --git a/plinth/modules/snapshot/forms.py b/plinth/modules/snapshot/forms.py index 23ec8fc0e..48be5f3cc 100644 --- a/plinth/modules/snapshot/forms.py +++ b/plinth/modules/snapshot/forms.py @@ -23,26 +23,27 @@ from django.utils.translation import ugettext_lazy as _ class SnapshotForm(forms.Form): - enable_timeline_snapshots = forms.BooleanField( - label=_('Enable Timeline Snapshots'), required=False, help_text=_( - 'Uncheck this to disable timeline snapshots ' - '(hourly, daily, monthly and yearly).')) + enable_timeline_snapshots = forms.ChoiceField( + label=_('Timeline Snapshots'), + help_text=_('Enable or disable timeline snapshots ' + '(hourly, daily, monthly and yearly).'), + choices=[('yes', 'Enabled'), ('no', 'Disabled')]) hourly_limit = forms.IntegerField( - label=_('Hourly Snapshots Limit'), min_value=0, help_text=_( - 'Keep a maximum of this many hourly snapshots.')) + label=_('Hourly Snapshots Limit'), min_value=0, + help_text=_('Keep a maximum of this many hourly snapshots.')) daily_limit = forms.IntegerField( - label=_('Daily Snapshots Limit'), min_value=0, help_text=_( - 'Keep a maximum of this many daily snapshots.')) + label=_('Daily Snapshots Limit'), min_value=0, + help_text=_('Keep a maximum of this many daily snapshots.')) weekly_limit = forms.IntegerField( - label=_('Weekly Snapshots Limit'), min_value=0, help_text=_( - 'Keep a maximum of this many weekly snapshots.')) + label=_('Weekly Snapshots Limit'), min_value=0, + help_text=_('Keep a maximum of this many weekly snapshots.')) monthly_limit = forms.IntegerField( - label=_('Monthly Snapshots Limit'), min_value=0, help_text=_( - 'Keep a maximum of this many monthly snapshots.')) + label=_('Monthly Snapshots Limit'), min_value=0, + help_text=_('Keep a maximum of this many monthly snapshots.')) yearly_limit = forms.IntegerField( label=_('Yearly Snapshots Limit'), min_value=0, help_text=_( diff --git a/plinth/modules/snapshot/templates/snapshot.html b/plinth/modules/snapshot/templates/snapshot.html index ea43c8959..297f1ee17 100644 --- a/plinth/modules/snapshot/templates/snapshot.html +++ b/plinth/modules/snapshot/templates/snapshot.html @@ -22,13 +22,13 @@ {% load i18n %} {% block configuration %} -

-

- {% csrf_token %} -

{% trans "Configuration" %}

+ + + {% csrf_token %} +

{% trans "Configuration" %}

{{ form|bootstrap_horizontal:'col-sm-4' }} - +
{% endblock %} diff --git a/plinth/modules/snapshot/templates/snapshot_manage.html b/plinth/modules/snapshot/templates/snapshot_manage.html index 8df08307b..573fdfb42 100644 --- a/plinth/modules/snapshot/templates/snapshot_manage.html +++ b/plinth/modules/snapshot/templates/snapshot_manage.html @@ -26,19 +26,19 @@
{% csrf_token %} -
- -
+
+ +
diff --git a/plinth/modules/snapshot/views.py b/plinth/modules/snapshot/views.py index 1ae402799..1825ea1cc 100644 --- a/plinth/modules/snapshot/views.py +++ b/plinth/modules/snapshot/views.py @@ -19,25 +19,29 @@ Views for snapshot module. """ import json +import subprocess from django.contrib import messages from django.shortcuts import redirect from django.template.response import TemplateResponse from django.urls import reverse, reverse_lazy +from django.utils.translation import ugettext as _ +from django.utils.translation import ugettext_lazy from plinth import actions from plinth.errors import ActionError from plinth.modules import snapshot as snapshot_module -from plinth.utils import yes_or_no -from django.utils.translation import ugettext as _, ugettext_lazy from . import get_configuration from .forms import SnapshotForm -subsubmenu = [{'url': reverse_lazy('snapshot:index'), - 'text': ugettext_lazy('Configure')}, - {'url': reverse_lazy('snapshot:manage'), - 'text': ugettext_lazy('Manage Snapshots')}] +subsubmenu = [{ + 'url': reverse_lazy('snapshot:index'), + 'text': ugettext_lazy('Configure') +}, { + 'url': reverse_lazy('snapshot:manage'), + 'text': ugettext_lazy('Manage Snapshots') +}] def index(request): @@ -84,31 +88,31 @@ def manage(request): def update_configuration(request, old_status, new_status): """Update configuration of snapshots.""" - def update_key_configuration(key, stamp, threshold): + def make_config(args): + key, stamp = args[0], args[1] if old_status[key] != new_status[key]: - actions.superuser_run( - 'snapshot', ['configure', stamp.format(threshold)]) + return stamp.format(new_status[key]) + else: + return None + + new_status['number_min_age'] = int(new_status['number_min_age']) * 86400 + + config = filter(None, + map(make_config, [ + ('enable_timeline_snapshots', 'TIMELINE_CREATE={}'), + ('hourly_limit', 'TIMELINE_LIMIT_HOURLY={}'), + ('daily_limit', 'TIMELINE_LIMIT_DAILY={}'), + ('weekly_limit', 'TIMELINE_LIMIT_WEEKLY={}'), + ('monthly_limit', 'TIMELINE_LIMIT_MONTHLY={}'), + ('yearly_limit', 'TIMELINE_LIMIT_YEARLY={}'), + ('number_min_age', 'NUMBER_MIN_AGE={}'), + ])) + + command = ['snapper', 'set-config'] + list(config) try: - update_key_configuration( - 'enable_timeline_snapshots', 'TIMELINE_CREATE={}', - yes_or_no(new_status['enable_timeline_snapshots'])) - update_key_configuration('hourly_limit', 'TIMELINE_LIMIT_HOURLY={}', - new_status['hourly_limit']) - update_key_configuration('daily_limit', 'TIMELINE_LIMIT_DAILY={}', - new_status['daily_limit']) - update_key_configuration('weekly_limit', 'TIMELINE_LIMIT_WEEKLY={}', - new_status['weekly_limit']) - update_key_configuration('monthly_limit', 'TIMELINE_LIMIT_MONTHLY={}', - new_status['monthly_limit']) - update_key_configuration('yearly_limit', 'TIMELINE_LIMIT_YEARLY={}', - new_status['yearly_limit']) - update_key_configuration('number_min_age', 'NUMBER_MIN_AGE={}', - int(new_status['number_min_age']) * 86400) + subprocess.run(command, check=True) - actions.superuser_run('snapshot', ['configure', 'NUMBER_LIMIT=0']) - actions.superuser_run('snapshot', - ['configure', 'NUMBER_LIMIT_IMPORTANT=4-10']) messages.success(request, _('Storage snapshots configuration updated')) except ActionError as exception: messages.error(request,