Skip to content

Commit

Permalink
Merge pull request #10 from nationalarchives/feature/wagtail-cookie-p…
Browse files Browse the repository at this point in the history
…ages

Add cookie pages for Wagtail use
  • Loading branch information
ahosgood authored Aug 29, 2024
2 parents afb8997 + a3d9ede commit 64694cd
Show file tree
Hide file tree
Showing 12 changed files with 130 additions and 276 deletions.
2 changes: 1 addition & 1 deletion app/lib/template_filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
125 changes: 46 additions & 79 deletions app/main/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"""<p>Cookies are files saved on your phone, tablet or computer when you visit a website.</p>
<p>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.</p>
<p>This page has a brief explanation of each type of cookie we use. If you want more details, <a href="{url_for('main.cookie_details')}">our detailed cookie information</a>.</p>""",
"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")
Expand Down
4 changes: 2 additions & 2 deletions app/templates/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -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) }}
Expand Down Expand Up @@ -266,7 +266,7 @@
},
{
'text': 'Cookies',
'href': url_for('main.cookies')
'href': '/cookies/'
}
],
'classes': 'tna-!--margin-top-l'
Expand Down
164 changes: 0 additions & 164 deletions app/templates/main/cookie-details.html

This file was deleted.

26 changes: 8 additions & 18 deletions app/templates/main/cookies.html
Original file line number Diff line number Diff line change
@@ -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() }}
<link rel="stylesheet" href="{{ url_for('static', filename='forms.css') }}" media="screen,print">
{% endblock %}

{% set breadcrumbs = [
{
'text': 'Home',
'href': '/'
}
] %}

{% block content %}
<form method="post" action="{{ url_for('main.cookies') }}" id="cookie-settings">
<div class="tna-aside tna-accent-green tna-background-accent" role="alert" id="cookie-settings-success"{%- if request.args.saved == 'true' %} tabindex="-1" autofocus{% else %} hidden{%- endif %}>
<form method="post" action="{{ url_for('main.set_cookies') }}" id="cookie-settings" class="tna-!--padding-top-l">
<div class="tna-aside tna-accent-green tna-background-accent" role="alert" id="cookie-settings-success"{% if request.args.saved == 'true' %} tabindex="0" autofocus{% else %} hidden{% endif %}>
<h2 class="tna-heading-l">Success</h2>
<p>Your cookie settings were saved.</p>
{%- if request.args.referrer and request.args.referrer != request.base_url %}
Expand All @@ -34,7 +20,10 @@ <h2 class="tna-heading-l">Success</h2>
{%- endif %}
</div>
<h2 class="tna-heading-l">Cookie settings</h2>
<p>We use three types of cookie. You can choose which cookies you’re happy for us to use.</p>
<p>This page has a brief explanation of each of the four types of cookie The National Archives uses. You can choose which cookies you’re happy for us to use.</p>
{%- if details_page %}
<p>If you want more details, see <a href="{{ details_page.url }}">our detailed cookie information</a>.</p>
{%- endif %}
{{ tnaRadios({
'label': 'Cookies that measure website use',
'headingLevel': 3,
Expand Down Expand Up @@ -95,7 +84,8 @@ <h2 class="tna-heading-l">Cookie settings</h2>
<h3 class="tna-heading-m">Strictly necessary cookies</h3>
<p>These essential cookies do things like allow you to remain logged in to some of our services.</p>
<p>They always need to be on.</p>
<input type="hidden" name="referrer" value="{{ request.referrer }}">
{{ page_data }}
<input type="hidden" name="referrer" value="{{ page_data.meta.html_url }}">
<div class="tna-button-group">
{{ tnaButton({
'text': 'Save changes',
Expand Down
2 changes: 2 additions & 0 deletions app/wagtail/pages/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
from .blog_post_page import blog_post_page
from .categories_page import categories_page
from .category_index_page import category_index_page
from .cookie_details_page import cookie_details_page
from .cookies_page import cookies_page
from .explore_index_page import explore_index_page
from .general_page import general_page
from .highlight_gallery_page import highlight_gallery_page
Expand Down
Loading

0 comments on commit 64694cd

Please sign in to comment.