From 63f9f4b536dc00cc43a3e2e0e9c173c9abdc6f43 Mon Sep 17 00:00:00 2001 From: Ross Mountjoy Date: Thu, 6 Feb 2020 21:31:51 -0500 Subject: [PATCH] starting working on ui for multiple users --- dashmachine/main/routes.py | 4 + dashmachine/settings_system/routes.py | 13 ++- .../static/js/settings_system/settings.js | 15 +++- .../templates/settings_system/settings.html | 49 +---------- .../templates/settings_system/user.html | 88 +++++++++++++++++++ dashmachine/user_system/forms.py | 10 +-- migrations/versions/8f5a046465e8_.py | 40 +++++++++ 7 files changed, 166 insertions(+), 53 deletions(-) create mode 100644 dashmachine/templates/settings_system/user.html create mode 100644 migrations/versions/8f5a046465e8_.py diff --git a/dashmachine/main/routes.py b/dashmachine/main/routes.py index e92113e..811959a 100755 --- a/dashmachine/main/routes.py +++ b/dashmachine/main/routes.py @@ -64,8 +64,12 @@ def home(): return render_template("main/home.html") +@public_route @main.route("/app_view?", methods=["GET"]) def app_view(app_id): + settings = Settings.query.first() + if not check_groups(settings.home_access_groups, current_user): + return redirect(url_for("user_system.login")) app_db = Apps.query.filter_by(id=app_id).first() return render_template("main/app-view.html", url=f"{app_db.prefix}{app_db.url}") diff --git a/dashmachine/settings_system/routes.py b/dashmachine/settings_system/routes.py index 9000053..bc63333 100644 --- a/dashmachine/settings_system/routes.py +++ b/dashmachine/settings_system/routes.py @@ -4,6 +4,7 @@ from flask_login import current_user from flask import render_template, request, Blueprint, jsonify, redirect, url_for from dashmachine.user_system.forms import UserForm from dashmachine.user_system.utils import add_edit_user +from dashmachine.user_system.models import User 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 @@ -29,6 +30,7 @@ def settings(): config_form = ConfigForm() user_form = UserForm() + # user_form.role.choices = [(role, role) for role in settings_db.roles.split(",")] with open(os.path.join(user_data_folder, "config.ini"), "r") as config_file: config_form.config.data = config_file.read() files_html = load_files_html() @@ -36,6 +38,8 @@ def settings(): t_apps = TemplateApps.query.all() for t_app in t_apps: template_apps.append(f"{t_app.name}&&{t_app.icon}") + + users = User.query.all() return render_template( "settings_system/settings.html", config_form=config_form, @@ -43,6 +47,7 @@ def settings(): user_form=user_form, template_apps=",".join(template_apps), version=version, + users=users, ) @@ -93,7 +98,13 @@ def edit_user(): if form.validate_on_submit(): if form.password.data != form.confirm_password.data: return jsonify(data={"err": "Passwords don't match"}) - add_edit_user(form.username.data, form.password.data) + if not form.id.data: + new = True + else: + new = False + add_edit_user( + form.username.data, form.password.data, user_id=form.id.data, new=new + ) else: err_str = "" for fieldName, errorMessages in form.errors.items(): diff --git a/dashmachine/static/js/settings_system/settings.js b/dashmachine/static/js/settings_system/settings.js index 3ce6514..df6849b 100644 --- a/dashmachine/static/js/settings_system/settings.js +++ b/dashmachine/static/js/settings_system/settings.js @@ -4,6 +4,11 @@ d.className += " active theme-primary"; $( document ).ready(function() { initTCdrop('#images-tcdrop'); $("#config-wiki-modal").modal(); + $("#user-modal").modal({ + onCloseEnd: function () { + $("#edit-user-form").trigger('reset'); + } + }); $("#save-config-btn").on('click', function(e) { $.ajax({ @@ -58,7 +63,7 @@ $( document ).ready(function() { } }); - $("#edit-user-btn").on('click', function(e) { + $("#save-user-btn").on('click', function(e) { $.ajax({ url: $(this).attr('data-url'), type: 'POST', @@ -75,4 +80,12 @@ $( document ).ready(function() { }); }); + $(".edit-user-btn").on('click', function(e) { + $("#user-modal").modal('open'); + $("#user-form-username").val($(this).attr("data-username")); + $("#user-form-role").val($(this).attr("data-role")); + $("#user-form-id").val($(this).attr("data-id")); + M.updateTextFields(); + }); + }); \ No newline at end of file diff --git a/dashmachine/templates/settings_system/settings.html b/dashmachine/templates/settings_system/settings.html index 01e9fd4..b213979 100644 --- a/dashmachine/templates/settings_system/settings.html +++ b/dashmachine/templates/settings_system/settings.html @@ -1,7 +1,8 @@ {% extends "main/layout.html" %} -{% from 'global_macros.html' import input, button %} +{% from 'global_macros.html' import input, button, select %} {% from 'main/tcdrop.html' import tcdrop %} {% from 'settings_system/config-readme.html' import ConfigReadme %} +{% from 'settings_system/user.html' import UserTab with context %} {% block page_vendor_css %} {% endblock page_vendor_css %} @@ -133,51 +134,7 @@
-
-
User
- -
- {{ user_form.hidden_tag() }} - - {{ input( - label="Username", - id="user-form-username", - size="s12", - form_obj=user_form.username, - val=current_user.username - ) }} - - {{ input( - label="Password", - id="user-form-password", - form_obj=user_form.password, - size="s12" - ) }} - - {{ input( - label="Confirm Password", - id="user-form-confirm_password", - form_obj=user_form.confirm_password, - required='required', - size="s12" - ) }} -
- - {{ button( - icon="save", - float="left", - id="edit-user-btn", - data={'url': url_for('settings_system.edit_user')}, - text="save" - ) }} -
- -
-
DashMachine
-

