customization: Show custom shortcuts on frontpage

Closes #1379

Signed-off-by: Joseph Nuthalapati <njoseph@thoughtworks.com>
Reviewed-by: James Valleroy <jvalleroy@mailbox.org>
This commit is contained in:
Joseph Nuthalapati 2018-10-01 15:53:14 +05:30 committed by James Valleroy
parent 5e06017e5c
commit 1307e474da
No known key found for this signature in database
GPG Key ID: 77C0C75E7B650808
7 changed files with 63 additions and 28 deletions

View File

@ -24,14 +24,14 @@ import stat
import sys
import warnings
import axes
import cherrypy
import django.conf
import django.core.management
import django.core.wsgi
from django.contrib.messages import constants as message_constants
import axes
import cherrypy
from plinth import cfg, menu, module_loader, service, setup
from plinth import cfg, frontpage, menu, module_loader, service, setup
axes.default_app_config = "plinth.axes_app_config.AppConfig"
precedence_commandline_arguments = ["server_dir", "develop"]
@ -47,9 +47,10 @@ def parse_arguments():
# TODO: server_dir is actually a url prefix; use a better variable name
parser.add_argument('--server_dir', default=None,
help='web server path under which to serve')
parser.add_argument('--develop', action='store_true', default=None,
help=('run Plinth *insecurely* from current folder; '
'enable auto-reloading and debugging options'))
parser.add_argument(
'--develop', action='store_true', default=None,
help=('run Plinth *insecurely* from current folder; '
'enable auto-reloading and debugging options'))
parser.add_argument(
'--setup', default=False, nargs='*',
help='run setup tasks on all essential modules and exit')
@ -283,7 +284,7 @@ def configure_django():
DEBUG=cfg.develop,
FORCE_SCRIPT_NAME=cfg.server_dir,
INSTALLED_APPS=applications,
IPWARE_META_PRECEDENCE_ORDER=('HTTP_X_FORWARDED_FOR',),
IPWARE_META_PRECEDENCE_ORDER=('HTTP_X_FORWARDED_FOR', ),
LOGGING=logging_configuration,
LOGIN_URL='users:login',
LOGIN_REDIRECT_URL='index',
@ -301,7 +302,8 @@ def configure_django():
'plinth.middleware.AdminRequiredMiddleware',
'plinth.middleware.FirstSetupMiddleware',
'plinth.modules.first_boot.middleware.FirstBootMiddleware',
'plinth.middleware.SetupMiddleware', ),
'plinth.middleware.SetupMiddleware',
),
ROOT_URLCONF='plinth.urls',
SECURE_BROWSER_XSS_FILTER=True,
SECURE_CONTENT_TYPE_NOSNIFF=True,
@ -314,7 +316,8 @@ def configure_django():
'captcha-image',
'captcha-image-2x',
'captcha-audio',
'captcha-refresh', ),
'captcha-refresh',
),
TEMPLATES=templates,
USE_L10N=True,
USE_X_FORWARDED_HOST=cfg.use_x_forwarded_host)
@ -422,6 +425,7 @@ def main():
menu.init()
module_loader.load_modules()
frontpage.add_custom_shortcuts()
if arguments.setup is not False:
run_setup_and_exit(arguments.setup, allow_install=True)

View File

@ -17,6 +17,11 @@
"""
Manage application shortcuts on front page.
"""
import json
import os
from plinth import cfg
from . import actions
shortcuts = {}
@ -51,6 +56,37 @@ def get_shortcuts(username=None, web_apps_only=False, sort_by='label'):
return sorted(shortcuts_to_return.values(), key=lambda item: item[sort_by])
def add_custom_shortcuts():
custom_shortcuts = get_custom_shortcuts()
if custom_shortcuts:
for shortcut in custom_shortcuts['shortcuts']:
web_app_url = _extract_web_app_url(shortcut)
if web_app_url:
add_shortcut(None, shortcut['name'],
shortcut['short_description'],
icon=shortcut['icon_url'], url=web_app_url)
def _extract_web_app_url(custom_shortcut):
if custom_shortcut.get('clients'):
for client in custom_shortcut['clients']:
if client.get('platforms'):
for platform in client['platforms']:
if platform['type'] == 'web':
return platform['url']
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:
with open(shortcuts_file) as shortcuts:
custom_shortcuts = json.load(shortcuts)
return custom_shortcuts
return None
def add_shortcut(shortcut_id, name, short_description="", login_required=False,
icon=None, url=None, details=None, configure_url=None,
allowed_groups=None):

