From 13c4c687da2c1514fe273c5ad35c06ae9abc373a Mon Sep 17 00:00:00 2001 From: Sunil Mohan Adapa Date: Wed, 6 Dec 2017 15:06:19 +0530 Subject: [PATCH] api: Update for clarity (API breaking change) - Change the term 'service' to 'shortcut' as what we want to present is actually shortcuts and not services. Services is already a confused term between firewall service and a service daemon. - Fix icon paths containing relative URLs. - Make the access information API extensible in future. - Send full list of domains in access information and not just one domain per domain type. Signed-off-by: Sunil Mohan Adapa Reviewed-by: James Valleroy --- plinth/modules/api/urls.py | 6 ++-- plinth/modules/api/views.py | 64 ++++++++++++++++++++----------------- 2 files changed, 37 insertions(+), 33 deletions(-) diff --git a/plinth/modules/api/urls.py b/plinth/modules/api/urls.py index f070276d3..37af90e53 100644 --- a/plinth/modules/api/urls.py +++ b/plinth/modules/api/urls.py @@ -21,9 +21,9 @@ URLs for the plinth api for android app. from django.conf.urls import url from stronghold.decorators import public -from plinth.modules.api.views import get_access_info, get_services +from plinth.modules.api import views urlpatterns = [ - url(r'^api/(?P[0-9]+)/services/?$', public(get_services)), - url(r'^api/(?P[0-9]+)/access-info/?$', public(get_access_info)), + url(r'^api/(?P[0-9]+)/shortcuts/?$', public(views.shortcuts)), + url(r'^api/(?P[0-9]+)/access-info/?$', public(views.access_info)), ] diff --git a/plinth/modules/api/views.py b/plinth/modules/api/views.py index 58166c5e1..ab4dfda01 100644 --- a/plinth/modules/api/views.py +++ b/plinth/modules/api/views.py @@ -20,49 +20,53 @@ Plinth module for api for android app. from django.http import HttpResponse from django.core.serializers.json import DjangoJSONEncoder +from django.templatetags.static import static -from plinth.modules.names import get_domain, get_domain_types from plinth import frontpage from plinth import module_loader +from plinth.modules import names import json -def get_access_info(request, **kwargs): - response = { - domain_type: get_domain(domain_type) - for domain_type in get_domain_types() - } - return HttpResponse(json.dumps(response), content_type="application/json") +def access_info(request, **kwargs): + """API view to return a list of domains and types.""" + domains = [{ + 'domain': domain, + 'type': domain_type + } for domain_type, domains in names.domains.items() for domain in domains] + response = {'domains': domains} + + return HttpResponse(json.dumps(response), content_type='application/json') -def get_services(request, **kwargs): - services = [shortcut['id'].split('_')[0] - for shortcut in frontpage.get_shortcuts()] - response = {'services': list(map(_get_service_data, services))} +def shortcuts(request, **kwargs): + """API view to return the list of frontpage services.""" + # XXX: Get the module (or module name) from shortcut properly. + shortcuts = [ + _get_shortcut_data(shortcut['id'].split('_')[0], shortcut) + for shortcut in frontpage.get_shortcuts() + ] + response = {'shortcuts': shortcuts} return HttpResponse( json.dumps(response, cls=DjangoJSONEncoder), - content_type="application/json") + content_type='application/json') -def _get_service_data(service): - module = module_loader.loaded_modules[service] - - def _getattr(attr, not_found=None): - """A closure to get the enclosed module's attributes""" - return getattr(module, attr, not_found) - +def _get_shortcut_data(module_name, shortcut): + """Return detailed information about a shortcut.""" + module = module_loader.loaded_modules[module_name] return { - key: value - for key, value in dict( - name=module.name, - short_description=_getattr('short_description'), - icon_url=_get_icon_url(_getattr('icon')), - description=_getattr('description'), - usage=_getattr('usage'), - manual_url=_getattr('manual_url'), - clients=_getattr('clients')).items() + 'name': shortcut['name'], + 'short_description': shortcut['short_description'], + 'description': shortcut['details'], + 'icon_url': _get_icon_url(shortcut['icon']), + 'clients': getattr(module, 'clients', None) } -def _get_icon_url(icon): - return 'static/theme/icons/{}.svg'.format(icon) if icon else None +def _get_icon_url(icon_name): + """Return icon path given icon name.""" + if not icon_name: + return None + + return static('static/theme/icons/{}.png'.format(icon_name))