version: {{ version }}

- -
- + {{ UserTab() }}
diff --git a/dashmachine/templates/settings_system/user.html b/dashmachine/templates/settings_system/user.html new file mode 100644 index 0000000..820070a --- /dev/null +++ b/dashmachine/templates/settings_system/user.html @@ -0,0 +1,88 @@ +{% macro UserTab() %} + + +
+
+
Users + + add + +
+ {% for user in users %} +
+
+ + {{ user.username }} + {{ user.role }} + + + edit + close + +
+
+ {% endfor %} +
+
+ + +
+
DashMachine
+

version: {{ version }}

+
+{% endmacro %} \ No newline at end of file diff --git a/dashmachine/user_system/forms.py b/dashmachine/user_system/forms.py index 7372c67..02e9fe1 100644 --- a/dashmachine/user_system/forms.py +++ b/dashmachine/user_system/forms.py @@ -1,9 +1,5 @@ from flask_wtf import FlaskForm -from wtforms import ( - StringField, - PasswordField, - BooleanField, -) +from wtforms import StringField, PasswordField, BooleanField, SelectField from wtforms.validators import DataRequired, Length @@ -12,6 +8,10 @@ class UserForm(FlaskForm): password = PasswordField(validators=[DataRequired(), Length(min=8, max=120)]) + # role = SelectField() + + id = StringField() + confirm_password = PasswordField() remember = BooleanField() diff --git a/migrations/versions/8f5a046465e8_.py b/migrations/versions/8f5a046465e8_.py new file mode 100644 index 0000000..42d6172 --- /dev/null +++ b/migrations/versions/8f5a046465e8_.py @@ -0,0 +1,40 @@ +"""empty message + +Revision ID: 8f5a046465e8 +Revises: 45ebff47af9f +Create Date: 2020-02-06 19:51:14.594434 + +""" +from alembic import op +import sqlalchemy as sa + + +# revision identifiers, used by Alembic. +revision = "8f5a046465e8" +down_revision = "45ebff47af9f" +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 ###