View File

@ -20,13 +20,12 @@ FreedomBox app for api for android app.
import copy
import json
import os
from django.core.serializers.json import DjangoJSONEncoder
from django.http import HttpResponse
from django.templatetags.static import static
from plinth import cfg, frontpage, module_loader
from plinth import frontpage, module_loader
from plinth.modules import names
@ -54,24 +53,14 @@ def shortcuts(request, **kwargs):
def get_shortcuts_as_json(username=None):
shortcuts = [
_get_shortcut_data(shortcut['id'].split('_')[0], shortcut)
for shortcut in frontpage.get_shortcuts(username)
for shortcut in frontpage.get_shortcuts(username) if shortcut['id']
]
custom_shortcuts = get_custom_shortcuts()
custom_shortcuts = frontpage.get_custom_shortcuts()
if custom_shortcuts:
shortcuts += custom_shortcuts['shortcuts']
return {'shortcuts': shortcuts}
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:
with open(shortcuts_file) as shortcuts:
custom_shortcuts = json.load(shortcuts)
return custom_shortcuts
return None
def _get_shortcut_data(module_name, shortcut):
"""Return detailed information about a shortcut."""
module = module_loader.loaded_modules[module_name]

View File

@ -44,7 +44,8 @@ def domain_label_validator(domainname):
def get_default_app_choices():
shortcuts = frontpage.get_shortcuts(web_apps_only=True, sort_by='name')
apps = [(shortcut['id'], shortcut['name']) for shortcut in shortcuts]
apps = [(shortcut['id'], shortcut['name']) for shortcut in shortcuts
if shortcut['id']]
return [('plinth', 'FreedomBox Service (Plinth)')] + apps

View File

@ -87,7 +87,11 @@
{{ shortcut.name }}
</div>
<div class="card-icon">
<img src="{% static 'theme/icons/' %}{{ shortcut.icon }}.png"/>
{% if "custom" in shortcut.icon %}
<img src="{{ shortcut.icon }}"/>
{% else %}
<img src="{% static 'theme/icons/' %}{{ shortcut.icon }}.png"/>
{% endif %}
</div>
<div class="card-description">
{{ shortcut.short_description }}

View File

@ -1 +1 @@
{"shortcuts": [{"name": "NextCloud", "short_description": "File Hosting Service", "description": "Nextcloud is a suite of client-server software for creating and using file hosting services.", "icon_url": "/plinth/custom/static/themes/default/icons/nextcloud.png", "clients": [{"name": "nextcloud", "platforms": [{"type": "web", "url": "/nextcloud"}]}]}]}
{"shortcuts": [{"name": "NextCloud", "short_description": "File Hosting Service", "description": ["Nextcloud is a suite of client-server software for creating and using file hosting services."], "icon_url": "/plinth/custom/static/themes/default/icons/nextcloud.png", "clients": [{"name": "nextcloud", "platforms": [{"type": "web", "url": "/nextcloud"}]}]}]}

View File

@ -37,9 +37,10 @@ NEXTCLOUD_SHORTCUT = {
'NextCloud',
'short_description':
'File Hosting Service',
'description':
'description': [
'Nextcloud is a suite of client-server software for creating '
'and using file hosting services.',
'and using file hosting services.'
],
'icon_url':
'/plinth/custom/static/themes/default/icons/nextcloud.png',
'clients': [{