Skip to content

Commit

Permalink
organize docstring and comments
Browse files Browse the repository at this point in the history
  • Loading branch information
FuHsinyu committed Jul 8, 2024
1 parent 92881a4 commit d2f43ca
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 36 deletions.
29 changes: 16 additions & 13 deletions admin/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,36 +3,39 @@
__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",
static_url_path="/assets")

@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:
return render_template("admin.html")
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)
Expand All @@ -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()
Expand All @@ -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,
Expand Down Expand Up @@ -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:
Expand Down
47 changes: 25 additions & 22 deletions app.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'] = []
Expand Down Expand Up @@ -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']]
Expand Down
2 changes: 1 addition & 1 deletion general/templates/general/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@

<body class="d-flex flex-column min-vh-100">
{% 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 %}
<header class="py-3">
<div class="container">
<div class="row">
Expand Down

0 comments on commit d2f43ca

Please sign in to comment.