From d2f43caaa5c42e666fe9326806582ae898ab9772 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=80=9CFuHsinyu=E2=80=9D?= Date: Mon, 8 Jul 2024 11:50:13 +0200 Subject: [PATCH] organize docstring and comments --- admin/admin.py | 29 ++++++++++-------- app.py | 47 +++++++++++++++-------------- general/templates/general/base.html | 2 +- 3 files changed, 42 insertions(+), 36 deletions(-) diff --git a/admin/admin.py b/admin/admin.py index d2491c058..d37a00462 100644 --- a/admin/admin.py +++ b/admin/admin.py @@ -3,15 +3,19 @@ __copyright__ = "Copyright (c) 2024, Utrecht University" __license__ = "GPLv3, see LICENSE" -from flask import abort, g, Blueprint, render_template, request, Response import json -from flask import flash, current_app as app -from werkzeug.exceptions import BadRequest -import api -from flask import redirect, url_for from os import path -from markupsafe import escape from functools import wraps + +from flask import ( + abort, g, Blueprint, render_template, request, Response, + flash, current_app as app, redirect, url_for +) +from werkzeug.exceptions import BadRequest +from markupsafe import escape + +import api + admin_bp = Blueprint("admin_bp", __name__, template_folder="templates/admin", static_folder="static/admin", @@ -19,7 +23,7 @@ @admin_bp.route("/") def index() -> Response: - """Route to the admin page, if user has admin access""" + """Route to the admin page, if user has admin access.""" has_admin_access = api.call("admin_has_access", data={})["data"] if has_admin_access: @@ -27,12 +31,11 @@ def index() -> Response: else: return abort(403) -# TODO: Code reability, simplify codes and update app.py for code snipts location (bottom?) # TODO: Automation Test # TODO: Write API and UI tests def admin_required(f): - '''Decorator for admin access check''' + """Decorator to check if the user has admin privileges.""" @wraps(f) def decorated_function(*args, **kwargs): print("admin access Setbanner:",g.admin) @@ -45,7 +48,7 @@ def decorated_function(*args, **kwargs): @admin_bp.route('/set_banner', methods=['POST']) @admin_required def set_banner(): - """Set up banner and save settings to web server's config files.""" + """set the banner message and persist it to configuration files""" # Get the message input banner_message = request.form.get('banner', '').strip() @@ -57,7 +60,7 @@ def set_banner(): flash(error_message, "danger") return redirect(url_for('admin_bp.index')) - # Update app config settings + # Update app config settings and save settings settings = { 'banner_enabled': True, 'banner_importance': 'importance' in request.form, @@ -93,10 +96,10 @@ def length_check(banner_message): def escape_html(text): """Escape HTML special characters in text.""" - return escape(text) # Assuming `escape` is from an imported module + return escape(text) def save_settings(settings, flash_msg): - """Save settings to the configuration file.""" + """Apply and save the given settings to the configuration file.""" config_file_path = path.join(app.config['APP_SHARED_FOLDER'], 'banner_settings.json') app.config.update(settings) try: diff --git a/app.py b/app.py index 01c196eb4..2474bb00e 100644 --- a/app.py +++ b/app.py @@ -44,6 +44,31 @@ ]) app.jinja_loader = theme_loader +# Load banner configurations +def load_banner_config(): + '''Load or initialize banner configurations.''' + config_file_path = path.join(app.config['APP_SHARED_FOLDER'], 'banner_settings.json') + default_config = {'banner_enabled': False} + + try: + if not path.exists(config_file_path): + return default_config + + with open(config_file_path, 'r') as file: + settings = json.load(file) + return { + 'banner_enabled': settings.get('banner_enabled', False), + 'banner_importance': settings.get('banner_importance', False), + 'banner_message': settings.get('banner_message', '') + } + except json.JSONDecodeError: + return default_config + except Exception as e: + return default_config + +app.config['APP_SHARED_FOLDER'] = '/tmp' +app.config.update(load_banner_config()) + # Setup values for the navigation bar used in # general/templates/general/base.html app.config['modules'] = [] @@ -75,31 +100,9 @@ {'name': 'Group Manager', 'function': 'group_manager_bp.index'}, ) -#TODO: improve the .py file for organized codes - -app.config['APP_SHARED_FOLDER'] = '/tmp' - -def load_banner_config(): - config_file_path = path.join(app.config['APP_SHARED_FOLDER'], 'banner_settings.json') - default_config = {'BANNER_ENABLED': False} - try: - if not path.exists(config_file_path): - return default_config - with open(config_file_path, 'r') as file: - settings = json.load(file) - return { - 'BANNER_ENABLED': settings.get('BANNER_ENABLED', False), - 'banner_importance': settings.get('banner_importance', False), - 'banner_message': settings.get('banner_message', '') - } - except json.JSONDecodeError: - return default_config - except Exception as e: - return default_config -app.config.update(load_banner_config()) app.config['modules_list'] = [module['name'] for module in app.config['modules']] diff --git a/general/templates/general/base.html b/general/templates/general/base.html index 79328293d..b3daa233e 100644 --- a/general/templates/general/base.html +++ b/general/templates/general/base.html @@ -44,7 +44,7 @@ {% if config.get('YODA_ENVIRONMENT') != "production" %}{% include 'environment.html' %}{% endif %} - {% if config.get('BANNER_ENABLED') %}{% include 'banner.html' %}{% endif %} + {% if config.get('banner_enabled') %}{% include 'banner.html' %}{% endif %}