diff --git a/docs/api/settings.rst b/docs/api/settings.rst index c5947c4b5..f87887536 100644 --- a/docs/api/settings.rst +++ b/docs/api/settings.rst @@ -86,6 +86,266 @@ Regenerate the system wide API key :status 200: No error :status 403: No admin rights +.. _sec-api-settings-fetchtemplaatedata: + +Fetch template data +=================== + +.. http:get:: /api/settings/templates + + Fetch data (currently only the sorting order) of all registered template components in the system. + + Use this to get a full list of the identifiers of all UI components provided either by core OctoPrint or any + currently active plugins. + + Example: + + .. sourcecode:: http + + GET /api/settings/templates HTTP/1.1 + Host: example.com + X-Api-Key: abcdef... + + .. sourcecode:: http + + HTTP/1.1 200 OK + Content-Type: application/json + + { + "order": { + "about": [ + { + "id": "about", + "name": "About OctoPrint" + }, + { + "id": "supporters", + "name": "Supporters" + }, + { + "id": "authors", + "name": "Authors" + }, + { + "id": "changelog", + "name": "Changelog" + }, + { + "id": "license", + "name": "OctoPrint License" + }, + { + "id": "thirdparty", + "name": "Third Party Licenses" + }, + { + "id": "plugin_pluginmanager", + "name": "Plugin Licenses", + "plugin_id": "pluginmanager", + "plugin_name": "Plugin Manager" + } + ], + "generic": [ + { + "id": "plugin_announcements", + "name": "plugin_announcements", + "plugin_id": "announcements", + "plugin_name": "Announcement Plugin" + } + ], + "navbar": [ + { + "id": "settings", + "name": "settings" + }, + { + "id": "systemmenu", + "name": "systemmenu" + }, + { + "id": "plugin_announcements", + "name": "plugin_announcements", + "plugin_id": "announcements", + "plugin_name": "Announcement Plugin" + }, + { + "id": "login", + "name": "login" + } + ], + "plugin_pluginmanager_about_thirdparty": [], + "settings": [ + { + "id": "section_printer", + "name": "Printer" + }, + { + "id": "serial", + "name": "Serial Connection" + }, + { + "id": "printerprofiles", + "name": "Printer Profiles" + }, + { + "id": "temperatures", + "name": "Temperatures" + }, + { + "id": "terminalfilters", + "name": "Terminal Filters" + }, + { + "id": "gcodescripts", + "name": "GCODE Scripts" + }, + { + "id": "section_features", + "name": "Features" + }, + { + "id": "features", + "name": "Features" + }, + { + "id": "webcam", + "name": "Webcam & Timelapse" + }, + { + "id": "accesscontrol", + "name": "Access Control" + }, + { + "id": "gcodevisualizer", + "name": "GCODE Visualizer" + }, + { + "id": "api", + "name": "API" + }, + { + "id": "section_octoprint", + "name": "OctoPrint" + }, + { + "id": "server", + "name": "Server" + }, + { + "id": "folders", + "name": "Folders" + }, + { + "id": "appearance", + "name": "Appearance" + }, + { + "id": "plugin_logging", + "name": "Logging", + "plugin_id": "logging", + "plugin_name": "Logging" + }, + { + "id": "plugin_pluginmanager", + "name": "Plugin Manager", + "plugin_id": "pluginmanager", + "plugin_name": "Plugin Manager" + }, + { + "id": "plugin_softwareupdate", + "name": "Software Update", + "plugin_id": "softwareupdate", + "plugin_name": "Software Update" + }, + { + "id": "plugin_announcements", + "name": "Announcements", + "plugin_id": "announcements", + "plugin_name": "Announcement Plugin" + }, + { + "id": "section_plugins", + "name": "Plugins" + }, + { + "id": "plugin_action_command_prompt", + "name": "Action Command Prompt", + "plugin_id": "action_command_prompt", + "plugin_name": "Action Command Prompt Support" + }, + { + "id": "plugin_cura", + "name": "CuraEngine (<= 15.04)", + "plugin_id": "cura", + "plugin_name": "CuraEngine (<= 15.04)" + } + ], + "sidebar": [ + { + "id": "plugin_printer_safety_check", + "name": "Printer Safety Warning", + "plugin_id": "printer_safety_check", + "plugin_name": "Printer Safety Check" + }, + { + "id": "connection", + "name": "Connection" + }, + { + "id": "state", + "name": "State" + }, + { + "id": "files", + "name": "Files" + } + ], + "tab": [ + { + "id": "temperature", + "name": "Temperature" + }, + { + "id": "control", + "name": "Control" + }, + { + "id": "gcodeviewer", + "name": "GCode Viewer" + }, + { + "id": "terminal", + "name": "Terminal" + }, + { + "id": "timelapse", + "name": "Timelapse" + } + ], + "usersettings": [ + { + "id": "access", + "name": "Access" + }, + { + "id": "interface", + "name": "Interface" + } + ], + "wizard": [] + } + } + + Requires admin rights. + + .. warning:: + + This API endpoint is in beta. Things might change. If you happen to want to develop against it, you should drop + me an email to make sure I can give you a heads-up when something changes in an backwards incompatible way. + + :status 200: No error + :status 403: No admin rights + .. _sec-api-settings-datamodel: Data model diff --git a/src/octoprint/server/api/settings.py b/src/octoprint/server/api/settings.py index c23bd0fee..6cdcfe0be 100644 --- a/src/octoprint/server/api/settings.py +++ b/src/octoprint/server/api/settings.py @@ -14,7 +14,7 @@ from werkzeug.exceptions import BadRequest from octoprint.events import eventManager, Events from octoprint.settings import settings, valid_boolean_trues -from octoprint.server import admin_permission, printer +from octoprint.server import admin_permission, printer, pluginManager from octoprint.server.api import api, NO_CONTENT from octoprint.server.util.flask import restricted_access, with_revalidation_checking @@ -312,6 +312,38 @@ def deleteApiKey(): settings().deleteApiKey() return NO_CONTENT +@api.route("/settings/templates", methods=["GET"]) +@restricted_access +@admin_permission.require(403) +def fetchTemplateData(): + from octoprint.server.views import fetch_template_data + + refresh = request.values.get("refresh", "false") in valid_boolean_trues + templates, _, _ = fetch_template_data(refresh=refresh) + + result = dict() + for tt in templates: + result[tt] = [] + for key in templates[tt]["order"]: + entry = templates[tt]["entries"].get(key) + if not entry: + continue + + if isinstance(entry, dict): + name = key + else: + name, entry = entry + + data = dict(id=key, name=name) + + if entry and "_plugin" in entry: + plugin = pluginManager.get_plugin_info(entry["_plugin"], require_enabled=False) + data["plugin_id"] = plugin.key + data["plugin_name"] = plugin.name + + result[tt].append(data) + + return jsonify(order=result) def _saveSettings(data): logger = logging.getLogger(__name__) diff --git a/src/octoprint/server/views.py b/src/octoprint/server/views.py index 02c46dc99..a1fe15333 100644 --- a/src/octoprint/server/views.py +++ b/src/octoprint/server/views.py @@ -173,8 +173,7 @@ def index(): force_refresh = util.flask.cache_check_headers() or "_refresh" in request.values or wizard_active(_templates.get(locale)) # if we need to refresh our template cache or it's not yet set, process it - if force_refresh or _templates.get(locale) is None or _plugin_names is None or _plugin_vars is None: - _templates[locale], _plugin_names, _plugin_vars = _process_templates() + fetch_template_data(refresh=force_refresh) now = datetime.datetime.utcnow() @@ -441,7 +440,14 @@ def _get_render_kwargs(templates, plugin_names, plugin_vars, now): return render_kwargs -def _process_templates(): +def fetch_template_data(refresh=False): + global _templates, _plugin_names, _plugin_vars + + locale = g.locale.language if g.locale else "en" + + if not refresh and _templates.get(locale) is not None and _plugin_names is not None and _plugin_vars is not None: + return _templates[locale], _plugin_names, _plugin_vars + first_run = settings().getBoolean(["server", "firstRun"]) ##~~ prepare templates @@ -741,6 +747,10 @@ def _process_templates(): templates[t]["entries"].update(template_sorting[t]["custom_insert_entries"](sorted_missing)) templates[t]["order"] = template_sorting[t]["custom_insert_order"](templates[t]["order"], sorted_missing) + _templates[locale] = templates + _plugin_names = plugin_names + _plugin_vars = plugin_vars + return templates, plugin_names, plugin_vars @@ -828,6 +838,8 @@ def _process_template_config(name, implementation, rule, config=None, counter=1) if "suffix" in data: data["_key"] += data["suffix"] + data["_plugin"] = name + return data