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 <sunil@medhas.org>
Reviewed-by: James Valleroy <jvalleroy@mailbox.org>
This commit is contained in:
Sunil Mohan Adapa 2023-02-09 08:42:41 -08:00 committed by James Valleroy
parent 3667e934f1
commit 8f2520b327
No known key found for this signature in database
GPG Key ID: 77C0C75E7B650808
3 changed files with 14 additions and 5 deletions

View File

@ -110,6 +110,7 @@ class CreateArchiveForm(forms.Form):
components = api.get_all_components_for_backup()
choices = _get_app_choices(components)
self.fields['selected_apps'].choices = choices
if not self.initial or 'selected_apps' not in self.initial:
self.fields['selected_apps'].initial = [
choice[0] for choice in choices
]

View File

@ -15,8 +15,8 @@ urlpatterns = [
re_path(r'^sys/backups/$', BackupsView.as_view(), name='index'),
re_path(r'^sys/backups/(?P<uuid>[^/]+)/schedule/$', ScheduleView.as_view(),
name='schedule'),
re_path(r'^sys/backups/create/$', CreateArchiveView.as_view(),
name='create'),
re_path(r'^sys/backups/create/(?:(?P<app_id>[1-9a-z\-_]+)/)?$',
CreateArchiveView.as_view(), name='create'),
re_path(r'^sys/backups/(?P<uuid>[^/]+)/download/(?P<name>[^/]+)/$',
DownloadArchiveView.as_view(), name='download'),
re_path(r'^sys/backups/(?P<uuid>[^/]+)/delete/(?P<name>[^/]+)/$',

View File

@ -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'])