Skip to content

Commit 1122d0e

Browse files
[FEATURE] Addition of Update CPU Info to Settings SysInfo Page (automatic-ripping-machine#1330)
* feature add for automatic-ripping-machine#1309 addition of cpu information update to settings * ARM Wiki update with System Info * PyLint fixes removing global variables replaced global url values with flask url_for reference calls * Version bump - FEATURE * Logging error status fix
1 parent 27f9649 commit 1122d0e

20 files changed

+238
-71
lines changed

VERSION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
2.12.2
1+
2.13.0

arm/ui/auth/auth.py

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
"""
22
ARM route blueprint for auth pages
33
Covers
4-
- user_loader [..]
4+
- user_loader [GET, POST]
55
- unauthorized_handler [GET]
66
- login [GET, POST]
77
- logout [GET]
88
- update_password [GET, POST]
99
"""
10-
10+
from sqlite3 import OperationalError
1111
import bcrypt
1212
from flask import redirect, render_template, request, Blueprint, flash, app, session
1313
from flask_login import LoginManager, login_required, \
@@ -26,18 +26,14 @@
2626
login_manager = LoginManager()
2727
login_manager.init_app(app)
2828

29-
# Page definitions
30-
page_support_databaseupdate = "support/databaseupdate.html"
31-
redirect_settings = "/settings"
32-
3329

3430
@route_auth.route('/login', methods=['GET', 'POST'])
3531
def login():
3632
"""
3733
Login page if login is enabled
3834
:return: redirect
3935
"""
40-
global page_support_databaseupdate
36+
page_support_databaseupdate = "support/databaseupdate.html"
4137

4238
# Check the database is current
4339
db_update = ui_utils.arm_db_check()
@@ -51,15 +47,17 @@ def login():
5147
user_list = User.query.all()
5248
# If we don't raise an exception but the usr table is empty
5349
if not user_list:
54-
app.logger.debug("No admin found")
55-
except Exception:
50+
app.logger.error("No admin found")
51+
except OperationalError as e:
52+
# Handle no data found when querying the db
5653
flash(constants.NO_ADMIN_ACCOUNT, "danger")
57-
app.logger.debug(constants.NO_ADMIN_ACCOUNT)
54+
app.logger.error(constants.NO_ADMIN_ACCOUNT)
55+
app.logger.error(f"ERROR: Missing Data in the ARM User Table: {e}")
5856
dbform = DBUpdate(request.form)
5957
db_update = ui_utils.arm_db_check()
6058
return render_template(page_support_databaseupdate, db_update=db_update, dbform=dbform)
6159

62-
# if user is logged in
60+
# if a user is logged in
6361
if current_user.is_authenticated:
6462
return_redirect = redirect(constants.HOME_PAGE)
6563

@@ -81,7 +79,7 @@ def login():
8179
else:
8280
flash("Something isn't right", "danger")
8381

84-
# If nothing has gone wrong give them the login page
82+
# If nothing has gone wrong, give them the login page
8583
if request.method == 'GET' or return_redirect is None:
8684
return_redirect = render_template('login.html', form=form)
8785

@@ -103,7 +101,7 @@ def logout():
103101
@login_required
104102
def update_password():
105103
"""
106-
updating password for the admin account
104+
updating the password for the admin account
107105
"""
108106
# get current user
109107
user = User.query.first()
@@ -131,10 +129,10 @@ def update_password():
131129
return redirect("logout")
132130
except Exception as error:
133131
flash(str(error), "danger")
134-
app.logger.debug(f"Error in updating password: {error}")
132+
app.logger.error(f"Error in updating password: {error}")
135133
else:
136134
flash("Password couldn't be updated. Problem with old password", "danger")
137-
app.logger.info("Password not updated, issue with old password")
135+
app.logger.error("Password not updated, issue with old password")
138136

139137
return render_template('update_password.html', user=user.email, form=form)
140138

@@ -148,15 +146,16 @@ def load_user(user_id):
148146
"""
149147
try:
150148
return User.query.get(int(user_id))
151-
except Exception:
152-
app.logger.debug("Error getting user")
149+
except OperationalError as e:
150+
app.logger.error("Error getting user")
151+
app.logger.error(f"ERROR: {e}")
153152
return None
154153

155154

156155
@login_manager.unauthorized_handler
157156
def unauthorized():
158157
"""
159-
User isn't authorised to view the page
160-
:return: Page redirect
158+
User isn't authorised to view the requested page
159+
:return: redirect to login page
161160
"""
162161
return redirect('/login')

arm/ui/routes.py

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
Other routes handled in flask blueprints
1010
- auth, database, history, jobs, logs, sendmovies, settings
1111
"""
12-
1312
import os
1413
import json
1514
from pathlib import Path, PurePath
@@ -19,6 +18,7 @@
1918
from flask.logging import default_handler # noqa: F401
2019
from flask_login import LoginManager, login_required, \
2120
current_user, login_user, logout_user # noqa: F401
21+
from sqlalchemy.exc import SQLAlchemyError
2222

2323
import arm.ui.utils as ui_utils
2424
from arm.ui import app, db, constants
@@ -33,10 +33,6 @@
3333
# This attaches the armui_cfg globally to let the users use any bootswatch skin from cdn
3434
armui_cfg = ui_utils.arm_db_cfg()
3535

36-
# Page definitions
37-
page_support_databaseupdate = "support/databaseupdate.html"
38-
redirect_settings = "/settings"
39-
4036
# Define the Flask login manager
4137
login_manager = LoginManager()
4238
login_manager.init_app(app)
@@ -49,22 +45,20 @@ def home():
4945
"""
5046
The main homepage showing current rips and server stats
5147
"""
52-
global page_support_databaseupdate
53-
5448
# Check the database is current
5549
db_update = ui_utils.arm_db_check()
56-
# Push out HW transcode status for homepage
50+
# Push out HW transcode status for the homepage
5751
stats = {'hw_support': check_hw_transcode_support()}
5852
if not db_update["db_current"] or not db_update["db_exists"]:
5953
dbform = DBUpdate(request.form)
6054
app.logger.debug(f"Error with ARM DB: [{db_update['db_current']}]-[{db_update['db_exists']}]")
61-
return render_template(page_support_databaseupdate, db_update=db_update, dbform=dbform)
55+
return render_template("support/databaseupdate.html",
56+
db_update=db_update,
57+
dbform=dbform)
6258

63-
# System details in class server
59+
# Get system details from Server Info and Config
6460
server = SystemInfo.query.filter_by(id="1").first()
6561
serverutil = ServerUtil()
66-
67-
# System details in class server
6862
arm_path = cfg.arm_config['TRANSCODE_PATH']
6963
media_path = cfg.arm_config['COMPLETED_PATH']
7064

@@ -75,8 +69,9 @@ def home():
7569
if os.path.isfile(cfg.arm_config['DBFILE']):
7670
try:
7771
jobs = db.session.query(Job).filter(Job.status.notin_(['fail', 'success'])).all()
78-
except Exception:
72+
except SQLAlchemyError as e:
7973
# db isn't setup
74+
app.logger.error(f"Error getting jobs from DB: {e}")
8075
return redirect(url_for('setup'))
8176
else:
8277
jobs = {}
@@ -176,7 +171,7 @@ def setup():
176171
return redirect("/error")
177172
except Exception as error:
178173
flash(str(error))
179-
app.logger.debug("Setup - " + str(error))
174+
app.logger.error("Setup - " + str(error))
180175
return redirect(constants.HOME_PAGE)
181176

182177

@@ -189,8 +184,9 @@ def load_user(user_id):
189184
"""
190185
try:
191186
return User.query.get(int(user_id))
192-
except Exception:
193-
app.logger.debug("Error getting user")
187+
except SQLAlchemyError as e:
188+
app.logger.error("Error getting user")
189+
app.logger.error(f"ERROR: {e}")
194190
return None
195191

196192

arm/ui/settings/settings.py

Lines changed: 47 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
- drive_eject [GET]
1313
- drive_remove [GET]
1414
- testapprise [GET]
15+
- updateCPU [GET]
1516
"""
1617
import platform
1718
import importlib
@@ -22,7 +23,7 @@
2223
from flask_login import login_required, \
2324
current_user, login_user, UserMixin, logout_user # noqa: F401
2425
from flask import render_template, request, flash, \
25-
redirect, Blueprint, session
26+
redirect, Blueprint, session, url_for
2627

2728
import arm.ui.utils as ui_utils
2829
from arm.ui import app, db
@@ -40,10 +41,6 @@
4041
template_folder='templates',
4142
static_folder='../static')
4243

