Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unset default tags until a choice has been made #110

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cypress/e2e/cookie_consent.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ describe("Cookie consent", () => {
cy.getCookie("wtm").should(
"have.property",
"value",
"necessary:true|preferences:unset|statistics:pending|marketing:false"
"necessary:unset|preferences:unset|statistics:pending|marketing:unset"
);
});

Expand Down
2 changes: 1 addition & 1 deletion cypress/e2e/sandbox.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ describe("The website", () => {
cy.getCookie("wtm").should(
"have.property",
"value",
"necessary:true|preferences:unset|statistics:pending|marketing:false"
"necessary:unset|preferences:unset|statistics:pending|marketing:unset"
);

cy.get("body")
Expand Down
8 changes: 4 additions & 4 deletions cypress/e2e/tags.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ describe("Necessary tags", () => {
cy.getCookie("wtm").should(
"have.property",
"value",
"necessary:true|preferences:unset|statistics:pending|marketing:false"
"necessary:unset|preferences:unset|statistics:pending|marketing:unset"
);

cy.setCookie("wtm", "necessary:false|preferences:false|statistics:false|marketing:false");
Expand All @@ -37,7 +37,7 @@ describe("Necessary tags", () => {
cy.getCookie("wtm").should(
"have.property",
"value",
"necessary:true|preferences:false|statistics:false|marketing:false"
"necessary:unset|preferences:false|statistics:false|marketing:false"
);

cy.setCookie("wtm", "necessary:unset|preferences:false|statistics:false|marketing:false");
Expand All @@ -51,7 +51,7 @@ describe("Necessary tags", () => {
cy.getCookie("wtm").should(
"have.property",
"value",
"necessary:true|preferences:false|statistics:false|marketing:false"
"necessary:unset|preferences:false|statistics:false|marketing:false"
);
});

Expand Down Expand Up @@ -183,7 +183,7 @@ describe("Marketing tags", () => {
cy.getCookie("wtm").should(
"have.property",
"value",
"necessary:true|preferences:false|statistics:false|marketing:false"
"necessary:true|preferences:false|statistics:false|marketing:unset"
);
});

Expand Down
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
41 changes: 33 additions & 8 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 All @@ -23,6 +23,7 @@
SETTING_REQUIRED,
(
# Consent validator, consent value, include instant tags, include lazy tags
(lambda c: c != CONSENT_TRUE, CONSENT_UNSET, True, False),
(lambda c: True, CONSENT_TRUE, True, False),
),
),
Expand All @@ -47,11 +48,18 @@
"GET",
SETTING_DEFAULT,
(
(lambda c: c == CONSENT_UNSET, CONSENT_UNSET, False, False),
(lambda c: c == CONSENT_TRUE, CONSENT_TRUE, True, False),
(lambda c: c != CONSENT_TRUE, CONSENT_FALSE, False, False),
),
),
("POST", SETTING_REQUIRED, ((lambda c: True, CONSENT_TRUE, False, True),)),
(
"POST",
SETTING_REQUIRED,
(
(lambda c: c != CONSENT_TRUE, CONSENT_UNSET, False, True),
(lambda c: True, CONSENT_TRUE, False, True),
),
),
(
"POST",
SETTING_INITIAL,
Expand All @@ -73,8 +81,8 @@
"POST",
SETTING_DEFAULT,
(
(lambda c: c == CONSENT_UNSET, CONSENT_UNSET, False, False),
(lambda c: c == CONSENT_TRUE, CONSENT_TRUE, False, True),
(lambda c: c != CONSENT_TRUE, CONSENT_FALSE, False, False),
),
),
)
Expand Down Expand Up @@ -249,10 +257,27 @@ 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 == SETTING_REQUIRED:
cookie_state[tag_type] = True
elif setting == 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
6 changes: 3 additions & 3 deletions tests/unit/test_endpoints.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,10 @@ def test_lazy_cookies(client, site):

assert "wtm" in response.cookies
consent_state = get_consent(response)
assert consent_state.get("necessary", "") == "true"
assert consent_state.get("necessary", "") == "unset"
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 All @@ -69,7 +69,7 @@ def test_required_lazy_cookies(client, site):

assert "wtm" in response.cookies
consent_state = get_consent(response)
assert consent_state.get("necessary", "") == "true"
assert consent_state.get("necessary", "") == "unset"


@pytest.mark.django_db
Expand Down