From 0b0791d78fe038c939b13eb4da6375816386c67b Mon Sep 17 00:00:00 2001 From: Sunil Mohan Adapa Date: Wed, 28 Aug 2019 11:08:25 -0700 Subject: [PATCH] backups: Introduce method for checking if a repository is usable get_repositories method will return repositories instead of dictionaries for view content. This will make it usable in more situations. Signed-off-by: Sunil Mohan Adapa Reviewed-by: James Valleroy --- plinth/modules/backups/forms.py | 10 ++++------ plinth/modules/backups/repository.py | 16 ++++++++++++---- plinth/modules/backups/views.py | 4 +++- 3 files changed, 19 insertions(+), 11 deletions(-) diff --git a/plinth/modules/backups/forms.py b/plinth/modules/backups/forms.py index d855dfdb1..0a6ced275 100644 --- a/plinth/modules/backups/forms.py +++ b/plinth/modules/backups/forms.py @@ -32,7 +32,8 @@ from django.utils.translation import ugettext_lazy as _ from plinth.modules.storage import get_disks from plinth.utils import format_lazy -from . import ROOT_REPOSITORY_NAME, api, split_path, store +from . import api, split_path, store +from .repository import get_repositories logger = logging.getLogger(__name__) @@ -53,11 +54,8 @@ def _get_app_choices(apps): def _get_repository_choices(): """Return the list of available repositories.""" - choices = [('root', ROOT_REPOSITORY_NAME)] - storages = store.get_storages() - for storage in storages.values(): - if storage.get('verified'): - choices += [(storage['uuid'], storage['path'])] + choices = [(repository.uuid, repository.name) + for repository in get_repositories() if repository.is_usable()] return choices diff --git a/plinth/modules/backups/repository.py b/plinth/modules/backups/repository.py index 9bb7aab59..e8dd925e8 100644 --- a/plinth/modules/backups/repository.py +++ b/plinth/modules/backups/repository.py @@ -89,6 +89,8 @@ class BaseBorgRepository(abc.ABC): If only a uuid is given, load the values from kvstore. """ + self.kwargs = kwargs + if not uuid: uuid = str(uuid1()) self.uuid = uuid @@ -112,6 +114,11 @@ class BaseBorgRepository(abc.ABC): """Return the storage type of repository.""" raise NotImplementedError + @staticmethod + def is_usable(): + """Return whether the repository is ready to be used.""" + return True + @property def repo_path(self): """Return the repository that the backups action script should use.""" @@ -340,6 +347,10 @@ class SshBorgRepository(BaseBorgRepository): sort_order = 30 flags = {'removable': True, 'mountable': True} + def is_usable(self): + """Return whether repository is usable.""" + return self.kwargs.get('verified') + @property def repo_path(self): """ @@ -414,10 +425,7 @@ def get_repositories(): for uuid in store.get_storages(): repositories.append(create_repository(uuid)) - return [ - repository.get_view_content() - for repository in sorted(repositories, key=lambda x: x.sort_order) - ] + return sorted(repositories, key=lambda x: x.sort_order) def create_repository(uuid): diff --git a/plinth/modules/backups/views.py b/plinth/modules/backups/views.py index 6dfd39635..63da39b4d 100644 --- a/plinth/modules/backups/views.py +++ b/plinth/modules/backups/views.py @@ -60,7 +60,9 @@ class IndexView(TemplateView): context['title'] = backups.name context['description'] = backups.description context['manual_page'] = backups.manual_page - context['repositories'] = get_repositories() + context['repositories'] = [ + repository.get_view_content() for repository in get_repositories() + ] return context