diff --git a/src/wagtail_tag_manager/forms.py b/src/wagtail_tag_manager/forms.py index 349d2e1..81b4a00 100644 --- a/src/wagtail_tag_manager/forms.py +++ b/src/wagtail_tag_manager/forms.py @@ -14,7 +14,7 @@ def __init__(self, *args, **kwargs): value = config.get("value") initial = value == SETTING_INITIAL or value == SETTING_REQUIRED - if SETTING_INITIAL in kwargs: + if "initial" in kwargs: initial = kwargs.get(SETTING_INITIAL)[tag_type] self.fields[tag_type] = forms.BooleanField( diff --git a/src/wagtail_tag_manager/strategy.py b/src/wagtail_tag_manager/strategy.py index 371fcfe..de30d43 100644 --- a/src/wagtail_tag_manager/strategy.py +++ b/src/wagtail_tag_manager/strategy.py @@ -1,6 +1,6 @@ from django.http import Http404 -from django.db.models import Q from wagtail.views import serve as wagtail_serve +from django.db.models import Q from wagtail_tag_manager.mixins import TagMixin from wagtail_tag_manager.models import Tag, Trigger, TagTypeSettings @@ -48,7 +48,7 @@ SETTING_DEFAULT, ( (lambda c: c == CONSENT_TRUE, CONSENT_TRUE, True, False), - (lambda c: c != CONSENT_TRUE, CONSENT_FALSE, False, False), + (lambda c: c == CONSENT_UNSET, CONSENT_UNSET, False, False), ), ), ("POST", SETTING_REQUIRED, ((lambda c: True, CONSENT_TRUE, False, True),)), @@ -74,7 +74,7 @@ SETTING_DEFAULT, ( (lambda c: c == CONSENT_TRUE, CONSENT_TRUE, False, True), - (lambda c: c != CONSENT_TRUE, CONSENT_FALSE, False, False), + (lambda c: c == CONSENT_UNSET, CONSENT_UNSET, False, False), ), ), ) @@ -249,10 +249,25 @@ def _get_tags_for_trigger(self): @property def cookie_state(self): - return { - tag_type: self.consent.get(tag_type, CONSENT_FALSE) != CONSENT_FALSE - for tag_type in Tag.get_types() - } + """ + Returns the consent state mapped to simple True and False booleans + instead of the in-between consent states that might exist. + """ + + cookie_state = {} + + for tag_type, config in TagTypeSettings.all().items(): + state = self.consent.get(tag_type, CONSENT_FALSE) + setting = config.get("value", SETTING_DEFAULT) + + if setting in (SETTING_DEFAULT): + cookie_state[tag_type] = self.consent.get( + tag_type, CONSENT_UNSET + ) not in (CONSENT_FALSE, CONSENT_UNSET) + else: + cookie_state[tag_type] = state != CONSENT_FALSE + + return cookie_state @property def is_debug(self): diff --git a/tests/unit/test_endpoints.py b/tests/unit/test_endpoints.py index a82ce24..d9905a6 100644 --- a/tests/unit/test_endpoints.py +++ b/tests/unit/test_endpoints.py @@ -51,7 +51,7 @@ def test_lazy_cookies(client, site): assert consent_state.get("necessary", "") == "true" assert consent_state.get("preferences", "") == "unset" assert consent_state.get("statistics", "") == "unset" - assert consent_state.get("marketing", "") == "false" + assert consent_state.get("marketing", "") == "unset" @pytest.mark.django_db