diff --git a/dashmachine/main/utils.py b/dashmachine/main/utils.py index 5540c4a..3727022 100755 --- a/dashmachine/main/utils.py +++ b/dashmachine/main/utils.py @@ -127,9 +127,10 @@ def check_groups(groups, current_user): def get_data_source(data_source): - data_source_args = [] + data_source_args = {} for arg in data_source.args: - data_source_args.append(row2dict(arg)) + arg = row2dict(arg) + data_source_args[arg.get("key")] = arg.get("value") data_source = row2dict(data_source) module = importlib.import_module( f"dashmachine.platform.{data_source['platform']}", "." diff --git a/dashmachine/platform/ping.py b/dashmachine/platform/ping.py new file mode 100644 index 0000000..06dd47a --- /dev/null +++ b/dashmachine/platform/ping.py @@ -0,0 +1,23 @@ +from requests import get + + +class Platform: + def __init__(self, data_source, data_source_args): + # parse the user's options from the config entries + for key, value in data_source_args.items(): + self.__dict__[key] = value + + def process(self): + try: + value = get(self.resource) + except Exception: + icon_class = "theme-failure-text" + + if 599 >= value.status_code >= 400: + icon_class = "theme-failure-text" + if 399 >= value.status_code >= 300: + icon_class = "theme-warning-text" + if 299 >= value.status_code >= 100: + icon_class = "theme-success-text" + + return f"fiber_manual_record " diff --git a/dashmachine/platform/rest.py b/dashmachine/platform/rest.py index a1dc85d..455a863 100644 --- a/dashmachine/platform/rest.py +++ b/dashmachine/platform/rest.py @@ -1,41 +1,20 @@ -from requests import get +import json +from requests import get, post +from requests.auth import HTTPBasicAuth, HTTPDigestAuth from flask import render_template_string class Platform: def __init__(self, data_source, data_source_args): - self.data_source = data_source - self.name = data_source["name"] - # parse the user's options from the config entries - for source_arg in data_source_args: - if source_arg.get("key") == "resource": - self.resource = source_arg.get("value") + for key, value in data_source_args.items(): + self.__dict__[key] = value - if source_arg.get("key") == "method": - self.method = source_arg.get("value") - - if source_arg.get("key") == "payload": - self.payload = source_arg.get("value") - - if source_arg.get("key") == "authentication": - self.authentication = source_arg.get("value") - - if source_arg.get("key") == "username": - self.username = source_arg.get("value") - - if source_arg.get("key") == "password": - self.password = source_arg.get("value") - - if source_arg.get("key") == "value_template": - self.value_template = source_arg.get("value") - - if source_arg.get("key") == "data_template": - self.data_template = source_arg.get("value") - - # set defaults for omitted options - if not hasattr(self, "method"): - self.method = "GET" + # set defaults for omitted options + if not hasattr(self, "method"): + self.method = "GET" + if not hasattr(self, "authentication"): + self.authentication = None def process(self): if self.method.upper() == "GET": @@ -43,6 +22,17 @@ class Platform: value = get(self.resource).json() except Exception as e: value = f"{e}" + + elif self.method.upper() == "POST": + if self.authentication: + if self.authentication.lower() == "digest": + auth = HTTPDigestAuth(self.username, self.password) + else: + auth = HTTPBasicAuth(self.username, self.password) + else: + auth = None + + payload = json.loads(self.payload.replace("'", '"')) + value = post(self.resource, data=payload, auth=auth) value_template = render_template_string(self.value_template, value=value) - data_template = render_template_string(self.data_template, value=value_template) - return data_template + return value_template diff --git a/dashmachine/static/css/global/dashmachine-theme.css b/dashmachine/static/css/global/dashmachine-theme.css index 318e1d3..cd9f62f 100644 --- a/dashmachine/static/css/global/dashmachine-theme.css +++ b/dashmachine/static/css/global/dashmachine-theme.css @@ -11,7 +11,9 @@ --theme-color-font: #2c2f3a; --theme-color-font-muted: rgba(44, 47, 58, 0.85); --theme-color-font-muted2: rgba(44, 47, 58, 0.65); - --theme-warning: #f44336; + --theme-failure: #f44336; + --theme-warning: #ffae42; + --theme-success: #4BB543; --theme-on-primary: #fff; } [data-theme="dark"] { @@ -117,12 +119,24 @@ .theme-text { color: var(--theme-color-font) !important; } +.theme-failure { + background-color: var(--theme-failure) !important; +} +.theme-failure-text { + color: var(--theme-failure) !important; +} .theme-warning { background-color: var(--theme-warning) !important; } .theme-warning-text { color: var(--theme-warning) !important; } +.theme-success { + background-color: var(--theme-success) !important; +} +.theme-success-text { + color: var(--theme-success) !important; +} .theme-muted-text { color: var(--theme-color-font-muted) !important; } diff --git a/dashmachine/static/js/settings_system/settings.js b/dashmachine/static/js/settings_system/settings.js index df6849b..dcfc156 100644 --- a/dashmachine/static/js/settings_system/settings.js +++ b/dashmachine/static/js/settings_system/settings.js @@ -20,7 +20,7 @@ $( document ).ready(function() { M.toast({html: 'Config applied successfully'}); location.reload(true); } else { - M.toast({html: data.data.msg, classes: "theme-warning"}); + M.toast({html: data.data.msg, classes: "theme-failure"}); } } }); @@ -70,7 +70,7 @@ $( document ).ready(function() { data: $("#edit-user-form").serialize(), success: function(data){ if (data.data.err !== 'success'){ - M.toast({html: data.data.err, classes: 'theme-warning'}); + M.toast({html: data.data.err, classes: 'theme-failure'}); } else { $("#user-form-password").val(''); $("#user-form-confirm_password").val(''); diff --git a/dashmachine/templates/main/tcdrop.html b/dashmachine/templates/main/tcdrop.html index 857c364..5bd918a 100644 --- a/dashmachine/templates/main/tcdrop.html +++ b/dashmachine/templates/main/tcdrop.html @@ -16,8 +16,8 @@ {{ preload_circle() }}