backups: Add apps selection to restore form

Signed-off-by: James Valleroy <jvalleroy@mailbox.org>
Reviewed-by: Joseph Nuthalapati <njoseph@thoughtworks.com>
This commit is contained in:
James Valleroy 2018-09-01 20:36:23 -04:00 committed by Joseph Nuthalapati
parent 50b4bc86ae
commit 461fe21a19
No known key found for this signature in database
GPG Key ID: 5398F00A2FA43C35
3 changed files with 29 additions and 6 deletions

View File

@ -57,3 +57,19 @@ class ExportArchiveForm(forms.Form):
"""Initialize the form with disk choices."""
super().__init__(*args, **kwargs)
self.fields['disk'].choices = get_export_locations()
class RestoreForm(forms.Form):
selected_apps = forms.MultipleChoiceField(
label=_('Restore apps'),
help_text=_('Apps data to restore from the backup'),
widget=forms.CheckboxSelectMultiple)
def __init__(self, *args, **kwargs):
"""Initialize the form with selectable apps."""
super().__init__(*args, **kwargs)
apps = _list_of_all_apps_for_backup()
# TODO: Only list apps included in the backup file.
self.fields['selected_apps'].choices = [
(app[0], app[1].name) for app in apps]
self.fields['selected_apps'].initial = [app[0] for app in apps]

View File

@ -47,6 +47,8 @@
<form class="form" method="post">
{% csrf_token %}
{{ form|bootstrap }}
<input type="submit" class="btn btn-danger"
value="{% blocktrans trimmed %}
Restore data from {{ name }}

View File

@ -31,7 +31,7 @@ from urllib.parse import unquote
from plinth.modules import backups
from .backups import _list_of_all_apps_for_backup
from .forms import CreateArchiveForm, ExportArchiveForm
from .forms import CreateArchiveForm, ExportArchiveForm, RestoreForm
class IndexView(TemplateView):
@ -125,9 +125,13 @@ class ExportArchiveView(SuccessMessageMixin, FormView):
return super().form_valid(form)
class RestoreView(SuccessMessageMixin, TemplateView):
class RestoreView(SuccessMessageMixin, FormView):
"""View to restore files from an exported archive."""
form_class = RestoreForm
prefix = 'backups'
template_name = 'backups_restore.html'
success_url = reverse_lazy('backups:index')
success_message = _('Restored files from backup.')
def get_context_data(self, **kwargs):
"""Return additional context for rendering the template."""
@ -137,8 +141,9 @@ class RestoreView(SuccessMessageMixin, TemplateView):
context['name'] = self.kwargs['name']
return context
def post(self, request, label, name):
def form_valid(self, form):
"""Restore files from the archive on valid form submission."""
backups.restore_exported(label, name)
messages.success(request, _('Restored data from backup.'))
return redirect(reverse_lazy('backups:index'))
backups.restore_exported(unquote(self.kwargs['label']),
self.kwargs['name'],
form.cleaned_data['selected_apps'])
return super().form_valid(form)