Merge pull request #165 from ZeusWPI/optimisations
Add a few optimisations to haldis
This commit is contained in:
commit
33203af6cc
2 changed files with 41 additions and 59 deletions
|
@ -2,6 +2,8 @@
|
||||||
import os
|
import os
|
||||||
from datetime import datetime, timedelta
|
from datetime import datetime, timedelta
|
||||||
|
|
||||||
|
import yaml
|
||||||
|
|
||||||
from flask import Flask, render_template, make_response
|
from flask import Flask, render_template, make_response
|
||||||
from flask import request
|
from flask import request
|
||||||
from flask import Blueprint, abort
|
from flask import Blueprint, abort
|
||||||
|
@ -9,8 +11,6 @@ from flask import current_app as app
|
||||||
from flask import send_from_directory, url_for
|
from flask import send_from_directory, url_for
|
||||||
from flask_login import login_required
|
from flask_login import login_required
|
||||||
|
|
||||||
import yaml
|
|
||||||
|
|
||||||
from models import Location, Order
|
from models import Location, Order
|
||||||
# import views
|
# import views
|
||||||
from views.order import get_orders
|
from views.order import get_orders
|
||||||
|
@ -33,70 +33,52 @@ def home() -> str:
|
||||||
@general_bp.route("/css")
|
@general_bp.route("/css")
|
||||||
def css():
|
def css():
|
||||||
"Generate the css"
|
"Generate the css"
|
||||||
if (request.cookies.get('performance') and request.cookies.get('performance') == 'highPerformance'):
|
if request.cookies.get('performance', '') == 'highPerformance':
|
||||||
cssPath = 'static/css/themes/highPerformance/'
|
cssPath = 'static/css/themes/highPerformance/'
|
||||||
else:
|
else:
|
||||||
cssPath = 'static/css/themes/lowPerformance/'
|
cssPath = 'static/css/themes/lowPerformance/'
|
||||||
|
|
||||||
if request.cookies.get('theme'):
|
cookie_theme = request.cookies.get('theme', '')
|
||||||
if request.cookies['theme'] == 'customTheme':
|
if cookie_theme == 'customTheme':
|
||||||
#TODO: The custom theme is hardcoded :(. Make the server auto select a custom team.
|
# Here seasonal themes will be returned; matching the current date.
|
||||||
# Here seasonal themes will be returned; matching the current date.
|
|
||||||
|
|
||||||
# Open the YAML file with all the themes.
|
# Open the YAML file with all the themes.
|
||||||
path = os.path.join(str(app.root_path), "views/themes.yml")
|
path = os.path.join(app.root_path, "views/themes.yml")
|
||||||
with open(path, 'r') as stream:
|
with open(path, 'r') as stream:
|
||||||
data = yaml.safe_load(stream)
|
data = yaml.safe_load(stream)
|
||||||
# Build a dictionary from the YAML file with all the themes and there attributes.
|
# Build a dictionary from the YAML file with all the themes and there attributes.
|
||||||
themes = {}
|
themes = {}
|
||||||
for item in data:
|
for item in data:
|
||||||
key = list(item.keys())[0]
|
key = list(item.keys())[0]
|
||||||
themes[key] = item[key]
|
themes[key] = item[key]
|
||||||
|
|
||||||
# Get the current date.
|
|
||||||
current_day = datetime.now().day
|
|
||||||
current_month = datetime.now().month
|
|
||||||
|
|
||||||
# Check each theme in the dictionary and return the first one that is "correct"
|
# Get the current date.
|
||||||
for theme in themes.values():
|
current_date = datetime.now()
|
||||||
if theme['type'] == 'static-date':
|
current_year = current_date.year
|
||||||
start_day, start_month = theme['start'].split('/')
|
|
||||||
start_day = int(start_day)
|
|
||||||
start_month = int(start_month)
|
|
||||||
|
|
||||||
end_day, end_month = theme['end'].split('/')
|
|
||||||
end_day = int(end_day)
|
|
||||||
end_month = int(end_month)
|
|
||||||
|
|
||||||
if end_month < start_month:
|
|
||||||
# Hacky (werkt nu maar kan beter)
|
|
||||||
end_month += 12
|
|
||||||
|
|
||||||
if theme['type'] == 'static-date':
|
# Check each theme in the dictionary and return the first one that is "correct"
|
||||||
|
for theme in themes.values():
|
||||||
|
if theme['type'] == 'static-date':
|
||||||
|
start_day, start_month = theme['start'].split('/')
|
||||||
|
start_date = datetime(year=current_year, day=int(
|
||||||
|
start_day), month=int(start_month))
|
||||||
|
|
||||||
if (((start_month == current_month) and
|
end_day, end_month = theme['end'].split('/')
|
||||||
(start_day <= current_day)) or
|
if int(start_month) > int(end_month):
|
||||||
(start_month <= current_month)):
|
current_year += 1
|
||||||
|
end_date = datetime(
|
||||||
|
year=current_year, day=int(end_day), month=int(end_month))
|
||||||
|
|
||||||
if (((end_month == current_month) and
|
if start_date <= current_date <= end_date:
|
||||||
(end_day >= current_day)) or
|
path = os.path.join(app.root_path, cssPath, theme['file'])
|
||||||
(end_month > current_month)):
|
break
|
||||||
path = os.path.join(str(app.root_path), cssPath, theme['file'])
|
elif cookie_theme == 'darkmode':
|
||||||
break
|
path = os.path.join(
|
||||||
else:
|
app.root_path, "static/css/themes/lowPerformance/darkmode.css")
|
||||||
if request.cookies['theme'] == 'darkmode' :
|
|
||||||
path = os.path.join(str(app.root_path), "static/css/themes/lowPerformance/darkmode.css")
|
|
||||||
else:
|
|
||||||
path = os.path.join(str(app.root_path), "static/css/themes/lowPerformance/lightmode.css")
|
|
||||||
|
|
||||||
# Tijdelijk ongebruikt tot bewezen dat het veilig is
|
|
||||||
#try:
|
|
||||||
# path = os.path.join(str(app.root_path), "static/css/themes/lowPerformance/", request.cookies['theme']+".css")
|
|
||||||
# f = open(path)
|
|
||||||
#except IOError:
|
|
||||||
# f = open(cssPath+"lightmode.css")
|
|
||||||
else:
|
else:
|
||||||
path = os.path.join(str(app.root_path), "static/css/themes/lowPerformance/lightmode.css")
|
path = os.path.join(
|
||||||
|
app.root_path, "static/css/themes/lowPerformance/lightmode.css")
|
||||||
|
|
||||||
f = open(path)
|
f = open(path)
|
||||||
response = make_response(f.read())
|
response = make_response(f.read())
|
||||||
response.headers['Content-Type'] = 'text/css'
|
response.headers['Content-Type'] = 'text/css'
|
||||||
|
@ -146,13 +128,13 @@ def favicon() -> str:
|
||||||
# pylint: disable=R1705
|
# pylint: disable=R1705
|
||||||
if not get_orders((Order.stoptime > datetime.now())):
|
if not get_orders((Order.stoptime > datetime.now())):
|
||||||
return send_from_directory(
|
return send_from_directory(
|
||||||
os.path.join(str(app.root_path), "static"),
|
os.path.join(app.root_path, "static"),
|
||||||
"favicon.ico",
|
"favicon.ico",
|
||||||
mimetype="image/x-icon",
|
mimetype="image/x-icon",
|
||||||
)
|
)
|
||||||
else:
|
else:
|
||||||
return send_from_directory(
|
return send_from_directory(
|
||||||
os.path.join(str(app.root_path), "static"),
|
os.path.join(app.root_path, "static"),
|
||||||
"favicon_orange.ico",
|
"favicon_orange.ico",
|
||||||
mimetype="image/x-icon",
|
mimetype="image/x-icon",
|
||||||
)
|
)
|
||||||
|
|
|
@ -31,7 +31,7 @@ def authorized() -> typing.Any:
|
||||||
request.args["error_description"],
|
request.args["error_description"],
|
||||||
)
|
)
|
||||||
if isinstance(resp, OAuthException):
|
if isinstance(resp, OAuthException):
|
||||||
return "Access denied: %s" % resp.message + "<br>" + str(resp.data)
|
return f"Access denied: {resp.message}<br>{resp.data}"
|
||||||
|
|
||||||
session["zeus_token"] = (resp["access_token"], "")
|
session["zeus_token"] = (resp["access_token"], "")
|
||||||
me = current_app.zeus.get("current_user/")
|
me = current_app.zeus.get("current_user/")
|
||||||
|
|
Loading…
Reference in a new issue