frontpage: Read custom shortcuts from multiple locations

Read from /etc/plinth, /usr/share/plinth and /var/lib/plinth.

Signed-off-by: Sunil Mohan Adapa <sunil@medhas.org>
Reviewed-by: James Valleroy <jvalleroy@mailbox.org>
This commit is contained in:
Sunil Mohan Adapa 2020-06-23 19:30:11 -07:00 committed by James Valleroy
parent 4263f9e2c8
commit 5b579ff06d
No known key found for this signature in database
GPG Key ID: 77C0C75E7B650808
3 changed files with 20 additions and 15 deletions

View File

@ -5,7 +5,7 @@ Manage application shortcuts on front page.
import json
import logging
import os
import pathlib
from plinth import app, cfg
@ -129,8 +129,6 @@ class Shortcut(app.FollowerComponent):
def add_custom_shortcuts():
custom_shortcuts = get_custom_shortcuts()
if not custom_shortcuts:
return
for shortcut in custom_shortcuts['shortcuts']:
web_app_url = _extract_web_app_url(shortcut)
@ -161,11 +159,20 @@ def _extract_web_app_url(custom_shortcut):
def get_custom_shortcuts():
cfg_dir = os.path.dirname(cfg.config_file)
shortcuts_file = os.path.join(cfg_dir, 'custom-shortcuts.json')
if os.path.isfile(shortcuts_file) and os.stat(shortcuts_file).st_size:
logger.info('Loading custom shortcuts from %s', shortcuts_file)
with open(shortcuts_file) as shortcuts:
custom_shortcuts = json.load(shortcuts)
return custom_shortcuts
return None
"""Return a merged dictionary of all custom shortcuts."""
shortcuts_dirs = [cfg.config_dir, cfg.data_dir, cfg.file_root]
shortcuts = {'shortcuts': []}
for shortcuts_dir in shortcuts_dirs:
file_path = pathlib.Path(shortcuts_dir) / 'custom-shortcuts.json'
if not file_path.is_file() or not file_path.stat().st_size:
continue
logger.info('Loading custom shortcuts from %s', file_path)
with file_path.open() as file_handle:
try:
shortcuts['shortcuts'] += json.load(file_handle)['shortcuts']
except (KeyError, json.JSONDecodeError):
logger.info('Error loading shortcuts from %s', file_path)
return shortcuts

View File

@ -40,9 +40,7 @@ def get_shortcuts_as_json(username=None):
for shortcut in frontpage.Shortcut.list(username)
if shortcut.component_id and shortcut.is_enabled()
]
custom_shortcuts = frontpage.get_custom_shortcuts()
if custom_shortcuts:
shortcuts += custom_shortcuts['shortcuts']
shortcuts += frontpage.get_custom_shortcuts()['shortcuts']
return {'shortcuts': shortcuts}

View File

@ -31,7 +31,7 @@ NEXTCLOUD_SHORTCUT = {
@pytest.fixture(name='custom_shortcuts_file')
def fixture_custom_shortcuts_file(load_cfg, tmp_path):
"""Fixture to set path for a custom shortcuts file."""
load_cfg.config_file = str(tmp_path / 'plinth.conf')
load_cfg.config_dir = str(tmp_path)
return tmp_path / 'custom-shortcuts.json'