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
from datetime import datetime, timedelta
import yaml
from flask import Flask, render_template, make_response
from flask import request
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_login import login_required
import yaml
from models import Location, Order
# import views
from views.order import get_orders
@ -33,70 +33,52 @@ def home() -> str:
@general_bp.route("/css")
def 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/'
else:
cssPath = 'static/css/themes/lowPerformance/'
if request.cookies.get('theme'):
if request.cookies['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.
cookie_theme = request.cookies.get('theme', '')
if cookie_theme == 'customTheme':
# Here seasonal themes will be returned; matching the current date.
# Open the YAML file with all the themes.
path = os.path.join(str(app.root_path), "views/themes.yml")
with open(path, 'r') as stream:
data = yaml.safe_load(stream)
# Build a dictionary from the YAML file with all the themes and there attributes.
themes = {}
for item in data:
key = list(item.keys())[0]
themes[key] = item[key]
# Open the YAML file with all the themes.
path = os.path.join(app.root_path, "views/themes.yml")
with open(path, 'r') as stream:
data = yaml.safe_load(stream)
# Build a dictionary from the YAML file with all the themes and there attributes.
themes = {}
for item in data:
key = list(item.keys())[0]
themes[key] = item[key]
# Get the current date.
current_day = datetime.now().day
current_month = datetime.now().month
# Get the current date.
current_date = datetime.now()
current_year = current_date.year
# 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_day = int(start_day)
start_month = int(start_month)
# 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))
end_day, end_month = theme['end'].split('/')
end_day = int(end_day)
end_month = int(end_month)
end_day, end_month = theme['end'].split('/')
if int(start_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:
# Hacky (werkt nu maar kan beter)
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
else:
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")
if start_date <= current_date <= end_date:
path = os.path.join(app.root_path, cssPath, theme['file'])
break
elif cookie_theme == 'darkmode':
path = os.path.join(
app.root_path, "static/css/themes/lowPerformance/darkmode.css")
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)
response = make_response(f.read())
response.headers['Content-Type'] = 'text/css'
@ -146,13 +128,13 @@ def favicon() -> str:
# pylint: disable=R1705
if not get_orders((Order.stoptime > datetime.now())):
return send_from_directory(
os.path.join(str(app.root_path), "static"),
os.path.join(app.root_path, "static"),
"favicon.ico",
mimetype="image/x-icon",
)
else:
return send_from_directory(
os.path.join(str(app.root_path), "static"),
os.path.join(app.root_path, "static"),
"favicon_orange.ico",
mimetype="image/x-icon",
)

View file

@ -31,7 +31,7 @@ def authorized() -> typing.Any:
request.args["error_description"],
)
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"], "")
me = current_app.zeus.get("current_user/")