Make custom theme code a bit cleaner

This commit is contained in:
Jan-Pieter Baert 2019-12-06 16:12:05 +01:00
parent a81055b574
commit ababb85402
No known key found for this signature in database
GPG key ID: B19186932178234A

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,70 +33,53 @@ 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. #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(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(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(app.root_path, "static/css/themes/lowPerformance/darkmode.css")
else:
path = os.path.join(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(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'