From 71b5f17f83b7f4f4eebfe59b883d3799fbaf207d Mon Sep 17 00:00:00 2001 From: Ross Mountjoy Date: Thu, 6 Feb 2020 12:00:03 -0500 Subject: [PATCH] finished the core of platform/plugin system --- dashmachine/main/models.py | 12 --------- dashmachine/main/read_config.py | 3 +-- dashmachine/main/routes.py | 7 ----- dashmachine/main/utils.py | 22 +-------------- dashmachine/platform/rest.py | 21 ++++++++------- dashmachine/static/js/main/home.js | 15 +---------- dashmachine/templates/main/home.html | 2 +- migrations/versions/45ebff47af9f_.py | 40 ++++++++++++++++++++++++++++ 8 files changed, 55 insertions(+), 67 deletions(-) create mode 100644 migrations/versions/45ebff47af9f_.py diff --git a/dashmachine/main/models.py b/dashmachine/main/models.py index 276a920..7ff36a3 100644 --- a/dashmachine/main/models.py +++ b/dashmachine/main/models.py @@ -40,18 +40,6 @@ class TemplateApps(db.Model): open_in = db.Column(db.String()) -class ApiCalls(db.Model): - id = db.Column(db.Integer, primary_key=True) - name = db.Column(db.String()) - resource = db.Column(db.String()) - method = db.Column(db.String()) - payload = db.Column(db.String()) - authentication = db.Column(db.String()) - username = db.Column(db.String()) - password = db.Column(db.String()) - value_template = db.Column(db.String()) - - class DataSources(db.Model): id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String()) diff --git a/dashmachine/main/read_config.py b/dashmachine/main/read_config.py index 26e652f..0549d86 100644 --- a/dashmachine/main/read_config.py +++ b/dashmachine/main/read_config.py @@ -1,6 +1,6 @@ import os from configparser import ConfigParser -from dashmachine.main.models import Apps, ApiCalls, Groups, DataSources, DataSourcesArgs +from dashmachine.main.models import Apps, Groups, DataSources, DataSourcesArgs from dashmachine.settings_system.models import Settings from dashmachine.paths import user_data_folder from dashmachine import db @@ -27,7 +27,6 @@ def read_config(): DataSources.query.delete() DataSourcesArgs.query.delete() Apps.query.delete() - ApiCalls.query.delete() Settings.query.delete() Groups.query.delete() diff --git a/dashmachine/main/routes.py b/dashmachine/main/routes.py index 546191a..e92113e 100755 --- a/dashmachine/main/routes.py +++ b/dashmachine/main/routes.py @@ -6,7 +6,6 @@ from flask import render_template, url_for, redirect, request, Blueprint, jsonif from flask_login import current_user from dashmachine.main.models import Files, Apps, DataSources from dashmachine.main.utils import ( - get_rest_data, public_route, check_groups, get_data_source, @@ -71,12 +70,6 @@ def app_view(app_id): return render_template("main/app-view.html", url=f"{app_db.prefix}{app_db.url}") -@main.route("/load_rest_data", methods=["GET"]) -def load_rest_data(): - data_template = get_rest_data(request.args.get("template")) - return data_template - - @main.route("/load_data_source", methods=["GET"]) def load_data_source(): data_source = DataSources.query.filter_by(id=request.args.get("id")).first() diff --git a/dashmachine/main/utils.py b/dashmachine/main/utils.py index 71a7673..5540c4a 100755 --- a/dashmachine/main/utils.py +++ b/dashmachine/main/utils.py @@ -5,7 +5,7 @@ from shutil import copyfile from requests import get from configparser import ConfigParser from dashmachine.paths import dashmachine_folder, images_folder, root_folder -from dashmachine.main.models import ApiCalls, TemplateApps, Groups +from dashmachine.main.models import TemplateApps, Groups from dashmachine.main.read_config import read_config from dashmachine.settings_system.models import Settings from dashmachine.user_system.models import User @@ -104,26 +104,6 @@ def dashmachine_init(): user.role = "admin" -def get_rest_data(template): - while template and template.find("{{") > -1: - start_braces = template.find("{{") + 2 - end_braces = template.find("}}") - key = template[start_braces:end_braces].strip() - key_w_braces = template[start_braces - 2 : end_braces + 2] - value = do_api_call(key) - template = template.replace(key_w_braces, value) - return template - - -def do_api_call(key): - api_call = ApiCalls.query.filter_by(name=key).first() - if api_call.method.upper() == "GET": - value = get(api_call.resource) - exec(f"{key} = {value.json()}") - value = str(eval(api_call.value_template)) - return value - - def check_groups(groups, current_user): if current_user.is_anonymous: current_user.role = "public_user" diff --git a/dashmachine/platform/rest.py b/dashmachine/platform/rest.py index c928fa7..a1dc85d 100644 --- a/dashmachine/platform/rest.py +++ b/dashmachine/platform/rest.py @@ -1,4 +1,5 @@ from requests import get +from flask import render_template_string class Platform: @@ -13,8 +14,6 @@ class Platform: if source_arg.get("key") == "method": self.method = source_arg.get("value") - else: - self.method = "GET" if source_arg.get("key") == "payload": self.payload = source_arg.get("value") @@ -30,18 +29,20 @@ class Platform: if source_arg.get("key") == "value_template": self.value_template = source_arg.get("value") - else: - self.value_template = "value" if source_arg.get("key") == "data_template": self.data_template = source_arg.get("value") - else: - self.value_template = self.name + + # set defaults for omitted options + if not hasattr(self, "method"): + self.method = "GET" def process(self): if self.method.upper() == "GET": try: - value = get(self.resource) - except: - pass - return self.name + value = get(self.resource).json() + except Exception as e: + value = f"{e}" + value_template = render_template_string(self.value_template, value=value) + data_template = render_template_string(self.data_template, value=value_template) + return data_template diff --git a/dashmachine/static/js/main/home.js b/dashmachine/static/js/main/home.js index 47f6603..39dc0b9 100644 --- a/dashmachine/static/js/main/home.js +++ b/dashmachine/static/js/main/home.js @@ -23,20 +23,7 @@ $( document ).ready(function() { data: {id: el.attr('data-id')}, success: function(data){ el.closest('.col').find('.data-source-loading').addClass('hide'); - el.text(data); - } - }); - }); - - $(".data-template").each(function(e) { - var el = $(this); - $.ajax({ - url: el.attr('data-url'), - type: 'GET', - data: {template: el.text()}, - success: function(data){ - el.text(data); - el.removeClass('hide'); + el.html(data); } }); }); diff --git a/dashmachine/templates/main/home.html b/dashmachine/templates/main/home.html index d36cdd2..ed3ab05 100755 --- a/dashmachine/templates/main/home.html +++ b/dashmachine/templates/main/home.html @@ -91,4 +91,4 @@ {% block page_lvl_js %} {{ process_js_sources(src="main/home.js")|safe }} -{% endblock page_lvl_js %} +{% endblock page_lvl_js %} \ No newline at end of file diff --git a/migrations/versions/45ebff47af9f_.py b/migrations/versions/45ebff47af9f_.py new file mode 100644 index 0000000..bc1e504 --- /dev/null +++ b/migrations/versions/45ebff47af9f_.py @@ -0,0 +1,40 @@ +"""empty message + +Revision ID: 45ebff47af9f +Revises: 6bd40f00f2eb +Create Date: 2020-02-06 11:48:22.563926 + +""" +from alembic import op +import sqlalchemy as sa + + +# revision identifiers, used by Alembic. +revision = "45ebff47af9f" +down_revision = "6bd40f00f2eb" +branch_labels = None +depends_on = None + + +def upgrade(): + # ### commands auto generated by Alembic - please adjust! ### + op.drop_table("api_calls") + # ### end Alembic commands ### + + +def downgrade(): + # ### commands auto generated by Alembic - please adjust! ### + op.create_table( + "api_calls", + sa.Column("id", sa.INTEGER(), nullable=False), + sa.Column("name", sa.VARCHAR(), nullable=True), + sa.Column("resource", sa.VARCHAR(), nullable=True), + sa.Column("method", sa.VARCHAR(), nullable=True), + sa.Column("payload", sa.VARCHAR(), nullable=True), + sa.Column("authentication", sa.VARCHAR(), nullable=True), + sa.Column("username", sa.VARCHAR(), nullable=True), + sa.Column("password", sa.VARCHAR(), nullable=True), + sa.Column("value_template", sa.VARCHAR(), nullable=True), + sa.PrimaryKeyConstraint("id"), + ) + # ### end Alembic commands ###