Backups: show free disk space on upload+restore page

Reviewed-by: James Valleroy <jvalleroy@mailbox.org>
This commit is contained in:
Michael Pimmer 2018-10-29 10:11:30 +00:00 committed by James Valleroy
parent 8cc74bd203
commit d4d5c15566
No known key found for this signature in database
GPG Key ID: 77C0C75E7B650808
4 changed files with 46 additions and 19 deletions

View File

@ -32,7 +32,10 @@
You can choose the apps you wish to import after uploading a backup file. You can choose the apps you wish to import after uploading a backup file.
{% endblocktrans %} {% endblocktrans %}
</p> </p>
<br /> <div class="alert alert-info" role="alert">
{% trans "Free disk space" %}: {{ free_space }}<br />
{% trans "Make sure the uploaded file is smaller than that." %}
</div>
<form class="form" enctype="multipart/form-data" method="post"> <form class="form" enctype="multipart/form-data" method="post">
{% csrf_token %} {% csrf_token %}

View File

@ -21,6 +21,7 @@ Views for the backups app.
from datetime import datetime from datetime import datetime
import gzip import gzip
from io import BytesIO from io import BytesIO
import logging
import mimetypes import mimetypes
import os import os
import tempfile import tempfile
@ -36,12 +37,15 @@ from django.utils.translation import ugettext as _
from django.utils.translation import ugettext_lazy from django.utils.translation import ugettext_lazy
from django.views.generic import View, FormView, TemplateView from django.views.generic import View, FormView, TemplateView
from plinth.modules import backups
from plinth import actions from plinth import actions
from plinth.errors import PlinthError
from plinth.modules import backups, storage
from . import api, forms, SESSION_PATH_VARIABLE from . import api, forms, SESSION_PATH_VARIABLE
from .decorators import delete_tmp_backup_file from .decorators import delete_tmp_backup_file
logger = logging.getLogger(__name__)
subsubmenu = [{ subsubmenu = [{
'url': reverse_lazy('backups:index'), 'url': reverse_lazy('backups:index'),
'text': ugettext_lazy('Backups') 'text': ugettext_lazy('Backups')
@ -142,6 +146,13 @@ class UploadArchiveView(SuccessMessageMixin, FormView):
context = super().get_context_data(**kwargs) context = super().get_context_data(**kwargs)
context['title'] = _('Upload and restore a backup file') context['title'] = _('Upload and restore a backup file')
context['subsubmenu'] = subsubmenu context['subsubmenu'] = subsubmenu
try:
disk_info = storage.get_disk_info('/', self.request)
except (PlinthError, PermissionError):
logger.error('Error getting information about root partition.')
else:
context['free_space'] = storage.format_bytes(
disk_info["free_bytes"])
return context return context
def form_valid(self, form): def form_valid(self, form):

View File

@ -25,8 +25,9 @@ from django.utils.translation import ugettext_lazy as _
from plinth import service as service_module from plinth import service as service_module
from plinth import action_utils, actions, cfg from plinth import action_utils, actions, cfg
from plinth.errors import PlinthError
from plinth.menu import main_menu from plinth.menu import main_menu
from plinth.utils import format_lazy from plinth.utils import format_lazy, is_user_admin
from .manifest import backup from .manifest import backup
@ -77,6 +78,24 @@ def get_disks():
return combined_list return combined_list
def get_disk_info(mountpoint, request):
"""Get information about the free space of a drive"""
if not is_user_admin(request, cached=True):
raise PermissionError
disks = get_disks()
list_root = [disk for disk in disks if disk['mountpoint'] == mountpoint]
if not list_root:
raise PlinthError
percent_used = list_root[0]['percent_used']
free_bytes = list_root[0]['free']
free_gib = free_bytes / (1024**3)
return {
"percent_used": percent_used,
"free_bytes": free_bytes,
"free_gib": free_gib
}
def _get_disks_from_df(): def _get_disks_from_df():
"""Return the list of disks and free space available using 'df'.""" """Return the list of disks and free space available using 'df'."""
command = [ command = [

View File

@ -30,10 +30,11 @@ from django.utils.translation import ugettext as _
from django.views.decorators.http import require_POST from django.views.decorators.http import require_POST
from plinth import actions from plinth import actions
from plinth.errors import PlinthError
from plinth.modules import storage from plinth.modules import storage
from plinth.utils import format_lazy, is_user_admin from plinth.utils import format_lazy
from . import udisks2 from . import udisks2, get_disk_info
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
@ -91,29 +92,22 @@ def expand_partition(request, device):
def warn_about_low_disk_space(request): def warn_about_low_disk_space(request):
"""Warn about insufficient space on root partition.""" """Warn about insufficient space on root partition."""
if not is_user_admin(request, cached=True): try:
return root_info = get_disk_info('/', request)
except (PlinthError, PermissionError):
disks = storage.get_disks()
list_root = [disk for disk in disks if disk['mountpoint'] == '/']
if not list_root:
logger.error('Error getting information about root partition.') logger.error('Error getting information about root partition.')
return return
percent_used = list_root[0]['percent_used']
free_bytes = list_root[0]['free']
free_gib = free_bytes / (1024**3)
message = format_lazy( message = format_lazy(
# Translators: xgettext:no-python-format # Translators: xgettext:no-python-format
_('Warning: Low space on system partition ({percent_used}% used, ' _('Warning: Low space on system partition ({percent_used}% used, '
'{free_space} free).'), '{free_space} free).'),
percent_used=percent_used, percent_used=root_info["percent_used"],
free_space=storage.format_bytes(free_bytes)) free_space=storage.format_bytes(root_info["free_bytes"]))
if percent_used > 90 or free_gib < 1: if root_info["percent_used"] > 90 or root_info["free_gib"] < 1:
messages.error(request, message) messages.error(request, message)
elif percent_used > 75 or free_gib < 2: elif root_info["percent_used"] > 75 or root_info["free_gib"] < 2:
messages.warning(request, message) messages.warning(request, message)