diff --git a/config_readme.md b/config_readme.md index cc3ec98..817436e 100644 --- a/config_readme.md +++ b/config_readme.md @@ -75,62 +75,4 @@ permanent, submit a pull request for it to be added by default! > To add a data source to your app, add a data source config entry from one of the samples below **above** the application entry in config.ini, then add the following to your app config entry: -`data_source = variable_name` - -##### ping -Check if a service is online. -```ini -[variable_name] -platform = ping -resource = 192.168.1.1 -``` -> **Returns:** a right-aligned colored bullet point on the app card. - -| Variable | Required | Description | Options | -|-----------------|----------|-----------------------------------------------------------------|-------------------| -| [variable_name] | Yes | Name for the data source. | [variable_name] | -| plaform | Yes | Name of the platform. | rest | -| resource | Yes | Url of whatever you want to ping | url | - -##### rest -Make a call on a REST API and display the results as a jinja formatted string. -```ini -[variable_name] -platform = rest -resource = https://your-website.com/api -value_template = {{value}} -method = post -authentication = basic -username = my_username -password = my_password -payload = {"var1": "hi", "var2": 1} -``` -> **Returns:** `value_template` as rendered string - -| Variable | Required | Description | Options | -|-----------------|----------|-----------------------------------------------------------------|-------------------| -| [variable_name] | Yes | Name for the data source. | [variable_name] | -| plaform | Yes | Name of the platform. | rest | -| resource | Yes | Url of rest api resource. | url | -| value_template | Yes | Jinja template for how the returned data from api is displayed. | jinja template | -| method | No | Method for the api call, default is GET | GET,POST | -| authentication | No | Authentication for the api call, default is None | None,basic,digest | -| username | No | Username to use for auth. | string | -| password | No | Password to use for auth. | string | -| payload | No | Payload for post request. | json | - -> **Working example:** ->```ini ->[test] ->platform = rest ->resource = https://pokeapi.co/api/v2/pokemon ->value_template = Pokemon: {{value['count']}} -> ->[Pokemon] ->prefix = https:// ->url = pokemon.com ->icon = static/images/apps/default.png ->description = Data sources example ->open_in = this_tab ->data_sources = test ->``` \ No newline at end of file +`data_source = variable_name` \ No newline at end of file diff --git a/dashmachine/paths.py b/dashmachine/paths.py index d5e6bb0..800487e 100755 --- a/dashmachine/paths.py +++ b/dashmachine/paths.py @@ -13,6 +13,8 @@ root_folder = get_root_folder() dashmachine_folder = os.path.join(root_folder, "dashmachine") +platform_folder = os.path.join(dashmachine_folder, "platform") + user_data_folder = os.path.join(dashmachine_folder, "user_data") static_folder = os.path.join(dashmachine_folder, "static") diff --git a/dashmachine/platform/ping.py b/dashmachine/platform/ping.py index c17b2fb..f182e5c 100644 --- a/dashmachine/platform/ping.py +++ b/dashmachine/platform/ping.py @@ -1,3 +1,23 @@ +""" + +##### ping +Check if a service is online. +```ini +[variable_name] +platform = ping +resource = 192.168.1.1 +``` +> **Returns:** a right-aligned colored bullet point on the app card. + +| Variable | Required | Description | Options | +|-----------------|----------|-----------------------------------------------------------------|-------------------| +| [variable_name] | Yes | Name for the data source. | [variable_name] | +| plaform | Yes | Name of the platform. | rest | +| resource | Yes | Url of whatever you want to ping | url | + + +""" + import platform import subprocess diff --git a/dashmachine/platform/rest.py b/dashmachine/platform/rest.py index 61b5d61..ef8825c 100644 --- a/dashmachine/platform/rest.py +++ b/dashmachine/platform/rest.py @@ -1,3 +1,50 @@ +""" + +##### rest +Make a call on a REST API and display the results as a jinja formatted string. +```ini +[variable_name] +platform = rest +resource = https://your-website.com/api +value_template = {{value}} +method = post +authentication = basic +username = my_username +password = my_password +payload = {"var1": "hi", "var2": 1} +``` +> **Returns:** `value_template` as rendered string + +| Variable | Required | Description | Options | +|-----------------|----------|-----------------------------------------------------------------|-------------------| +| [variable_name] | Yes | Name for the data source. | [variable_name] | +| plaform | Yes | Name of the platform. | rest | +| resource | Yes | Url of rest api resource. | url | +| value_template | Yes | Jinja template for how the returned data from api is displayed. | jinja template | +| method | No | Method for the api call, default is GET | GET,POST | +| authentication | No | Authentication for the api call, default is None | None,basic,digest | +| username | No | Username to use for auth. | string | +| password | No | Password to use for auth. | string | +| payload | No | Payload for post request. | json | + +> **Working example:** +>```ini +>[test] +>platform = rest +>resource = https://pokeapi.co/api/v2/pokemon +>value_template = Pokemon: {{value['count']}} +> +>[Pokemon] +>prefix = https:// +>url = pokemon.com +>icon = static/images/apps/default.png +>description = Data sources example +>open_in = this_tab +>data_sources = test +>``` + +""" + import json from requests import get, post from requests.auth import HTTPBasicAuth, HTTPDigestAuth diff --git a/dashmachine/settings_system/routes.py b/dashmachine/settings_system/routes.py index 3c5dbd6..aa5b0ef 100644 --- a/dashmachine/settings_system/routes.py +++ b/dashmachine/settings_system/routes.py @@ -1,6 +1,5 @@ import os from shutil import move -from markdown2 import markdown_path from flask_login import current_user from flask import render_template, request, Blueprint, jsonify, redirect, url_for from dashmachine.user_system.forms import UserForm @@ -10,13 +9,12 @@ from dashmachine.main.utils import row2dict, public_route, check_groups from dashmachine.main.read_config import read_config from dashmachine.main.models import Files, TemplateApps from dashmachine.settings_system.forms import ConfigForm -from dashmachine.settings_system.utils import load_files_html +from dashmachine.settings_system.utils import load_files_html, get_config_html from dashmachine.settings_system.models import Settings from dashmachine.paths import ( backgrounds_images_folder, icons_images_folder, user_data_folder, - root_folder, ) from dashmachine.version import version @@ -41,16 +39,7 @@ def settings(): template_apps.append(f"{t_app.name}&&{t_app.icon}") users = User.query.all() - config_readme = markdown_path( - os.path.join(root_folder, "config_readme.md"), - extras=[ - "tables", - "fenced-code-blocks", - "break-on-newline", - "header-ids", - "code-friendly", - ], - ) + config_readme = get_config_html() return render_template( "settings_system/settings.html", config_form=config_form, @@ -123,4 +112,6 @@ def edit_user(): for err in errorMessages: err_str += f"{err} " return jsonify(data={"err": err_str}) - return jsonify(data={"err": "success"}) + users = User.query.all() + html = render_template("settings_system/user.html", users=users) + return jsonify(data={"err": "success", "html": html}) diff --git a/dashmachine/settings_system/utils.py b/dashmachine/settings_system/utils.py index 57fea69..49baded 100644 --- a/dashmachine/settings_system/utils.py +++ b/dashmachine/settings_system/utils.py @@ -1,11 +1,43 @@ -from dashmachine.paths import backgrounds_images_folder, icons_images_folder +import os +import importlib +from markdown2 import markdown +from dashmachine.paths import ( + backgrounds_images_folder, + icons_images_folder, + root_folder, + platform_folder, +) from flask import render_template -from os import listdir def load_files_html(): - backgrounds = listdir(backgrounds_images_folder) - icons = listdir(icons_images_folder) + backgrounds = os.listdir(backgrounds_images_folder) + icons = os.listdir(icons_images_folder) return render_template( "settings_system/files.html", backgrounds=backgrounds, icons=icons, ) + + +def get_config_html(): + with open(os.path.join(root_folder, "config_readme.md")) as readme_file: + md = readme_file.read() + platforms = os.listdir(platform_folder) + platforms = sorted(platforms) + for platform in platforms: + name, extension = os.path.splitext(platform) + if extension.lower() == ".py": + module = importlib.import_module(f"dashmachine.platform.{name}", ".") + if module.__doc__: + md += module.__doc__ + + config_html = markdown( + md, + extras=[ + "tables", + "fenced-code-blocks", + "break-on-newline", + "header-ids", + "code-friendly", + ], + ) + return config_html diff --git a/dashmachine/static/js/settings_system/settings.js b/dashmachine/static/js/settings_system/settings.js index 8157684..1204493 100644 --- a/dashmachine/static/js/settings_system/settings.js +++ b/dashmachine/static/js/settings_system/settings.js @@ -71,9 +71,10 @@ $( document ).ready(function() { if (data.data.err !== 'success'){ M.toast({html: data.data.err, classes: 'theme-failure'}); } else { - $("#user-form-password").val(''); - $("#user-form-confirm_password").val(''); - M.toast({html: 'User updated'}); + $("#users-div").empty(); + $("#users-div").append(data.data.html); + $("#edit-user-modal").modal('close'); + M.toast({html: 'User saved'}); } } }); diff --git a/dashmachine/templates/settings_system/user.html b/dashmachine/templates/settings_system/user.html index 561bd23..f4097a3 100644 --- a/dashmachine/templates/settings_system/user.html +++ b/dashmachine/templates/settings_system/user.html @@ -60,23 +60,11 @@ add - {% for user in users %} -
-
- - {{ user.username }} - {{ user.role }} - - - edit - close - -
-
- {% endfor %} + +
+ {{ Users(users) }} +
+ @@ -86,4 +74,25 @@

version: {{ version }}

{% endmacro %} -{{UserTab()}} \ No newline at end of file + +{% macro Users(users) %} + {% for user in users %} +
+
+ + {{ user.username }} + {{ user.role }} + + + edit + close + +
+
+ {% endfor %} +{% endmacro %} + +{{Users(users)}} \ No newline at end of file