From b8c15e5cf388696222a11018d7fcf4966000a8fa Mon Sep 17 00:00:00 2001 From: Jan-Pieter Baert Date: Tue, 17 Dec 2019 19:53:34 +0100 Subject: [PATCH 1/7] Add css misc functions A function to list all possible themes and a list to load the css from the cookie --- app/views/general.py | 97 ++++++++++++++++++++++++++------------------ 1 file changed, 57 insertions(+), 40 deletions(-) diff --git a/app/views/general.py b/app/views/general.py index c314264..51249e2 100644 --- a/app/views/general.py +++ b/app/views/general.py @@ -5,7 +5,7 @@ from datetime import datetime, timedelta import yaml from flask import Flask, render_template, make_response -from flask import request +from flask import request, jsonify from flask import Blueprint, abort from flask import current_app as app from flask import send_from_directory, url_for @@ -30,54 +30,71 @@ def home() -> str: ) +def get_css_dict(css_path): + themes_dict = dict() + + # 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 their attributes. + themes = {} + for item in data: + key = list(item.keys())[0] + themes[key] = item[key] + + # 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 key, theme in themes.items(): + print(key) + print(theme) + 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('/') + 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 start_date <= current_date <= end_date: + path = os.path.join(app.root_path, css_path, theme['file']) + themes_dict[key] = path + themes_dict['darkmode'] = os.path.join( + app.root_path, "static/css/themes/lowPerformance/darkmode.css") + themes_dict['lightmode'] = os.path.join( + app.root_path, "static/css/themes/lowPerformance/lightmode.css") + + return themes_dict + + +@general_bp.route("/css-list") +def css_list(): + if request.cookies.get('performance', '') == 'highPerformance': + css_path = 'static/css/themes/highPerformance/' + else: + css_path = 'static/css/themes/lowPerformance/' + return jsonify(list(get_css_dict(css_path).keys())) + + @general_bp.route("/css") def css(): "Generate the css" if request.cookies.get('performance', '') == 'highPerformance': - cssPath = 'static/css/themes/highPerformance/' + css_path = 'static/css/themes/highPerformance/' else: - cssPath = 'static/css/themes/lowPerformance/' + css_path = 'static/css/themes/lowPerformance/' 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(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 their attributes. - themes = {} - for item in data: - key = list(item.keys())[0] - themes[key] = item[key] + themes_dict = get_css_dict(css_path) - # 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_date = datetime(year=current_year, day=int( - start_day), month=int(start_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 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( - app.root_path, "static/css/themes/lowPerformance/lightmode.css") + path = themes_dict[cookie_theme] f = open(path) response = make_response(f.read()) From 1657cec1d1cc211a82e801868932ae2e0cd1fa08 Mon Sep 17 00:00:00 2001 From: Pieter-Jan Cassiman Date: Tue, 17 Dec 2019 22:21:17 +0100 Subject: [PATCH 2/7] Added JS for dealing with setting the custom theme --- app/static/js/customThemes.js | 11 +++++++++++ app/templates/layout.html | 1 + 2 files changed, 12 insertions(+) create mode 100644 app/static/js/customThemes.js diff --git a/app/static/js/customThemes.js b/app/static/js/customThemes.js new file mode 100644 index 0000000..9737685 --- /dev/null +++ b/app/static/js/customThemes.js @@ -0,0 +1,11 @@ +function changeTheme() { + // Get the selected theme for the dropdown + var themes_select = document.getElementById("themes_select"); + var selected_theme = themes_select.options[themes_select.selectedIndex].text; + + // Update the theme cookie + document.cookie = "theme=" + escape(selected_theme) + "; Path=/;" + + // Finally reload the page to let the new theme take effect + location.reload(); +} \ No newline at end of file diff --git a/app/templates/layout.html b/app/templates/layout.html index bcc73c9..7e4bce1 100644 --- a/app/templates/layout.html +++ b/app/templates/layout.html @@ -32,6 +32,7 @@ Haldis - {{ active_page|capitalize }} {{ super() }} + {% endblock %} {% block navbar %} From 4ad70992f5f4ab60f0429f37e804c5234dbd321c Mon Sep 17 00:00:00 2001 From: Pieter-Jan Cassiman Date: Tue, 17 Dec 2019 22:24:29 +0100 Subject: [PATCH 3/7] Added select for choosing custom themes --- app/templates/profile.html | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/app/templates/profile.html b/app/templates/profile.html index f270a91..964905b 100644 --- a/app/templates/profile.html +++ b/app/templates/profile.html @@ -6,4 +6,13 @@

