def test_get(note): """Test retrieving a notification.""" assert Notification.get('test-notification') == note note.delete() with pytest.raises(KeyError): Notification.get('test-notification')
def _warn_about_low_ram_space(request): """Warn about insufficient RAM space.""" from plinth.notification import Notification memory_info = _get_memory_info() if memory_info['free_bytes'] < 1024**3: # Translators: This is the unit of computer storage Mebibyte similar to # Megabyte. memory_available_unit = gettext_noop('MiB') memory_available = memory_info['free_bytes'] / 1024**2 else: # Translators: This is the unit of computer storage Gibibyte similar to # Gigabyte. memory_available_unit = gettext_noop('GiB') memory_available = memory_info['free_bytes'] / 1024**3 show = False if memory_info['percent_used'] > 90: severity = 'error' advice_message = gettext_noop( 'You should disable some apps to reduce memory usage.') show = True elif memory_info['percent_used'] > 75: severity = 'warning' advice_message = gettext_noop( 'You should not install any new apps on this system.') show = True if not show: try: Notification.get('diagnostics-low-ram-space').delete() except KeyError: pass return message = gettext_noop( # xgettext:no-python-format 'System is low on memory: {percent_used}% used, {memory_available} ' '{memory_available_unit} free. {advice_message}') title = gettext_noop('Low Memory') data = { 'app_icon': 'fa-heartbeat', 'app_name': 'translate:' + gettext_noop('Diagnostics'), 'percent_used': f'{memory_info["percent_used"]:.1f}', 'memory_available': f'{memory_available:.1f}', 'memory_available_unit': 'translate:' + memory_available_unit, 'advice_message': 'translate:' + advice_message } actions = [{'type': 'dismiss'}] Notification.update_or_create(id='diagnostics-low-ram-space', app_id='diagnostics', severity=severity, title=title, message=message, actions=actions, data=data, group='admin')
def test_dismiss(note): """Test that setting dismissed value works.""" note.dismiss() assert note.dismissed Notification.get('test-notification') == note # Saved note.dismiss(should_dismiss=True) assert note.dismissed note.dismiss(should_dismiss=False) assert not note.dismissed
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: message = ugettext_noop( # xgettext:no-python-format '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')
def _show_new_release_notification(self): """When upgraded to new release, show a notification.""" from plinth.notification import Notification try: note = Notification.get('upgrades-new-release') if note.data['version'] == plinth.__version__: # User already has notification for update to this version. It # may be dismissed or not yet dismissed return # User currently has a notification for an older version, update. dismiss = False except KeyError: # Don't show notification for the first version user runs, create # but don't show it. dismiss = True data = { 'version': plinth.__version__, 'app_name': 'Update', 'app_icon': 'fa-refresh' } title = ugettext_noop('FreedomBox Updated') note = Notification.update_or_create( id='upgrades-new-release', app_id='upgrades', severity='info', title=title, body_template='upgrades-new-release.html', data=data, group='admin') note.dismiss(should_dismiss=dismiss)
def _show_schedule_error_notification(repository, is_error, exception=None): """Show or hide a notification related scheduled backup operation.""" from plinth.notification import Notification id_ = 'backups-schedule-error-' + repository.uuid try: note = Notification.get(id_) error_count = note.data['error_count'] except KeyError: error_count = 0 message = ugettext_noop( 'A scheduled backup failed. Past {error_count} attempts for backup ' 'did not succeed. The latest error is: {error_message}') data = { 'app_name': 'translate:' + ugettext_noop('Backups'), 'app_icon': 'fa-files-o', 'error_count': error_count + 1 if is_error else 0, 'error_message': str(exception) } title = ugettext_noop('Error During Backup') actions_ = [{ 'type': 'link', 'class': 'primary', 'text': ugettext_noop('Go to {app_name}'), 'url': 'backups:index' }, { 'type': 'dismiss' }] note = Notification.update_or_create(id=id_, app_id='backups', severity='error', title=title, message=message, actions=actions_, data=data, group='admin') note.dismiss(should_dismiss=not is_error)
def test_update(note): """Test updating a existing notification.""" note = Notification.get('test-notification') assert note.app_id == 'test-app' assert note.severity == 'info' assert note.title == 'Test Title' assert note.data == {'test-key': 'test-value'} Notification.update_or_create(id='test-notification', app_id='test-app2', severity='error', title='Test Title2', data={'test-key2': 'test-value2'}) note = Notification.get('test-notification') assert note.app_id == 'test-app2' assert note.severity == 'error' assert note.title == 'Test Title2' assert note.data == {'test-key2': 'test-value2'}
def on_schedule_save(repository): """Dismiss notification. Called when repository's schedule is updated.""" if not repository.schedule.enabled: return from plinth.notification import Notification try: note = Notification.get('backups-remote-schedule') note.dismiss() except KeyError: pass
def test_create(note): """Test creating a new notification works.""" note.delete() note.update_or_create(id='test-notification', app_id='test-app2', severity='error', title='Test Title2', data={'test-key2': 'test-value2'}) note = Notification.get('test-notification') assert note.app_id == 'test-app2' assert note.severity == 'error' assert note.title == 'Test Title2' assert note.data == {'test-key2': 'test-value2'}
def test_delete(note): """Test deleting a new notification works.""" note.delete() with pytest.raises(KeyError): Notification.get('test-notification')