43-
# Page definitions
44-
page_settings = "settings/settings.html"
45-
redirect_settings = "/settings"
46-
4744

4845
@route_settings.route('/settings')
4946
@login_required
@@ -54,7 +51,6 @@ def settings():
5451
Overview - allows the user to update the all configs of A.R.M without
5552
needing to open a text editor
5653
"""
57-
global page_settings
5854

5955
# stats for info page
6056
failed_rips = Job.query.filter_by(status="fail").count()
@@ -88,11 +84,9 @@ def settings():
8884
# ARM UI config
8985
armui_cfg = ui_utils.arm_db_cfg()
9086

91-
# System details in class server
87+
# Get system details from Server Info and Config
9288
server = SystemInfo.query.filter_by(id="1").first()
9389
serverutil = ServerUtil()
94-
95-
# System details in class server
9690
arm_path = cfg.arm_config['TRANSCODE_PATH']
9791
media_path = cfg.arm_config['COMPLETED_PATH']
9892

@@ -106,7 +100,7 @@ def settings():
106100

107101
session["page_title"] = "Settings"
108102

109-
return render_template(page_settings,
103+
return render_template("settings/settings.html",
110104
settings=cfg.arm_config,
111105
ui_settings=armui_cfg,
112106
stats=stats,
@@ -222,7 +216,7 @@ def save_abcde():
222216
"""
223217
Page - save_abcde_settings
224218
Method - POST
225-
Overview - Save 'abcde Config' page settings to database. Not a user page
219+
Overview - Save 'abcde Config' page settings to the database. Not a user page
226220
"""
227221
success = False
228222
abcde_cfg_str = ""
@@ -241,7 +235,9 @@ def save_abcde():
241235
cfg.abcde_config = clean_abcde_str
242236

243237
# If we get to here, there was no post-data
244-
return {'success': success, 'settings': clean_abcde_str, 'form': 'abcde config'}
238+
return {'success': success,
239+
'settings': clean_abcde_str,
240+
'form': 'abcde config'}
245241

246242

247243
@route_settings.route('/save_apprise_cfg', methods=['POST'])
@@ -275,8 +271,6 @@ def server_info():
275271
Method - POST
276272
Overview - Save 'System Info' page settings to database. Not a user page
277273
"""
278-
global redirect_settings
279-
280274
# System Drives (CD/DVD/Blueray drives)
281275
form_drive = SystemInfoDrives(request.form)
282276
if request.method == 'POST' and form_drive.validate():
@@ -291,12 +285,12 @@ def server_info():
291285
drive.drive_mode = str(form_drive.drive_mode.data).strip()
292286
db.session.commit()
293287
flash(f"Updated Drive {drive.mount} details", "success")
294-
# Return to systeminfo page (refresh page)
295-
return redirect(redirect_settings)
288+
# Return to the systeminfo page (refresh page)
289+
return redirect(url_for('route_settings.settings'))
296290
else:
297291
flash("Error: Unable to update drive details", "error")
298292
# Return for GET
299-
return redirect(redirect_settings)
293+
return redirect(url_for('route_settings.settings'))
300294

