Merge pull request #165 from ZeusWPI/optimisations

Add a few optimisations to haldis
This commit is contained in:
Jan-Pieter Baert 2019-12-06 16:27:46 +01:00 committed by GitHub
commit 33203af6cc
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 41 additions and 59 deletions

View file

@ -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,18 +33,17 @@ 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.
@ -54,49 +53,32 @@ def css():
themes[key] = item[key] themes[key] = item[key]
# Get the current date. # Get the current date.
current_day = datetime.now().day current_date = datetime.now()
current_month = datetime.now().month current_year = current_date.year
# Check each theme in the dictionary and return the first one that is "correct" # Check each theme in the dictionary and return the first one that is "correct"
for theme in themes.values(): for theme in themes.values():
if theme['type'] == 'static-date': if theme['type'] == 'static-date':
start_day, start_month = theme['start'].split('/') start_day, start_month = theme['start'].split('/')
start_day = int(start_day) start_date = datetime(year=current_year, day=int(
start_month = int(start_month) start_day), month=int(start_month))
end_day, end_month = theme['end'].split('/') end_day, end_month = theme['end'].split('/')
end_day = int(end_day) if int(start_month) > int(end_month):
end_month = int(end_month) current_year += 1
end_date = datetime(
year=current_year, day=int(end_day), month=int(end_month))
if end_month < start_month: if start_date <= current_date <= end_date:
# Hacky (werkt nu maar kan beter) path = os.path.join(app.root_path, cssPath, theme['file'])
end_month += 12
if theme['type'] == 'static-date':
if (((start_month == current_month) and
(start_day <= current_day)) or
(start_month <= current_month)):
if (((end_month == current_month) and
(end_day >= current_day)) or
(end_month > current_month)):
path = os.path.join(str(app.root_path), cssPath, theme['file'])
break break
elif cookie_theme == 'darkmode':
path = os.path.join(
app.root_path, "static/css/themes/lowPerformance/darkmode.css")
else: else:
if request.cookies['theme'] == 'darkmode' : path = os.path.join(
path = os.path.join(str(app.root_path), "static/css/themes/lowPerformance/darkmode.css") app.root_path, "static/css/themes/lowPerformance/lightmode.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:
path = os.path.join(str(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",
) )

View file

@ -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/")