From a0669824a52a045fc96d0264b6108723a7368287 Mon Sep 17 00:00:00 2001 From: Andrew Hosgood Date: Wed, 28 Aug 2024 15:47:05 +0100 Subject: [PATCH 1/2] Add cookie pages for Wagtail use --- app/lib/template_filters.py | 2 +- app/main/routes.py | 125 +++++++---------- app/templates/base.html | 4 +- app/templates/main/cookie-details.html | 164 ----------------------- app/templates/main/cookies.html | 26 ++-- app/wagtail/pages/__init__.py | 2 + app/wagtail/pages/cookie_details_page.py | 11 ++ app/wagtail/pages/cookies_page.py | 25 ++++ app/wagtail/render.py | 4 + src/scripts/cookies.js | 29 ++-- src/styles/forms.scss | 10 ++ 11 files changed, 128 insertions(+), 274 deletions(-) delete mode 100644 app/templates/main/cookie-details.html create mode 100644 app/wagtail/pages/cookie_details_page.py create mode 100644 app/wagtail/pages/cookies_page.py diff --git a/app/lib/template_filters.py b/app/lib/template_filters.py index b4563f83..67a19817 100644 --- a/app/lib/template_filters.py +++ b/app/lib/template_filters.py @@ -205,7 +205,7 @@ def sidebar_items_from_wagtail_body(body): + item["id"], } ) - return page_sections + return page_sections or page_children def wagtail_table_parser(table_data): diff --git a/app/main/routes.py b/app/main/routes.py index e1639df1..9c1e1a9e 100644 --- a/app/main/routes.py +++ b/app/main/routes.py @@ -26,87 +26,54 @@ def browse(): return render_template("main/browse.html", global_alert=global_alerts()) -@bp.route("/cookies/", methods=["GET", "POST"]) -def cookies(): - if request.method == "POST": - current_cookies_policy = { - "usage": False, - "settings": False, - "marketing": False, - "essential": True, - } - if "cookies_policy" in request.cookies: - cookies_policy = request.cookies["cookies_policy"] - current_cookies_policy = json.loads(unquote(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"] - ) - marketing = ( - strtobool(request.form["marketing"]) - if "marketing" in request.form - else current_cookies_policy["marketing"] - ) - new_cookies_policy = { - "usage": usage, - "settings": settings, - "marketing": marketing, - "essential": True, - } - response = make_response( - redirect( - url_for( - "main.cookies", - saved="true", - referrer=( - request.form["referrer"] - if "referrer" in request.form - else "" - ), - ) - ) - ) - response.set_cookie( - "cookies_policy", - quote(json.dumps(new_cookies_policy, separators=(",", ":"))), - domain=current_app.config.get("COOKIE_DOMAIN"), - ) - response.set_cookie( - "cookie_preferences_set", - "true", - domain=current_app.config.get("COOKIE_DOMAIN"), - ) - if not usage: - for cookie in request.cookies: - if cookie.startswith("_ga"): - response.set_cookie(cookie, "", expires=0) - return response - global_alerts_data = global_alerts() - return render_template( - "main/cookies.html", - page_data={ - "title": "Cookies", - "intro": f"""

Cookies are files saved on your phone, tablet or computer when you visit a website.

-

We use cookies to collect and store information about how you use National Archives websites which means any page with nationalarchives.gov.uk in the URL.

-

This page has a brief explanation of each type of cookie we use. If you want more details, our detailed cookie information.

""", - "global_alert": global_alerts_data["global_alert"], - "mourning_notice": global_alerts_data["mourning_notice"], - }, +@bp.route("/cookies/set/", methods=["POST"]) +def set_cookies(): + current_cookies_policy = { + "usage": False, + "settings": False, + "marketing": False, + "essential": True, + } + if "cookies_policy" in request.cookies: + cookies_policy = request.cookies["cookies_policy"] + current_cookies_policy = json.loads(unquote(cookies_policy)) + usage = ( + strtobool(request.form["usage"]) + if "usage" in request.form + else current_cookies_policy["usage"] ) - - -@bp.route("/cookies/details/") -# @cache.cached(key_prefix=cache_key_prefix) -def cookie_details(): - return render_template( - "main/cookie-details.html", global_alert=global_alerts() + settings = ( + strtobool(request.form["settings"]) + if "settings" in request.form + else current_cookies_policy["settings"] + ) + marketing = ( + strtobool(request.form["marketing"]) + if "marketing" in request.form + else current_cookies_policy["marketing"] ) + new_cookies_policy = { + "usage": usage, + "settings": settings, + "marketing": marketing, + "essential": True, + } + response = make_response(redirect(f"{request.form['referrer']}?saved=true")) + response.set_cookie( + "cookies_policy", + quote(json.dumps(new_cookies_policy, separators=(",", ":"))), + domain=current_app.config.get("COOKIE_DOMAIN"), + ) + response.set_cookie( + "cookie_preferences_set", + "true", + domain=current_app.config.get("COOKIE_DOMAIN"), + ) + if not usage: + for cookie in request.cookies: + if cookie.startswith("_ga"): + response.set_cookie(cookie, "", expires=0) + return response @bp.route("/service-worker.min.js") diff --git a/app/templates/base.html b/app/templates/base.html index a726b963..54538ed6 100644 --- a/app/templates/base.html +++ b/app/templates/base.html @@ -59,7 +59,7 @@ {% if 'cookie_preferences_set' not in request.cookies or not request.cookies.get('cookie_preferences_set') %} {{ tnaCookieBanner({ 'serviceName': 'The National Archives', - 'cookiesUrl': url_for('main.cookies'), + 'cookiesUrl': '/cookies/', 'cookiesDomain': app_config.COOKIE_DOMAIN, 'policies': 'marketing' }) | indent(6) }} @@ -266,7 +266,7 @@ }, { 'text': 'Cookies', - 'href': url_for('main.cookies') + 'href': '/cookies/' } ], 'classes': 'tna-!--margin-top-l' diff --git a/app/templates/main/cookie-details.html b/app/templates/main/cookie-details.html deleted file mode 100644 index d9b72894..00000000 --- a/app/templates/main/cookie-details.html +++ /dev/null @@ -1,164 +0,0 @@ -{% extends 'layouts/page-with-contents-sidebar.html' %} - -{%- from 'components/breadcrumbs/macro.html' import tnaBreadcrumbs -%} -{%- from 'components/button/macro.html' import tnaButton -%} -{%- from 'macros/global_alert_banners.html' import global_alert_banners -%} - -{%- set themeAccent = 'black' if global_alert.mourning_notice else 'blue' -%} -{%- set pageTitle = 'Details about cookies' -%} - -{% block beforeContent %} -
-
- {{ tnaBreadcrumbs({ - 'items': [ - { - 'text': 'Home', - 'href': '/' - }, - { - 'text': 'Cookies', - 'href': url_for('main.cookies') - } - ], - 'structuredData': True - }) }} -
-
- {{ global_alert_banners(global_alert.global_alert, global_alert.mourning_notice) }} -{% endblock %} - -{% block contentHeader %} -

Details about our cookies

-

The National Archives put small files (known as ‘cookies’) onto your computer to collect information about how you browse the site. Find out more about the cookies we use, what they’re for and when they expire.

-{% endblock %} - -{% block content %} -

Cookies on our main site

-

Our main site is nationalarchives.gov.uk.

-
- - - - - - - - - - - - - - - - - - - - -
NamePurposeExpires
cookies_preferences_setLets us know that you’ve saved your cookie consent settings1 year
cookies_policySaves your cookie consent settings1 year
-
-

Cookies we use on our shop

-

Our shop site is shop.nationalarchives.gov.uk.

-
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
NamePurposeExpires
_abThis is used in connection with access to admin.2 years
_secure_session_idThis is used in connection with navigation through a storefront.24 hours
_shopify_countryThis is used in connection with the checkout.Expires when you exit the browser
_shopify_mThis is used for managing customer privacy settings.1 year
_shopify_tmThis is used for managing customer privacy settings.30 minutes
_shopify_twThis is used for managing customer privacy settings.2 weeks
_storefront_uThis is used to facilitate updating customer account information.1 minute
_tracking_consentThis is used for tracking preferences.1 year
CartThis is used in connection with the shopping cart.2 weeks
cart_currencyThis is used in connection with the shopping cart.2 weeks
cart_sigThis is used in connection with the checkout.2 weeks
cart_tsThis is used in connection with the checkout.2 weeks
cart_verThis is used in connection with the shopping cart.2 weeks
checkoutThis is used in connection with the checkout.4 weeks
checkout_tokenThis is used in connection with the checkout.1 year
_atuvcThis (communication and marketing) cookie is associated with the AddThis social sharing widget; it stores an updated page share count.1 year
_atuvsThis (communication and marketing) cookie is associated with the AddThis social sharing widget; it serves a similar purpose to other cookies set by the service.1 month
-
-

Change your settings

-

You can change which cookies you’re happy for us to use.

-{% endblock %} diff --git a/app/templates/main/cookies.html b/app/templates/main/cookies.html index f540a583..13ec0443 100644 --- a/app/templates/main/cookies.html +++ b/app/templates/main/cookies.html @@ -1,30 +1,16 @@ {% extends 'main/general.html' %} -{%- from 'components/breadcrumbs/macro.html' import tnaBreadcrumbs -%} {%- from 'components/button/macro.html' import tnaButton -%} {%- from 'components/radios/macro.html' import tnaRadios -%} -{%- from 'components/sidebar/macro.html' import tnaSidebar -%} -{%- from 'macros/global_alert_banners.html' import global_alert_banners -%} - -{%- set themeAccent = 'black' if page_data.global_alert.mourning_notice else 'blue' -%} -{%- set showSidebar = True -%} -{%- set sidebarPositionRight = True -%} {% block stylesheets %} {{ super() }} {% endblock %} -{% set breadcrumbs = [ - { - 'text': 'Home', - 'href': '/' - } -] %} - {% block content %} -