Themes

enable custom themes

enable high performance

+

+ + +

{% endblock %} From 7ac0d6291ed7ed3e8ea04c5f043d5a8ec1440eeb Mon Sep 17 00:00:00 2001 From: Pieter-Jan Cassiman Date: Tue, 17 Dec 2019 22:25:59 +0100 Subject: [PATCH 4/7] Fixed css function to deal with default [customTheme] cookie --- app/views/general.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/app/views/general.py b/app/views/general.py index 51249e2..569d448 100644 --- a/app/views/general.py +++ b/app/views/general.py @@ -94,7 +94,11 @@ def css(): themes_dict = get_css_dict(css_path) - path = themes_dict[cookie_theme] + # TODO: Fix to work with default cookie value [customTheme] + if cookie_theme == "customTheme": + path = css_path+"ligtmode.css" + else: + path = themes_dict[cookie_theme] f = open(path) response = make_response(f.read()) From 285c36f0cf6f96f0df550b2787dfbfefbdd2af70 Mon Sep 17 00:00:00 2001 From: Pieter-Jan Cassiman Date: Tue, 17 Dec 2019 22:26:48 +0100 Subject: [PATCH 5/7] Updated css_list function to return a list instead of a json --- app/views/general.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/views/general.py b/app/views/general.py index 569d448..73c691e 100644 --- a/app/views/general.py +++ b/app/views/general.py @@ -73,13 +73,13 @@ def get_css_dict(css_path): return themes_dict -@general_bp.route("/css-list") +# @general_bp.route("/css-list") def css_list(): if request.cookies.get('performance', '') == 'highPerformance': css_path = 'static/css/themes/highPerformance/' else: css_path = 'static/css/themes/lowPerformance/' - return jsonify(list(get_css_dict(css_path).keys())) + return list(get_css_dict(css_path).keys()) @general_bp.route("/css") From 98c57b161e8892ba845cbd1c7808f432956208da Mon Sep 17 00:00:00 2001 From: Pieter-Jan Cassiman Date: Tue, 17 Dec 2019 22:28:36 +0100 Subject: [PATCH 6/7] Added css_list to render_template for profile page --- app/views/general.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/views/general.py b/app/views/general.py index 73c691e..0f39358 100644 --- a/app/views/general.py +++ b/app/views/general.py @@ -140,7 +140,7 @@ def about() -> str: @login_required def profile() -> str: "Generate the profile view" - return render_template("profile.html") + return render_template("profile.html", themes_list=css_list()) @general_bp.route("/favicon.ico") From d6f0019232bd899444f4d051c465761608ce0cff Mon Sep 17 00:00:00 2001 From: Jan-Pieter Baert Date: Tue, 17 Dec 2019 23:44:15 +0100 Subject: [PATCH 7/7] Small fixes Remove print statements and use format strings instead of "+" --- app/views/general.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/app/views/general.py b/app/views/general.py index 0f39358..2f55183 100644 --- a/app/views/general.py +++ b/app/views/general.py @@ -49,8 +49,6 @@ def get_css_dict(css_path): # Check each theme in the dictionary and return the first one that is "correct" for key, theme in themes.items(): - print(key) - print(theme) if theme['type'] == 'static-date': start_day, start_month = theme['start'].split('/') start_date = datetime(year=current_year, day=int( @@ -96,7 +94,7 @@ def css(): # TODO: Fix to work with default cookie value [customTheme] if cookie_theme == "customTheme": - path = css_path+"ligtmode.css" + path = f"{css_path}ligtmode.css" else: path = themes_dict[cookie_theme]