mirror of
https://github.com/freedombox/FreedomBox.git
synced 2026-06-03 10:50:20 +00:00
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 <sunil@medhas.org> Reviewed-by: James Valleroy <jvalleroy@mailbox.org>
This commit is contained in:
parent
b5d7a910dd
commit
5df34d1927
@ -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):
|
||||
|
||||
@ -60,14 +60,8 @@
|
||||
|
||||
<h3>{% trans 'Existing Backups' %}</h3>
|
||||
|
||||
{% 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 %}
|
||||
|
||||
<a title="{% trans 'Add a backup location' %}"
|
||||
|
||||
@ -38,12 +38,12 @@ from django.views.generic import FormView, TemplateView, View
|
||||
from plinth.errors import PlinthError
|
||||
from plinth.modules import backups, storage
|
||||
|
||||
from . import (ROOT_REPOSITORY, SESSION_PATH_VARIABLE, api, forms,
|
||||
from . import (SESSION_PATH_VARIABLE, api, forms,
|
||||
get_known_hosts_path, is_ssh_hostkey_verified, network_storage,
|
||||
split_path)
|
||||
from .decorators import delete_tmp_backup_file
|
||||
from .errors import BorgRepositoryDoesNotExistError
|
||||
from .repository import (BorgRepository, RootBorgRepository, SshBorgRepository,
|
||||
from .repository import (BorgRepository, SshBorgRepository,
|
||||
create_repository, get_repositories)
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
@ -60,10 +60,7 @@ class IndexView(TemplateView):
|
||||
context['title'] = backups.name
|
||||
context['description'] = backups.description
|
||||
context['manual_page'] = backups.manual_page
|
||||
root_repository = RootBorgRepository(path=ROOT_REPOSITORY)
|
||||
context['root_repository'] = root_repository.get_view_content()
|
||||
context['ssh_repositories'] = get_repositories('ssh')
|
||||
context['disk_repositories'] = get_repositories('disk')
|
||||
context['repositories'] = get_repositories()
|
||||
return context
|
||||
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user