diff --git a/admin/admin.py b/admin/admin.py index a032e1d45..d2491c058 100644 --- a/admin/admin.py +++ b/admin/admin.py @@ -17,7 +17,6 @@ static_folder="static/admin", static_url_path="/assets") - @admin_bp.route("/") def index() -> Response: """Route to the admin page, if user has admin access""" @@ -31,6 +30,7 @@ def index() -> Response: # 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''' @wraps(f) @@ -42,36 +42,25 @@ def decorated_function(*args, **kwargs): return f(*args, **kwargs) return decorated_function -def validate_banner_message(banner_message): - """Validate the length and content of the banner message.""" - max_length = 256 - if not banner_message: - return "Empty banner message found.", False - elif len(banner_message) > max_length: - return "Banner message too long.", False - return None, True - -def escape_html(text): - """Escape HTML special characters in text.""" - return escape(text) # Assuming `escape` is from an imported module - @admin_bp.route('/set_banner', methods=['POST']) @admin_required def set_banner(): """Set up banner and save settings to web server's config files.""" + # Get the message input banner_message = request.form.get('banner', '').strip() banner_message = escape_html(banner_message) # Ensure safe text - error_message, is_valid = validate_banner_message(banner_message) + # Message length check + error_message, is_valid = length_check(banner_message) if not is_valid: flash(error_message, "danger") return redirect(url_for('admin_bp.index')) - is_important = 'importance' in request.form + # Update app config settings settings = { 'banner_enabled': True, - 'banner_importance': is_important, + 'banner_importance': 'importance' in request.form, 'banner_message': banner_message } flash_msg = 'Set banner message successfully' @@ -81,6 +70,8 @@ def set_banner(): @admin_required def remove_banner(): """Remove banner message and save settings to web server's config files.""" + + # Update app config settings settings = { 'banner_enabled': False, 'banner_importance': False, @@ -89,6 +80,21 @@ def remove_banner(): flash_msg = 'Banner removed successfully' return save_settings(settings, flash_msg) + + +def length_check(banner_message): + """Validate the length and content of the banner message.""" + max_length = 256 + if not banner_message: + return "Empty banner message found.", False + elif len(banner_message) > max_length: + return "Banner message too long.", False + return None, True + +def escape_html(text): + """Escape HTML special characters in text.""" + return escape(text) # Assuming `escape` is from an imported module + def save_settings(settings, flash_msg): """Save settings to the configuration file.""" config_file_path = path.join(app.config['APP_SHARED_FOLDER'], 'banner_settings.json')