From db3bcbc87af68198e5426762cd98724b82e46847 Mon Sep 17 00:00:00 2001 From: Andrew Hosgood Date: Wed, 12 Jun 2024 17:58:36 +0100 Subject: [PATCH] Add basic cookie preference page --- app/legal/routes.py | 37 +++++++++++- app/templates/legal/cookies.html | 99 ++++++++++++++++---------------- 2 files changed, 85 insertions(+), 51 deletions(-) diff --git a/app/legal/routes.py b/app/legal/routes.py index dcc56e4b..42e1c7ec 100644 --- a/app/legal/routes.py +++ b/app/legal/routes.py @@ -1,6 +1,10 @@ +import json +from urllib.parse import quote, unquote + from app.legal import bp from app.lib import cache, cache_key_prefix -from flask import render_template +from app.lib.util import strtobool +from flask import make_response, render_template, request @bp.route("/") @@ -9,9 +13,36 @@ def index(): return render_template("legal/index.html") -@bp.route("/cookies/") -@cache.cached(key_prefix=cache_key_prefix) +@bp.route("/cookies/", methods=["GET", "POST"]) def cookies(): + if request.method == "POST": + current_cookies_policy = { + "usage": False, + "settings": False, + "essential": True, + } + if "cookies_policy" in request.cookies: + cookies_policy = request.cookies["cookies_policy"] + current_cookies_policy = json.loads(unquote(cookies_policy)) + new_cookies_policy = { + "usage": ( + strtobool(request.form["usage"]) + if "usage" in request.form + else current_cookies_policy["usage"] + ), + "settings": ( + strtobool(request.form["settings"]) + if "settings" in request.form + else current_cookies_policy["settings"] + ), + "essential": True, + } + response = make_response(render_template("legal/cookies.html")) + response.set_cookie( + "cookies_policy", + quote(json.dumps(new_cookies_policy, separators=(",", ":"))), + ) + return response return render_template("legal/cookies.html") diff --git a/app/templates/legal/cookies.html b/app/templates/legal/cookies.html index 130d0868..a3e83d1b 100644 --- a/app/templates/legal/cookies.html +++ b/app/templates/legal/cookies.html @@ -43,55 +43,58 @@

Cookies

-

Cookie settings

-

We use three types of cookie. You can choose which cookies you’re happy for us to use.

- {{ tnaRadios({ - 'label': 'Cookies that measure website use', - 'headingLevel': 3, - 'headingSize': 'm', - 'hintHtml': '

We use Google Analytics cookies to measure how you use National Archives websites.

These cookies collect information about:

  • how you got to these sites
  • the pages you visit and how long you spend on each page
  • what you click on while you’re visiting these sites

We do not allow Google to use or share this data for their own purposes.

', - 'id': 'usage', - 'name': 'usage', - 'items': [ - { - 'text': 'Use cookies that measure my website use', - 'value': 'accept' - }, - { - 'text': 'Do not use cookies that measure my website use', - 'value': 'decline' - } - ], - 'selected': 'accept' if cookie_preference('usage') else 'decline' - }) }} - {{ tnaRadios({ - 'label': 'Cookies that remember your settings', - 'headingLevel': 3, - 'headingSize': 'm', - 'hint': 'These cookies do things like remember your preferences and the choices you make, to personalise your experience of using the site.', - 'id': 'settings', - 'name': 'settings', - 'items': [ - { - 'text': 'Use cookies that remember my settings on the site', - 'value': 'accept' - }, - { - 'text': 'Do not use cookies that remember my settings on the site', - 'value': 'decline' - } - ], - 'selected': 'accept' if cookie_preference('settings') else 'decline' - }) }} -

Strictly necessary cookies

-

These essential cookies do things like allow you to remain logged in to some of our services.

-

They always need to be on.

-
- {{ tnaButton({ - 'text': 'Save changes', - 'buttonElement': True +
+

Cookie settings

+

We use three types of cookie. You can choose which cookies you’re happy for us to use.

+ {{ tnaRadios({ + 'label': 'Cookies that measure website use', + 'headingLevel': 3, + 'headingSize': 'm', + 'hintHtml': '

We use Google Analytics cookies to measure how you use National Archives websites.

These cookies collect information about:

  • how you got to these sites
  • the pages you visit and how long you spend on each page
  • what you click on while you’re visiting these sites

We do not allow Google to use or share this data for their own purposes.

', + 'id': 'usage', + 'name': 'usage', + 'items': [ + { + 'text': 'Use cookies that measure my website use', + 'value': 'true' + }, + { + 'text': 'Do not use cookies that measure my website use', + 'value': 'false' + } + ], + 'selected': 'true' if cookie_preference('usage') else 'false' }) }} -
+ {{ tnaRadios({ + 'label': 'Cookies that remember your settings', + 'headingLevel': 3, + 'headingSize': 'm', + 'hint': 'These cookies do things like remember your preferences and the choices you make, to personalise your experience of using the site.', + 'id': 'settings', + 'name': 'settings', + 'items': [ + { + 'text': 'Use cookies that remember my settings on the site', + 'value': 'true' + }, + { + 'text': 'Do not use cookies that remember my settings on the site', + 'value': 'false' + } + ], + 'selected': 'true' if cookie_preference('settings') else 'false' + }) }} +

Strictly necessary cookies

+

These essential cookies do things like allow you to remain logged in to some of our services.

+

They always need to be on.

+
+ {{ tnaButton({ + 'text': 'Save changes', + 'buttonElement': True, + 'buttonType': 'submit' + }) }} +
+