From 5df34d192746e289df83562f630dc2e972e17c72 Mon Sep 17 00:00:00 2001 From: Sunil Mohan Adapa Date: Wed, 28 Aug 2019 00:12:26 -0700 Subject: [PATCH] backups: Simplify listing repositories in index page - Use sort_order property to decide which type of repositories should be listed first. - Remove getting repositories of a given type and retrieve all of them at the same time. Signed-off-by: Sunil Mohan Adapa Reviewed-by: James Valleroy --- plinth/modules/backups/repository.py | 40 ++++++------------- plinth/modules/backups/templates/backups.html | 10 +---- plinth/modules/backups/views.py | 9 ++--- 3 files changed, 18 insertions(+), 41 deletions(-) diff --git a/plinth/modules/backups/repository.py b/plinth/modules/backups/repository.py index 82a541411..5cb0fa86e 100644 --- a/plinth/modules/backups/repository.py +++ b/plinth/modules/backups/repository.py @@ -80,7 +80,6 @@ KNOWN_ERRORS = [{ class BaseBorgRepository(abc.ABC): """Base class for all kinds of Borg repositories.""" - uuid = None flags = {} is_mounted = True @@ -142,6 +141,7 @@ class BaseBorgRepository(abc.ABC): def get_view_content(self): """Get archives with additional information as needed by the view""" repository = { + 'uuid': self.uuid, 'name': self.name, 'storage_type': self.storage_type, 'flags': self.flags, @@ -296,10 +296,12 @@ class RootBorgRepository(BaseBorgRepository): storage_type = 'root' name = ROOT_REPOSITORY_NAME repo_path = ROOT_REPOSITORY + sort_order = 10 is_mounted = True def __init__(self, path, credentials=None): """Initialize the repository object.""" + self.uuid = ROOT_REPOSITORY_UUID if credentials is None: credentials = {} @@ -314,6 +316,7 @@ class BorgRepository(BaseBorgRepository): """General Borg repository implementation.""" KNOWN_CREDENTIALS = ['encryption_passphrase'] storage_type = 'disk' + sort_order = 20 flags = {'removable': True} @property @@ -334,6 +337,7 @@ class SshBorgRepository(BaseBorgRepository): 'ssh_keyfile', 'ssh_password', 'encryption_passphrase' ] storage_type = 'ssh' + sort_order = 30 flags = {'removable': True, 'mountable': True} @property @@ -404,34 +408,16 @@ class SshBorgRepository(BaseBorgRepository): return (arguments, kwargs) -def get_repositories(storage_type): +def get_repositories(): """Get all repositories of a given storage type.""" - if storage_type == 'disk': - return _get_disk_repositories() + repositories = [create_repository(ROOT_REPOSITORY_UUID)] + for uuid in network_storage.get_storages(): + repositories.append(create_repository(uuid)) - return _get_ssh_repositories() - - -def _get_ssh_repositories(): - """Get all SSH Repositories including the archive content""" - repositories = {} - for storage in network_storage.get_storages().values(): - if storage['storage_type'] == 'ssh': - repository = SshBorgRepository(**storage) - repositories[storage['uuid']] = repository.get_view_content() - - return repositories - - -def _get_disk_repositories(): - """Get all disk repositories including the archive content""" - repositories = {} - for storage in network_storage.get_storages().values(): - if storage['storage_type'] == 'disk': - repository = BorgRepository(**storage) - repositories[storage['uuid']] = repository.get_view_content() - - return repositories + return [ + repository.get_view_content() + for repository in sorted(repositories, key=lambda x: x.sort_order) + ] def create_repository(uuid): diff --git a/plinth/modules/backups/templates/backups.html b/plinth/modules/backups/templates/backups.html index fe98d864d..8938cb5ba 100644 --- a/plinth/modules/backups/templates/backups.html +++ b/plinth/modules/backups/templates/backups.html @@ -60,14 +60,8 @@

{% trans 'Existing Backups' %}

- {% include "backups_repository.inc" with repository=root_repository uuid='root' %} - - {% for uuid,repository in ssh_repositories.items %} - {% include "backups_repository.inc" %} - {% endfor %} - - {% for uuid,repository in disk_repositories.items %} - {% include "backups_repository.inc" %} + {% for repository in repositories %} + {% include "backups_repository.inc" with uuid=repository.uuid %} {% endfor %}