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 <sunil@medhas.org>
Reviewed-by: James Valleroy <jvalleroy@mailbox.org>
This commit is contained in:
Sunil Mohan Adapa 2019-08-28 11:08:25 -07:00 committed by James Valleroy
parent 0df07f5cf2
commit 0b0791d78f
No known key found for this signature in database
GPG Key ID: 77C0C75E7B650808
3 changed files with 19 additions and 11 deletions

View File

@ -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

View File

@ -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):

View File

@ -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