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..0ab8b3d 100644 --- a/mafiasi/base/static/js/misc.js +++ b/mafiasi/base/static/js/misc.js @@ -8,12 +8,17 @@ 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); 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'); + url.searchParams.delete('persistSpecialFeatureForThisSession'); + window.location.href = url } function enableSpecialFeature() { @@ -25,3 +30,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 %} - +