Skip to content
Merged
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
33 changes: 32 additions & 1 deletion bedrock/anonym/blocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
from wagtail_link_block.blocks import LinkBlock
from wagtail_thumbnail_choice_block import ThumbnailChoiceBlock

from bedrock.cms.blocks import UUIDBlock

BASIC_TEXT_FEATURES = [
"bold",
"italic",
Expand Down Expand Up @@ -401,7 +403,23 @@ class Meta:
icon = "doc-full"


class AnalyticsSettings(blocks.StructBlock):
analytics_id = UUIDBlock(
label="Analytics ID",
help_text="Unique identifier for analytics tracking. Leave blank to auto-generate.",
required=False,
)

class Meta:
icon = "cog"
collapsed = True
label = "Settings"
label_format = "Analytics ID: {analytics_id}"
form_classname = "compact-form struct-block"


class LinkWithTextBlock(blocks.StructBlock):
settings = AnalyticsSettings()
label = blocks.CharBlock(label="Link Text")
link = LinkBlock()

Expand Down Expand Up @@ -456,6 +474,19 @@ class Meta:
icon = "doc-full"


class CaseStudyItemWithAnalyticsBlock(blocks.StructBlock):
page = PageChooserBlock("anonym.AnonymCaseStudyItemPage")
analytics_id = UUIDBlock(
label="Analytics ID",
help_text="Unique identifier for analytics tracking. Leave blank to auto-generate.",
required=False,
)

class Meta:
label = "Case Study"
label_format = "Case Study - {page}"


class CaseStudyListBlock(blocks.StructBlock):
"""
Display a list of case study items with their logos, client names, and descriptions.
Expand All @@ -465,7 +496,7 @@ class CaseStudyListBlock(blocks.StructBlock):
"""

case_study_items = blocks.ListBlock(
PageChooserBlock("anonym.AnonymCaseStudyItemPage"),
CaseStudyItemWithAnalyticsBlock(),
min_num=1,
max_num=3,
default=[],
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at https://mozilla.org/MPL/2.0/.

import copy
from uuid import uuid4

from django.core.management.base import BaseCommand

from bedrock.anonym.models import AnonymContentSubPage, AnonymIndexPage


class Command(BaseCommand):
help = "Restructures CaseStudyListBlock items from int PKs to {page, analytics_id} structs (idempotent)"

def handle(self, *args, **options):
page_models = [AnonymIndexPage, AnonymContentSubPage]
total = 0
for Model in page_models:
for page in Model.objects.all():
if not page.content:
continue
content = copy.deepcopy(list(page.content.raw_data))
changed = False
for block in content:
if block.get("type") != "section":
continue
for inner_block in block.get("value", {}).get("section_content", []):
if inner_block.get("type") != "case_study_item_list_block":
continue
for item in inner_block.get("value", {}).get("case_study_items", []):
item_val = item.get("value")
if isinstance(item_val, int):
# If the item is a bare PK, set a new object as the value
item["value"] = {"page": item_val, "analytics_id": str(uuid4())}
changed = True
elif isinstance(item_val, dict) and not item_val.get("analytics_id"):
# If the item has an empty analytics_id, fill it in
item_val["analytics_id"] = str(uuid4())
changed = True
if changed:
page.content = content
page.save(update_fields=["content"])
total += 1
self.stdout.write(self.style.SUCCESS(f"Updated {total} page(s)."))
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at https://mozilla.org/MPL/2.0/.

import copy
from uuid import uuid4

from django.core.management.base import BaseCommand

from bedrock.anonym.models import (
AnonymCaseStudyItemPage,
AnonymContentSubPage,
AnonymIndexPage,
AnonymNewsItemPage,
)


class Command(BaseCommand):
help = "Injects settings.analytics_id into LinkWithTextBlock instances that are missing it (idempotent)"

def handle(self, *args, **options):
page_models = [
AnonymIndexPage,
AnonymContentSubPage,
AnonymNewsItemPage,
AnonymCaseStudyItemPage,
]
total = 0
for Model in page_models:
for page in Model.objects.all():
if not page.content:
continue
content = copy.deepcopy(list(page.content.raw_data))
changed = False
for block in content:
block_type = block.get("type")
value = block.get("value", {})
if block_type == "section":
for link_item in value.get("action", []):
link_val = link_item.get("value", link_item)
settings = link_val.setdefault("settings", {})
if not settings.get("analytics_id"):
settings["analytics_id"] = str(uuid4())
changed = True
elif block_type == "call_to_action":
for link_item in value.get("button", []):
link_val = link_item.get("value", link_item)
settings = link_val.setdefault("settings", {})
if not settings.get("analytics_id"):
settings["analytics_id"] = str(uuid4())
changed = True
if changed:
page.content = content
page.save(update_fields=["content"])
total += 1
self.stdout.write(self.style.SUCCESS(f"Updated {total} page(s)."))
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at https://mozilla.org/MPL/2.0/.

import copy
from uuid import uuid4

from django.core.management.base import BaseCommand

from bedrock.anonym.models import AnonymIndexPage


class Command(BaseCommand):
help = "Injects analytics_id into NavigationLinkBlock items with has_button_appearance=True that are missing it (idempotent)"

def handle(self, *args, **options):
total = 0
for page in AnonymIndexPage.objects.all():
if not page.navigation:
continue
navigation = copy.deepcopy(list(page.navigation.raw_data))
changed = False
for item in navigation:
value = item.get("value", {})
if value.get("has_button_appearance") and not value.get("analytics_id"):
value["analytics_id"] = str(uuid4())
changed = True
if changed:
page.navigation = navigation
page.save(update_fields=["navigation"])
total += 1
self.stdout.write(self.style.SUCCESS(f"Updated {total} page(s)."))
Loading
Loading