backups: Minor styling fixes

- More descriptive iteration variables
- Run yafp, isort

Signed-off-by: Joseph Nuthalapati <njoseph@thoughtworks.com>
This commit is contained in:
Joseph Nuthalapati 2018-09-23 08:54:57 -07:00
parent dc781b03fa
commit 670f58019e
No known key found for this signature in database
GPG Key ID: 5398F00A2FA43C35
4 changed files with 27 additions and 31 deletions

View File

@ -84,10 +84,10 @@ def _backup_handler(packet):
manifest_path = MANIFESTS_FOLDER + get_valid_filename( manifest_path = MANIFESTS_FOLDER + get_valid_filename(
packet.label) + '.json' packet.label) + '.json'
manifests = [{ manifests = [{
'name': x[0], 'name': manifest[0],
'version': x[1].version, 'version': manifest[1].version,
'backup': x[2] 'backup': manifest[2]
} for x in packet.manifests] } for manifest in packet.manifests]
with open(manifest_path, 'w') as manifest_file: with open(manifest_path, 'w') as manifest_file:
json.dump(manifests, manifest_file) json.dump(manifests, manifest_file)
@ -134,7 +134,7 @@ def get_export_files():
export_files = {} export_files = {}
for location in locations: for location in locations:
output = actions.superuser_run( output = actions.superuser_run(
'backups', ['list-exports', '--location', location[0]]) 'backups', ['list-exports', '--location', location[0]])
export_files[location[1]] = json.loads(output) export_files[location[1]] = json.loads(output)
return export_files return export_files
@ -157,21 +157,17 @@ def find_exported_archive(disk_label, archive_name):
def get_export_apps(filename): def get_export_apps(filename):
"""Get list of apps included in exported archive file.""" """Get list of apps included in exported archive file."""
output = actions.superuser_run( output = actions.superuser_run('backups',
'backups', ['get-export-apps', '--filename', filename]) ['get-export-apps', '--filename', filename])
return output.splitlines() return output.splitlines()
def _restore_handler(packet): def _restore_handler(packet):
"""Perform restore operation on packet.""" """Perform restore operation on packet."""
locations = { locations = {'directories': packet.directories, 'files': packet.files}
'directories': packet.directories,
'files': packet.files
}
locations_data = json.dumps(locations) locations_data = json.dumps(locations)
actions.superuser_run( actions.superuser_run('backups', ['restore', '--filename', packet.label],
'backups', ['restore', '--filename', packet.label], input=locations_data.encode())
input=locations_data.encode())
def restore_exported(label, name, apps=None): def restore_exported(label, name, apps=None):

View File

@ -91,9 +91,9 @@ class Packet:
"""Look at manifests and fill up the list of directories/files.""" """Look at manifests and fill up the list of directories/files."""
for manifest in self.manifests: for manifest in self.manifests:
backup = manifest[2] backup = manifest[2]
for x in ['config', 'data', 'secrets']: for section in ['config', 'data', 'secrets']:
self.directories += backup[x]['directories'] self.directories += backup[section]['directories']
self.files += backup[x]['files'] self.files += backup[section]['files']
def backup_full(backup_handler, label=None): def backup_full(backup_handler, label=None):

View File

@ -58,11 +58,11 @@ class TestBackups(unittest.TestCase):
packet = Packet('backup', 'apps', '/', manifests) packet = Packet('backup', 'apps', '/', manifests)
for manifest in manifests: for manifest in manifests:
backup = manifest[2] backup = manifest[2]
for x in ['config', 'data', 'secrets']: for section in ['config', 'data', 'secrets']:
for d in backup[x]['directories']: for directory in backup[section]['directories']:
assert d in packet.directories assert directory in packet.directories
for f in backup[x]['files']: for file_path in backup[section]['files']:
assert f in packet.files assert file_path in packet.files
def test_backup_apps(self): def test_backup_apps(self):
"""Test that backup_handler is called.""" """Test that backup_handler is called."""
@ -88,7 +88,7 @@ class TestBackups(unittest.TestCase):
load_modules() load_modules()
app_names = ['config', 'names'] app_names = ['config', 'names']
apps = _get_apps_in_order(app_names) apps = _get_apps_in_order(app_names)
ordered_app_names = [x[0] for x in apps] ordered_app_names = [app[0] for app in apps]
names_index = ordered_app_names.index('names') names_index = ordered_app_names.index('names')
config_index = ordered_app_names.index('config') config_index = ordered_app_names.index('config')

View File

@ -19,6 +19,7 @@ Views for the backups app.
""" """
from datetime import datetime from datetime import datetime
from urllib.parse import unquote
from django.contrib import messages from django.contrib import messages
from django.contrib.messages.views import SuccessMessageMixin from django.contrib.messages.views import SuccessMessageMixin
@ -27,7 +28,6 @@ from django.shortcuts import redirect
from django.urls import reverse_lazy from django.urls import reverse_lazy
from django.utils.translation import ugettext as _ from django.utils.translation import ugettext as _
from django.views.generic import FormView, TemplateView from django.views.generic import FormView, TemplateView
from urllib.parse import unquote
from plinth.modules import backups from plinth.modules import backups
@ -49,7 +49,7 @@ class IndexView(TemplateView):
context['archives'] = backups.list_archives() context['archives'] = backups.list_archives()
context['exports'] = backups.get_export_files() context['exports'] = backups.get_export_files()
apps = backups_api.get_all_apps_for_backup() apps = backups_api.get_all_apps_for_backup()
context['available_apps'] = [x[0] for x in apps] context['available_apps'] = [app[0] for app in apps]
return context return context
@ -122,8 +122,7 @@ class ExportArchiveView(SuccessMessageMixin, FormView):
def form_valid(self, form): def form_valid(self, form):
"""Create the archive on valid form submission.""" """Create the archive on valid form submission."""
backups.export_archive(self.kwargs['name'], backups.export_archive(self.kwargs['name'], form.cleaned_data['disk'])
form.cleaned_data['disk'])
return super().form_valid(form) return super().form_valid(form)
@ -157,7 +156,8 @@ class RestoreView(SuccessMessageMixin, FormView):
"""Pass additional keyword args for instantiating the form.""" """Pass additional keyword args for instantiating the form."""
kwargs = super().get_form_kwargs() kwargs = super().get_form_kwargs()
kwargs['apps'] = [ kwargs['apps'] = [
x for x in self.installed_apps if x[0] in self.included_apps] app for app in self.installed_apps if app[0] in self.included_apps
]
return kwargs return kwargs
def get_context_data(self, **kwargs): def get_context_data(self, **kwargs):
@ -170,7 +170,7 @@ class RestoreView(SuccessMessageMixin, FormView):
def form_valid(self, form): def form_valid(self, form):
"""Restore files from the archive on valid form submission.""" """Restore files from the archive on valid form submission."""
backups.restore_exported(unquote(self.kwargs['label']), backups.restore_exported(
self.kwargs['name'], unquote(self.kwargs['label']), self.kwargs['name'],
form.cleaned_data['selected_apps']) form.cleaned_data['selected_apps'])
return super().form_valid(form) return super().form_valid(form)