Skip to content

Commit

Permalink
Unset default tags until a choice has been made
Browse files Browse the repository at this point in the history
Fixes #108
  • Loading branch information
jberghoef committed Jun 8, 2022
1 parent 6b886aa commit ce5323c
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 9 deletions.
2 changes: 1 addition & 1 deletion src/wagtail_tag_manager/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
29 changes: 22 additions & 7 deletions src/wagtail_tag_manager/strategy.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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),)),
Expand All @@ -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),
),
),
)
Expand Down Expand Up @@ -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):
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/test_endpoints.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit ce5323c

Please sign in to comment.