From 8f2520b327084f3127632e73d7798ac3b6e0d245 Mon Sep 17 00:00:00 2001 From: Sunil Mohan Adapa Date: Thu, 9 Feb 2023 08:42:41 -0800 Subject: [PATCH] backups: Allow selecting a single app from URL when creating backup Take app_id in a URL fragment and fill that as the default selected app in create backup form. This URL can be used in apps to create a backup link. Tests: - Visit /plinth/sys/backups/create/bepasty/. Only bepasty app will be selected. - Visit /plinth/sys/backups/create/foo/. No apps are selected. - Visit /plinth/sys/backups/create/. All apps are selected. Signed-off-by: Sunil Mohan Adapa Reviewed-by: James Valleroy --- plinth/modules/backups/forms.py | 7 ++++--- plinth/modules/backups/urls.py | 4 ++-- plinth/modules/backups/views.py | 8 ++++++++ 3 files changed, 14 insertions(+), 5 deletions(-) diff --git a/plinth/modules/backups/forms.py b/plinth/modules/backups/forms.py index ac70ad2fa..5b0d9c07f 100644 --- a/plinth/modules/backups/forms.py +++ b/plinth/modules/backups/forms.py @@ -110,9 +110,10 @@ class CreateArchiveForm(forms.Form): components = api.get_all_components_for_backup() choices = _get_app_choices(components) self.fields['selected_apps'].choices = choices - self.fields['selected_apps'].initial = [ - choice[0] for choice in choices - ] + if not self.initial or 'selected_apps' not in self.initial: + self.fields['selected_apps'].initial = [ + choice[0] for choice in choices + ] self.fields['repository'].choices = _get_repository_choices() diff --git a/plinth/modules/backups/urls.py b/plinth/modules/backups/urls.py index 4aace50db..d8876b27f 100644 --- a/plinth/modules/backups/urls.py +++ b/plinth/modules/backups/urls.py @@ -15,8 +15,8 @@ urlpatterns = [ re_path(r'^sys/backups/$', BackupsView.as_view(), name='index'), re_path(r'^sys/backups/(?P[^/]+)/schedule/$', ScheduleView.as_view(), name='schedule'), - re_path(r'^sys/backups/create/$', CreateArchiveView.as_view(), - name='create'), + re_path(r'^sys/backups/create/(?:(?P[1-9a-z\-_]+)/)?$', + CreateArchiveView.as_view(), name='create'), re_path(r'^sys/backups/(?P[^/]+)/download/(?P[^/]+)/$', DownloadArchiveView.as_view(), name='download'), re_path(r'^sys/backups/(?P[^/]+)/delete/(?P[^/]+)/$', diff --git a/plinth/modules/backups/views.py b/plinth/modules/backups/views.py index fdadea719..2dc0e7407 100644 --- a/plinth/modules/backups/views.py +++ b/plinth/modules/backups/views.py @@ -116,6 +116,14 @@ class CreateArchiveView(SuccessMessageMixin, FormView): context['title'] = _('Create a new backup') return context + def get_initial(self): + """Return initialization arguments to the form.""" + initial = super().get_initial() + if 'app_id' in self.kwargs: + initial['selected_apps'] = [self.kwargs['app_id']] + + return initial + def form_valid(self, form): """Create the archive on valid form submission.""" repository = get_instance(form.cleaned_data['repository'])