From 0b88c9b66064222f55142561ebb16091e4717177 Mon Sep 17 00:00:00 2001 From: Pieter-Jan Cassiman Date: Fri, 22 Nov 2019 12:21:46 +0100 Subject: [PATCH 1/5] Added file for storing and parsing all themes --- app/views/themes.yml | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 app/views/themes.yml diff --git a/app/views/themes.yml b/app/views/themes.yml new file mode 100644 index 0000000..688f9e4 --- /dev/null +++ b/app/views/themes.yml @@ -0,0 +1,22 @@ +# Seasonal themes for Haldis +- lightmode: + file: lightmode.css + type: default +- darkmode: + file: darkmode.css + type: default +- halloween: + file: halloween.css + type: static-date + start: 21/10 + end: 10/11 +- sinterklaas: + file: sinterklaas.css + type: static-date + start: 28/11 + end: 16/12 +- kerstmis: + file: kerstmis.css + type: static-date + start: 15/12 + end: 31/12 \ No newline at end of file From 204e1945dabb35e78d79fe4bea86737a8f8c8d94 Mon Sep 17 00:00:00 2001 From: Pieter-Jan Cassiman Date: Fri, 22 Nov 2019 12:22:25 +0100 Subject: [PATCH 2/5] 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: From f3cbcf78f7ec4fc6e7225097e76ec905908f1497 Mon Sep 17 00:00:00 2001 From: Pieter-Jan Cassiman Date: Fri, 22 Nov 2019 12:22:57 +0100 Subject: [PATCH 3/5] Added PyYaml package to requirements.txt; requried for parsing yaml files --- requirements.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/requirements.txt b/requirements.txt index ee87b58..97f3926 100644 --- a/requirements.txt +++ b/requirements.txt @@ -33,6 +33,7 @@ oauthlib==2.1.0 # via flask-oauthlib, requests-oauthlib pymysql==0.9.3 python-dateutil==2.8.0 # via alembic python-editor==1.0.4 # via alembic +PyYaml==5.1.2 requests-oauthlib==1.1.0 # via flask-oauthlib requests==2.21.0 # via airbrake, requests-oauthlib six==1.12.0 # via python-dateutil From 15f8f6c5d9810f9de213d7ed47750f246f8eb01e Mon Sep 17 00:00:00 2001 From: Pieter-Jan Cassiman Date: Thu, 5 Dec 2019 19:19:03 +0100 Subject: [PATCH 4/5] Updated theme dates --- app/views/themes.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/views/themes.yml b/app/views/themes.yml index 688f9e4..775e295 100644 --- a/app/views/themes.yml +++ b/app/views/themes.yml @@ -14,9 +14,9 @@ file: sinterklaas.css type: static-date start: 28/11 - end: 16/12 + end: 14/12 - kerstmis: file: kerstmis.css type: static-date start: 15/12 - end: 31/12 \ No newline at end of file + end: 06/01 \ No newline at end of file From a71f7434d15c62a185757422549ef08f04eb42f1 Mon Sep 17 00:00:00 2001 From: Pieter-Jan Cassiman Date: Thu, 5 Dec 2019 19:19:36 +0100 Subject: [PATCH 5/5] Updated code to deal with dates that wrap around the new year --- app/views/general.py | 29 ++++++++++++++++++++--------- 1 file changed, 20 insertions(+), 9 deletions(-) diff --git a/app/views/general.py b/app/views/general.py index 63b5aff..e63a0f7 100644 --- a/app/views/general.py +++ b/app/views/general.py @@ -6,15 +6,15 @@ from flask import Flask, render_template, make_response from flask import request from flask import Blueprint, abort from flask import current_app as app -from flask import render_template, send_from_directory, url_for +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 -import yaml - general_bp = Blueprint("general_bp", __name__) @@ -29,6 +29,7 @@ def home() -> str: "home.html", orders=get_orders(), recently_closed=recently_closed ) + @general_bp.route("/css") def css(): "Generate the css" @@ -54,17 +55,26 @@ def css(): # 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('/') + 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': - if (((int(start_month) == current_month) and - (int(start_day) <= current_day)) or - (int(start_month) <= current_month)): + if (((start_month == current_month) and + (start_day <= current_day)) or + (start_month <= current_month)): - if (((int(end_month) == current_month) and - (int(end_day) >= current_day)) or - (int(end_month) > current_month)): + if (((end_month == current_month) and + (end_day >= current_day)) or + (end_month > current_month)): f = open("app/static/css/themes/"+theme['file']) break @@ -75,6 +85,7 @@ def css(): f = open("app/static/css/main.css") response = make_response(f.read()) response.headers['Content-Type'] = 'text/css' + f.close() return response