From 204e1945dabb35e78d79fe4bea86737a8f8c8d94 Mon Sep 17 00:00:00 2001 From: Pieter-Jan Cassiman Date: Fri, 22 Nov 2019 12:22:25 +0100 Subject: [PATCH] Added functionality to automatically choose the relevant seasonal theme --- app/views/general.py | 39 +++++++++++++++++++++++++++++++++++++-- 1 file changed, 37 insertions(+), 2 deletions(-) diff --git a/app/views/general.py b/app/views/general.py index fc13ae3..63b5aff 100644 --- a/app/views/general.py +++ b/app/views/general.py @@ -13,6 +13,8 @@ from models import Location, Order # import views from views.order import get_orders +import yaml + general_bp = Blueprint("general_bp", __name__) @@ -32,8 +34,41 @@ def css(): "Generate the css" if request.cookies.get('theme'): if request.cookies['theme'] == 'customTheme': - #TODO: The custom theme is hardcoded :(. Make the server auto select a custom team. - f = open("app/static/css/themes/sinterklaas.css") + + # Here seasonal themes will be returned; matching the current date. + + # Open the YAML file with all the themes. + with open('app/views/themes.yml', '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 + + # Check each theme in the dictionary and return the first one that is "correct" + for theme in themes.values(): + start_day, start_month = theme['start'].split('/') + end_day, end_month = theme['end'].split('/') + + if theme['type'] == 'static-date': + + if (((int(start_month) == current_month) and + (int(start_day) <= current_day)) or + (int(start_month) <= current_month)): + + if (((int(end_month) == current_month) and + (int(end_day) >= current_day)) or + (int(end_month) > current_month)): + + f = open("app/static/css/themes/"+theme['file']) + break + else: f = open("app/static/css/themes/"+request.cookies['theme']+".css") else: