-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
780e6bd
commit 68c8b9b
Showing
13 changed files
with
326 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
from datetime import datetime, timedelta | ||
from urllib.parse import quote | ||
|
||
from django.conf import settings | ||
|
||
|
||
class SetDefaultCookiePreferencesMiddleware: | ||
def __init__(self, get_response): | ||
self.get_response = get_response | ||
|
||
def __call__(self, request): | ||
response = self.get_response(request) | ||
if not settings.FEATURE_COOKIE_BANNER_ENABLED: | ||
return response | ||
cookie_name = "cookies_policy" | ||
if not request.COOKIES.get(cookie_name, None): | ||
expires = datetime.utcnow() + timedelta(days=90) | ||
value = '{"usage":false,"settings":false,"essential":true}' | ||
response.set_cookie( | ||
cookie_name, | ||
expires=expires, | ||
path="/", | ||
domain=settings.COOKIE_DOMAIN, | ||
value=quote(value), | ||
) | ||
return response |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import json | ||
|
||
from json.decoder import JSONDecodeError | ||
from typing import Union | ||
from urllib.parse import unquote | ||
|
||
from django import template | ||
|
||
register = template.Library() | ||
|
||
|
||
@register.filter | ||
def cookie_use_permitted(value: Union[str, None]) -> bool: | ||
""" | ||
Return the True/False based on cookie usage value | ||
if no cookie set it will return False | ||
""" | ||
usage = False | ||
|
||
try: | ||
if value is not None: | ||
cookie_str = unquote(value) | ||
usage = json.loads(cookie_str)["usage"] | ||
except ( | ||
JSONDecodeError, # invalid json | ||
TypeError, # decoded json isn't a dict | ||
KeyError, # dict doesn't contain 'usage' | ||
ValueError, # 'usage' value cannot be converted to boolean | ||
): | ||
usage = False | ||
|
||
return usage |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
from django.test import SimpleTestCase | ||
|
||
from etna.core.templatetags.cookie_tags import cookie_use_permitted | ||
|
||
|
||
class TestCookieUsePermittedTag(SimpleTestCase): | ||
|
||
empty_cookie = "" | ||
none_cookie = None | ||
json_usage_true_cookie = '{"usage":true,"settings":false,"essential":true}' | ||
json_unicoded_usage_true_cookie = ( | ||
"%7b%22usage%22%3atrue%2c%22settings%22%3afalse%2c%22essential%22%3atrue%7d" | ||
) | ||
json_usage_false_cookie = '{"usage":false,"settings":false,"essential":true}' | ||
invalid_json_cookie = "NOT_JSON" | ||
unexpected_json_format = '["item_one", "item_two"]' | ||
incorrect_bool_cookie = '{"usage":fase,"settings":false,"essential":true}' | ||
|
||
def test_default(self): | ||
for attribute_name, expected_result in ( | ||
("empty_cookie", False), | ||
("none_cookie", False), | ||
("json_usage_true_cookie", True), | ||
("json_unicoded_usage_true_cookie", True), | ||
("json_usage_false_cookie", False), | ||
("invalid_json_cookie", False), | ||
("unexpected_json_format", False), | ||
("incorrect_bool_cookie", False), | ||
): | ||
with self.subTest(attribute_name): | ||
source = getattr(self, attribute_name) | ||
|
||
self.assertEqual(cookie_use_permitted(source), expected_result) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
#ds-cookie-consent-banner { | ||
.container { | ||
.row { | ||
display: block; | ||
flex-wrap: nowrap; | ||
} | ||
|
||
.cookie_head { | ||
font-family: $font__roboto !important; | ||
} | ||
|
||
.button { | ||
font-family: $font__open-sans !important; | ||
|
||
&:hover { | ||
cursor: pointer; | ||
} | ||
} | ||
|
||
#btn_preferences { | ||
color: $color__white; | ||
padding: 0.5rem; | ||
&:hover { | ||
transition: none; | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<div id="ds-cookie-consent-banner" class="cookieConsent" role="region" aria-label="Cookie banner"> | ||
<div class="container" role="region" aria-label="Cookies on The National Archives"> | ||
<div class="row"> | ||
<p class="cookie_head">This website uses cookies</p> | ||
<p class="cookie-p"> | ||
We place some essential cookies on your device to make this website work. <br><br> | ||
We'd like to use additional cookies to remember your settings and understand how you use our services. <br><br> | ||
This information will help us make improvements to the website. | ||
</p> | ||
<a href="https://www.nationalarchives.gov.uk/legal/cookies/" id="btn_preferences" class="button">Set cookie preferences</a> | ||
</div> | ||
</div> | ||
</div> |
Oops, something went wrong.