Skip to content

Commit

Permalink
reorganize the codes
Browse files Browse the repository at this point in the history
  • Loading branch information
FuHsinyu committed Jul 8, 2024
1 parent 9ae1e6d commit 92881a4
Showing 1 changed file with 23 additions and 17 deletions.
40 changes: 23 additions & 17 deletions admin/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"""
Expand All @@ -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)
Expand All @@ -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'
Expand All @@ -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,
Expand All @@ -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')
Expand Down

0 comments on commit 92881a4

Please sign in to comment.