From a071905cc6f344ccf9559190e84d95225b5d9a05 Mon Sep 17 00:00:00 2001 From: Microtechno9000 Date: Wed, 29 Nov 2023 15:32:31 +1030 Subject: [PATCH] session ui ajax push to update the session status --- arm/ui/sessions/forms.py | 14 ++++ arm/ui/sessions/routes.py | 60 ++++++++++++++-- arm/ui/sessions/templates/sessions.html | 68 +++++++++++++++---- ...essions_edit.html => sessions_status.html} | 27 +++++--- 4 files changed, 142 insertions(+), 27 deletions(-) create mode 100644 arm/ui/sessions/forms.py rename arm/ui/sessions/templates/{sessions_edit.html => sessions_status.html} (67%) diff --git a/arm/ui/sessions/forms.py b/arm/ui/sessions/forms.py new file mode 100644 index 000000000..10d015eac --- /dev/null +++ b/arm/ui/sessions/forms.py @@ -0,0 +1,14 @@ +""" +Forms used in sessions blueprint +""" +from flask_wtf import FlaskForm +from wtforms import StringField +from wtforms.validators import DataRequired + + +class SessionStatusForm(FlaskForm): + """ + Session Status update form + """ + id = StringField('id', validators=[DataRequired()]) + value = StringField('value') diff --git a/arm/ui/sessions/routes.py b/arm/ui/sessions/routes.py index e14b20f0e..8783ba516 100644 --- a/arm/ui/sessions/routes.py +++ b/arm/ui/sessions/routes.py @@ -2,16 +2,20 @@ ARM route blueprint for sessions pages Covers - sessions [GET] +- sessions/edit [GET] +- sessions/update [POST] """ from flask_login import login_required -from flask import render_template +from flask import render_template, request +from arm.ui import app, db from arm.ui.sessions import route_sessions from arm.models.sessions import Sessions from arm.models.session_settings import SessionSettings from arm.models.session_types import SessionTypes from arm.models.system_drives import SystemDrives +from .forms import SessionStatusForm """ @@ -141,20 +145,64 @@ def sessions(): # Return the current list of system drives available to ARM db_system_drives = SystemDrives.query.all() + form = SessionStatusForm() + return render_template('sessions.html', db_session_types=db_session_types, db_sessions=db_sessions, - db_system_drives=db_system_drives) + db_system_drives=db_system_drives, + form=form) + -@route_sessions.route('/sessions/edit/') +@route_sessions.route('/sessions/edit/') @login_required -def session_edit(id): +def session_edit(session_id): """ Page - Sessions Method - GET Overview - Session editor """ - db_session = Sessions.query.where(id=id).first() + db_session = Sessions.query.where(id=session_id).first() - return render_template('sessions_edit.html', + return render_template('sessions_status.html', db_session=db_session) + + +@route_sessions.route('/sessions/update', methods=['POST']) +@login_required +def session_update_status(): + """ + Page - Session Update + Method - POST + Overview - Take user updates from Session Edit tab for active and drive changes + """ + form = SessionStatusForm() + status = False + response = "data value passed not valid" + + if form.validate(): + data = request.get_json() + app.logger.debug(f"Session Status update: {data}") + + # Get the row ID from json, load value from database + [name, row_id] = data['id'].split('_') + db_session = Sessions.query.filter_by(id=int(row_id)).first() + app.logger.debug(f"Session name: {name} row: {row_id} value: {data['value']}") + + if name == 'valid': + db_session.valid = data['value'] + response = f"Updated Session {row_id} status" + status = True + app.logger.debug(f"Session db update - id: {db_session.valid}") + db.session.commit() + + elif name == 'drive': + db_session.drive_id = int(data['value']) + response = f"Updated Session {row_id} drive" + status = True + app.logger.debug(f"Session db update - drive_id: {db_session.drive_id}") + db.session.commit() + + app.logger.info("Updated session information") + + return {'success': status, 'response': response} diff --git a/arm/ui/sessions/templates/sessions.html b/arm/ui/sessions/templates/sessions.html index 5338d9c24..b15502b93 100644 --- a/arm/ui/sessions/templates/sessions.html +++ b/arm/ui/sessions/templates/sessions.html @@ -19,8 +19,8 @@

ARM - Sessions

aria-controls="session-drives-tab" aria-selected="true">Current Sessions