mirror of
https://github.com/freedombox/FreedomBox.git
synced 2026-08-19 12:36:06 +00:00
storage: Show low disk space warning using notifications API
Signed-off-by: Sunil Mohan Adapa <sunil@medhas.org> Reviewed-by: James Valleroy <jvalleroy@mailbox.org>
This commit is contained in:
parent
8529022f63
commit
5714fc6f51
@ -17,15 +17,17 @@
|
||||
"""
|
||||
FreedomBox app to manage storage.
|
||||
"""
|
||||
|
||||
import logging
|
||||
import subprocess
|
||||
|
||||
import psutil
|
||||
from django.utils.translation import ugettext_lazy as _
|
||||
from django.utils.translation import ugettext_noop
|
||||
|
||||
from plinth import actions
|
||||
from plinth import app as app_module
|
||||
from plinth import cfg, menu, utils
|
||||
from plinth import cfg, glib, menu, utils
|
||||
from plinth.daemon import Daemon
|
||||
from plinth.errors import ActionError, PlinthError
|
||||
from plinth.utils import format_lazy, import_from_gi
|
||||
@ -72,6 +74,9 @@ class StorageApp(app_module.App):
|
||||
daemon = Daemon('daemon-udiskie', managed_services[0])
|
||||
self.add(daemon)
|
||||
|
||||
# Check every hour for low disk space
|
||||
glib.schedule(3600, warn_about_low_disk_space)
|
||||
|
||||
|
||||
def init():
|
||||
"""Initialize the module."""
|
||||
@ -288,3 +293,54 @@ def setup(helper, old_version=None):
|
||||
expand_partition(root_device)
|
||||
except ActionError:
|
||||
pass
|
||||
|
||||
|
||||
def warn_about_low_disk_space(request):
|
||||
"""Warn about insufficient space on root partition."""
|
||||
from plinth.notification import Notification
|
||||
|
||||
try:
|
||||
root_info = get_disk_info('/')
|
||||
except PlinthError as exception:
|
||||
logger.exception('Error getting information about root partition: %s',
|
||||
exception)
|
||||
return
|
||||
|
||||
show = False
|
||||
if root_info['percent_used'] > 90 or root_info['free_gib'] < 1:
|
||||
severity = 'error'
|
||||
show = True
|
||||
elif root_info['percent_used'] > 75 or root_info['free_gib'] < 2:
|
||||
severity = 'warning'
|
||||
show = True
|
||||
|
||||
if not show:
|
||||
try:
|
||||
Notification.get('storage-low-disk-space').delete()
|
||||
except KeyError:
|
||||
pass
|
||||
else:
|
||||
# Translators: xgettext:no-python-format
|
||||
message = ugettext_noop(
|
||||
'Low space on system partition: {percent_used}% used, '
|
||||
'{free_space} free.')
|
||||
title = ugettext_noop('Low disk space')
|
||||
data = {
|
||||
'app_icon': 'fa-hdd-o',
|
||||
'app_name': ugettext_noop('Storage'),
|
||||
'percent_used': root_info['percent_used'],
|
||||
'free_space': format_bytes(root_info['free_bytes'])
|
||||
}
|
||||
actions = [{
|
||||
'type': 'link',
|
||||
'class': 'primary',
|
||||
'text': 'Go to {app_name}',
|
||||
'url': 'storage:index'
|
||||
}, {
|
||||
'type': 'dismiss'
|
||||
}]
|
||||
Notification.update_or_create(id='storage-low-disk-space',
|
||||
app_id='storage', severity=severity,
|
||||
title=title, message=message,
|
||||
actions=actions, data=data,
|
||||
group='admin')
|
||||
|
||||
@ -30,11 +30,9 @@ from django.utils.translation import ugettext as _
|
||||
from django.views.decorators.http import require_POST
|
||||
|
||||
from plinth import actions, views
|
||||
from plinth.errors import PlinthError
|
||||
from plinth.modules import storage
|
||||
from plinth.utils import format_lazy, is_user_admin
|
||||
|
||||
from . import get_disk_info, get_error_message
|
||||
from . import get_error_message
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
@ -48,11 +46,6 @@ class StorageAppView(views.AppView):
|
||||
template_name = 'storage.html'
|
||||
show_status_block = False
|
||||
|
||||
def render_to_response(self, context, **response_kwargs):
|
||||
"""Add disk space warning to the view."""
|
||||
warn_about_low_disk_space(self.request)
|
||||
return super().render_to_response(context, **response_kwargs)
|
||||
|
||||
def get_context_data(self, *args, **kwargs):
|
||||
"""Return template context data."""
|
||||
context = super().get_context_data(*args, **kwargs)
|
||||
@ -98,31 +91,6 @@ def expand_partition(request, device):
|
||||
messages.success(request, _('Partition expanded successfully.'))
|
||||
|
||||
|
||||
def warn_about_low_disk_space(request):
|
||||
"""Warn about insufficient space on root partition."""
|
||||
if not is_user_admin(request, cached=True):
|
||||
return
|
||||
|
||||
try:
|
||||
root_info = get_disk_info('/')
|
||||
except PlinthError as exception:
|
||||
logger.exception('Error getting information about root partition: %s',
|
||||
exception)
|
||||
return
|
||||
|
||||
message = format_lazy(
|
||||
# Translators: xgettext:no-python-format
|
||||
_('Warning: Low space on system partition ({percent_used}% used, '
|
||||
'{free_space} free).'),
|
||||
percent_used=root_info['percent_used'],
|
||||
free_space=storage.format_bytes(root_info['free_bytes']))
|
||||
|
||||
if root_info['percent_used'] > 90 or root_info['free_gib'] < 1:
|
||||
messages.error(request, message)
|
||||
elif root_info['percent_used'] > 75 or root_info['free_gib'] < 2:
|
||||
messages.warning(request, message)
|
||||
|
||||
|
||||
@require_POST
|
||||
def eject(request, device_path):
|
||||
"""Eject a device, given its path.
|
||||
|
||||
@ -64,9 +64,6 @@ def index(request):
|
||||
]
|
||||
selected_shortcut = selected_shortcut[0] if selected_shortcut else None
|
||||
|
||||
from plinth.modules.storage import views as disk_views
|
||||
disk_views.warn_about_low_disk_space(request)
|
||||
|
||||
return TemplateResponse(
|
||||
request, 'index.html', {
|
||||
'title': _('FreedomBox'),
|
||||
@ -88,8 +85,6 @@ class AppsIndexView(TemplateView):
|
||||
|
||||
def system_index(request):
|
||||
"""Serve the system index page."""
|
||||
from plinth.modules.storage import views as disk_views
|
||||
disk_views.warn_about_low_disk_space(request)
|
||||
return TemplateResponse(request, 'system.html',
|
||||
{'advanced_mode': get_advanced_mode()})
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user