From d4e9e7a965dfb4405e94ca738a6061022b544024 Mon Sep 17 00:00:00 2001 From: Sunil Mohan Adapa Date: Thu, 14 Jan 2021 10:50:51 -0800 Subject: [PATCH] backups: Add a notification to suggest users to enable schedules Tests performed: - Ensure that backups module is setup from version zero by deleting entry in DB. Start FreedomBox. Observe the notification comes up. - Dismiss button dismiss the notification. - Go to button takes us to the backups module. - Incrementing the version number of the backups app does not show the notification again. - Starting with old code, the notification show up when FreedomBox is run. Incrementing the version number of the backups app after that does not show the notification again. - Enable a schedule and the notification should be dismissed. Signed-off-by: Sunil Mohan Adapa Reviewed-by: James Valleroy --- plinth/modules/backups/__init__.py | 45 +++++++++++++++++++++++++++++- plinth/modules/backups/views.py | 1 + 2 files changed, 45 insertions(+), 1 deletion(-) diff --git a/plinth/modules/backups/__init__.py b/plinth/modules/backups/__init__.py index b7a098f0c..e9143341e 100644 --- a/plinth/modules/backups/__init__.py +++ b/plinth/modules/backups/__init__.py @@ -12,6 +12,7 @@ import re import paramiko from django.utils.text import get_valid_filename from django.utils.translation import ugettext_lazy as _ +from django.utils.translation import ugettext_noop from plinth import actions from plinth import app as app_module @@ -21,7 +22,7 @@ from . import api logger = logging.getLogger(__name__) -version = 2 +version = 3 is_essential = True @@ -73,6 +74,10 @@ def setup(helper, old_version=None): ['setup', '--path', repository.RootBorgRepository.PATH]) helper.call('post', app.enable) + # First time setup or upgrading from older versions. + if old_version <= 2: + _show_schedule_setup_notification() + def _backup_handler(packet, encryption_passphrase=None): """Performs backup operation on packet.""" @@ -181,3 +186,41 @@ def split_path(path): """ return re.findall(r'^(.*)@([^/]*):(.*)$', path)[0] + + +def _show_schedule_setup_notification(): + """Show a notification hinting to setup a remote backup schedule.""" + from plinth.notification import Notification + message = ugettext_noop( + 'Enable an automatic backup schedule for data safety. Prefer an ' + 'encrypted remote backup location or an extra attached disk.') + data = { + 'app_name': 'translate:' + ugettext_noop('Backups'), + 'app_icon': 'fa-files-o' + } + title = ugettext_noop('Enable a Backup Schedule') + actions_ = [{ + 'type': 'link', + 'class': 'primary', + 'text': ugettext_noop('Go to {app_name}'), + 'url': 'backups:index' + }, { + 'type': 'dismiss' + }] + Notification.update_or_create(id='backups-remote-schedule', + app_id='backups', severity='info', + title=title, message=message, + actions=actions_, data=data, group='admin') + + +def on_schedule_save(repository): + """Dismiss notification. Called when repository's schedule is updated.""" + if not repository.schedule.enabled: + return + + from plinth.notification import Notification + try: + note = Notification.get('backups-remote-schedule') + note.dismiss() + except KeyError: + pass diff --git a/plinth/modules/backups/views.py b/plinth/modules/backups/views.py index 63e9a566f..5d27766ac 100644 --- a/plinth/modules/backups/views.py +++ b/plinth/modules/backups/views.py @@ -93,6 +93,7 @@ class ScheduleView(SuccessMessageMixin, FormView): schedule.unselected_apps = unselected_apps repository.save() + backups.on_schedule_save(repository) return super().form_valid(form)