301295

302296
@route_settings.route('/systemdrivescan')
@@ -306,24 +300,22 @@ def system_drive_scan():
306300
Method - GET
307301
Overview - Scan for the system drives and update the database.
308302
"""
309-
global redirect_settings
310303
# Update to scan for changes to the ripper system
311304
new_count = DriveUtils.drives_update()
312305
flash(f"ARM found {new_count} new drives", "success")
313-
return redirect(redirect_settings)
306+
return redirect(url_for('route_settings.settings'))
314307

315308

316309
@route_settings.route('/drive/eject/<eject_id>')
317310
@login_required
318311
def drive_eject(eject_id):
319312
"""
320-
Server System - change state of CD/DVD/BluRay drive - toggle eject
313+
Server System - change state of CD/DVD/BluRay drive - toggle eject status
321314
"""
322-
global redirect_settings
323315
drive = SystemDrives.query.filter_by(drive_id=eject_id).first()
324316
drive.open_close()
325317
db.session.commit()
326-
return redirect(redirect_settings)
318+
return redirect(url_for('route_settings.settings'))
327319

328320

329321
@route_settings.route('/drive/remove/<remove_id>')
@@ -332,7 +324,6 @@ def drive_remove(remove_id):
332324
"""
333325
Server System - remove a drive from the ARM UI
334326
"""
335-
global redirect_settings
336327
try:
337328
app.logger.debug(f"Removing drive {remove_id}")
338329
drive = SystemDrives.query.filter_by(drive_id=remove_id).first()
@@ -343,7 +334,7 @@ def drive_remove(remove_id):
343334
except Exception as e:
344335
app.logger.error(f"Drive removal encountered an error: {e}")
345336
flash("Drive unable to be removed, check logs for error", "error")
346-
return redirect(redirect_settings)
337+
return redirect(url_for('route_settings.settings'))
347338

348339

349340
@route_settings.route('/drive/manual/<manual_id>')
@@ -390,11 +381,41 @@ def testapprise():
390381
Method - GET
391382
Overview - Send a test notification to Apprise.
392383
"""
393-
global redirect_settings
394384
# Send a sample notification
395385
message = "This is a notification by the ARM-Notification Test!"
396386
if cfg.arm_config["UI_BASE_URL"] and cfg.arm_config["WEBSERVER_PORT"]:
397387
message = message + f" Server URL: http://{cfg.arm_config['UI_BASE_URL']}:{cfg.arm_config['WEBSERVER_PORT']}"
398388
ripper_utils.notify(None, "ARM notification", message)
399389
flash("Test notification sent ", "success")
400-
return redirect(redirect_settings)
390+
return redirect(url_for('route_settings.settings'))
391+
392+
393+
@route_settings.route('/updatecpu')
394+
def update_cpu():
395+
"""
396+
Update system CPU information
397+
"""
398+
# Get current system information from database
399+
current_system = SystemInfo.query.first()
400+
# Query system for new information
401+
new_system = SystemInfo()
402+
403+
app.logger.debug("****** System Information ******")
404+
if current_system is not None:
405+
app.logger.debug(f"Name old [{current_system.name}] new [{new_system.name}]")
406+
app.logger.debug(f"Name old [{current_system.cpu}] new [{new_system.cpu}]")
407+
current_system.name = new_system.name
408+
current_system.cpu = new_system.cpu
409+
db.session.add(current_system)
410+
411+
else:
412+
app.logger.debug(f"Name old [] new [{new_system.name}]")
413+
app.logger.debug(f"Name old [] new [{new_system.cpu}]")
414+
db.session.add(new_system)
415+
416+
app.logger.debug("****** End System Information ******")
417+
app.logger.info(f"Updated CPU Details with new info - {new_system.name} - {new_system.cpu}")
418+
419+
db.session.commit()
420+
421+
return redirect(url_for('route_settings.settings'))

0 commit comments

Comments
 (0)