From 93492858c6f7b78f86682931a3b552c3e8d0f565 Mon Sep 17 00:00:00 2001 From: kritzl Date: Mon, 1 Apr 2024 03:53:59 +0200 Subject: [PATCH 1/3] add get parameters to control features --- mafiasi/base/special_day_middleware.py | 52 +++++++++++++++++++++----- mafiasi/base/static/js/misc.js | 12 +++++- mafiasi/base/templates/base.html | 6 +-- 3 files changed, 56 insertions(+), 14 deletions(-) diff --git a/mafiasi/base/special_day_middleware.py b/mafiasi/base/special_day_middleware.py index cc243e6..7dd9860 100644 --- a/mafiasi/base/special_day_middleware.py +++ b/mafiasi/base/special_day_middleware.py @@ -10,33 +10,65 @@ def __init__(self, get_response): def __call__(self, request): request.session["specialDay"] = False - request.session["specialFeature"] = False request.session["specialFeatureClasses"] = "" + request.session["specialFeatureDisabled"] = False + feature = "" + + # check if user wants to persist feature for this session (or reset it) + if request.GET.get("persistSpecialFeatureForThisSession", "undefined") == "true": + request.session["specialFeaturePersist"] = True + elif request.GET.get("persistSpecialFeatureForThisSession", "undefined") == "false": + request.session["specialFeaturePersist"] = False + + # reset feature if persist is not true + if not request.session.get("specialFeaturePersist", False): + request.session["specialFeature"] = False # get date in current timezone n = localdate(now(), get_current_timezone()) # check if user disabled special day surprises via cookie if request.COOKIES.get("disable-special", False): - request.session["specialFeature"] = "DISABLED" + request.session["specialFeatureDisabled"] = True + + # check if user enabled feature manually via GET parameter + elif request.GET.get("specialFeature", False): + feature = request.GET.get("specialFeature", False) + + # check if feature is already set (if persisted and not reset above) + elif request.session["specialFeature"]: + feature = request.session["specialFeature"] + else: + # check if it's a special day + # first of april (april fools) if n.day == 1 and n.month == 4: request.session["specialDay"] = "April Fools" option = random.randint(0, 2) if option == 0: - request.session["specialFeature"] = "upsideDown" - request.session["specialFeatureClasses"] += " first-of-april" + feature = "upsideDown" elif option == 1: - request.session["specialFeature"] = "uwu" - translation.activate("en-uwu") - request.LANGUAGE_CODE = translation.get_language() + feature = "uwu" elif option == 2: - request.session["specialFeature"] = "mafiasiPurple" - + feature = "mafiasiPurple" # winter season if n.month == 12 and n.day >= 10: - request.session["specialFeature"] = "winter" + feature = "winter" + + # make sure session hold the current feature + request.session["specialFeature"] = feature + + # handle feature + if feature == "upsideDown": + request.session["specialFeatureClasses"] += " first-of-april" + elif feature == "uwu": + translation.activate("en-uwu") + request.LANGUAGE_CODE = translation.get_language() + elif feature == "mafiasiPurple": + pass + elif feature == "winter": + pass # call view response = self.get_response(request) diff --git a/mafiasi/base/static/js/misc.js b/mafiasi/base/static/js/misc.js index b264f55..0031aa0 100644 --- a/mafiasi/base/static/js/misc.js +++ b/mafiasi/base/static/js/misc.js @@ -13,7 +13,9 @@ function disableSpecialFeature() { tomorrow.setDate(tomorrow.getDate() + 1); tomorrow.setHours(0, 0, 0); document.cookie = `disable-special=True; path=/; expires=${tomorrow.toUTCString()}; Secure`; - window.location.reload() + let url = new URL(location.href); + url.searchParams.delete('specialFeature'); + window.location.href = url } function enableSpecialFeature() { @@ -25,3 +27,11 @@ const specialOff = document.querySelector('#special-off'); const specialOn = document.querySelector('#special-on'); if(specialOff) specialOff.addEventListener('click', disableSpecialFeature) if(specialOn) specialOn.addEventListener('click', enableSpecialFeature) + +if("feature" in document.querySelector('html').dataset){ + const feature = document.querySelector('html').dataset.feature + const url = new URL(location.href); + url.searchParams.set("specialFeature", feature); + url.searchParams.set("persistSpecialFeatureForThisSession", "featurePersisted" in document.querySelector('html').dataset ? 'true' : 'undefined'); + history.pushState({}, "", url); +} diff --git a/mafiasi/base/templates/base.html b/mafiasi/base/templates/base.html index 2846d80..e624b3c 100644 --- a/mafiasi/base/templates/base.html +++ b/mafiasi/base/templates/base.html @@ -4,7 +4,7 @@ {% load static %} {% value_from_settings BANNER_IMG as banner_img %} - + {% block wtitle %}{% endblock %} · {% value_from_settings PROJECT_NAME %} @@ -37,11 +37,11 @@ {% endif %}
{% value_from_settings PROJECT_BANNER %} - {% if request.session.specialFeature == 'DISABLED' %} + {% if request.session.specialFeatureDisabled %} - {% else %} + {% elif request.session.specialFeature %} {% trans "Disable surprise" %} From 4874e2437dfb509a1f1b20e2114fe513f68fd31a Mon Sep 17 00:00:00 2001 From: kritzl Date: Mon, 1 Apr 2024 03:59:27 +0200 Subject: [PATCH 2/3] fix theme toggle --- mafiasi/base/static/js/misc.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/mafiasi/base/static/js/misc.js b/mafiasi/base/static/js/misc.js index 0031aa0..d44ebef 100644 --- a/mafiasi/base/static/js/misc.js +++ b/mafiasi/base/static/js/misc.js @@ -8,6 +8,8 @@ function toggleTheme() { document.documentElement.dataset.theme = theme } +document.querySelector('#theme-toggle').addEventListener('click', toggleTheme) + function disableSpecialFeature() { const tomorrow = new Date() tomorrow.setDate(tomorrow.getDate() + 1); From 483d0c13168f3f1ea5f64d863f307ccc0328d113 Mon Sep 17 00:00:00 2001 From: kritzl Date: Mon, 1 Apr 2024 04:03:24 +0200 Subject: [PATCH 3/3] delete persist parameter when disabling special features --- mafiasi/base/static/js/misc.js | 1 + 1 file changed, 1 insertion(+) diff --git a/mafiasi/base/static/js/misc.js b/mafiasi/base/static/js/misc.js index d44ebef..0ab8b3d 100644 --- a/mafiasi/base/static/js/misc.js +++ b/mafiasi/base/static/js/misc.js @@ -17,6 +17,7 @@ function disableSpecialFeature() { document.cookie = `disable-special=True; path=/; expires=${tomorrow.toUTCString()}; Secure`; let url = new URL(location.href); url.searchParams.delete('specialFeature'); + url.searchParams.delete('persistSpecialFeatureForThisSession'); window.location.href = url }