Skip to content

Commit

Permalink
Merge branch 'main' into TP1-206-11455-pni_get_bg_home_page
Browse files Browse the repository at this point in the history
  • Loading branch information
mmmavis authored Sep 3, 2024
2 parents ffa3503 + f1ede5b commit ad79139
Show file tree
Hide file tree
Showing 13 changed files with 177 additions and 271 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{% load static %}

<div class="tw-w-max">
<a href="{{ link_href }}" class="tw-flex tw-items-center tw-font-sans tw-text-base tw-font-bold tw-text-blue-80 tw-leading-5 tw-no-underline hover:tw-underline {{ extra_classes }}">
{{ link_text }}
<a href="{{ link.url }}" {% if link.new_window %}target="_blank"{% endif %} class="tw-flex tw-items-center tw-font-sans tw-text-base tw-font-bold tw-text-blue-80 tw-leading-5 tw-no-underline hover:tw-underline {{ extra_classes }}">
{{ link.label }}
<img src="{% static "_images/buyers-guide/arrow-right.svg" %}" class="tw-ml-2 " alt="">
</a>
</div>
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,11 @@
</div>
{% endif %}

{% if link_text and link_href %}
<div class="tw-mt-16">
{% include "./arrow_link.html" with link_href=link_href link_text=link_text %}
</div>
{% if link %}
{% with link=link.0.value %}
<div class="tw-mt-16">
{% include "./arrow_link.html" with link=link %}
</div>
{% endwith %}
{% endif %}
</div>
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@
data-show-for-categories="{% for category in categories %}{% if category.show_cta %}{{ category.name }}, {% endif %}{% endfor %}"
>
{% with cta=featured_cta %}
{% include "fragments/buyersguide/call_to_action_box.html" with icon=cta.sticker_image heading=cta.title body=cta.content link_text=cta.link_label link_href=cta.get_target_url large=True %}
{% include "fragments/buyersguide/call_to_action_box.html" with icon=cta.sticker_image heading=cta.title body=cta.content link=cta.link large=True %}
{% endwith %}
</div>
{% endif %}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ <h1>{{ page.title }}</h1>
<aside class="tw-flex tw-flex-col medium:tw-flex-row tw-gap-16 tw-py-12">
{% if cta %}
<div class="tw-basis-1/2">
{% include "fragments/buyersguide/call_to_action_box.html" with icon=cta.sticker_image heading=cta.title body=cta.content link_text=cta.link_label link_href=cta.get_target_url %}
{% include "fragments/buyersguide/call_to_action_box.html" with icon=cta.sticker_image heading=cta.title body=cta.content link=cta.link %}
</div>
{% endif %}

Expand All @@ -62,7 +62,7 @@ <h1>{{ page.title }}</h1>
{% if cta or related_articles %}
<aside class="tw-hidden large:tw-flex tw-col-span-4 tw-flex-col tw-gap-16">
{% if cta %}
{% include "fragments/buyersguide/call_to_action_box.html" with icon=cta.sticker_image heading=cta.title body=cta.content link_text=cta.link_label link_href=cta.get_target_url %}
{% include "fragments/buyersguide/call_to_action_box.html" with icon=cta.sticker_image heading=cta.title body=cta.content link=cta.link %}
{% endif %}

{% if related_articles %}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@
{% with cta=featured_cta %}
{% if cta %}
<div class="{{ row_item_classes }}">
{% include "fragments/buyersguide/call_to_action_box.html" with icon=cta.sticker_image heading=cta.title body=cta.content link_text=cta.link_label link_href=cta.get_target_url %}
{% include "fragments/buyersguide/call_to_action_box.html" with icon=cta.sticker_image heading=cta.title body=cta.content link=cta.link %}
</div>
{% endif %}
{% endwith %}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ <h1 class="tw-h1-heading col-12">{{ product.title }}</h1>
">
{% with cta=featured_cta %}
{% if cta %}
{% include "fragments/buyersguide/call_to_action_box.html" with icon=cta.sticker_image heading=cta.title body=cta.content link_text=cta.link_label link_href=cta.get_target_url %}
{% include "fragments/buyersguide/call_to_action_box.html" with icon=cta.sticker_image heading=cta.title body=cta.content link=cta.link %}
{% endif %}
{% endwith %}

Expand Down
12 changes: 12 additions & 0 deletions network-api/networkapi/utility/faker/streamfield_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -731,6 +731,17 @@ def generate_app_install_download_button_field():
)


def generate_bg_cta_link_field():
link_value = {
"link_to": "external_url",
"external_url": fake.url(schemes=["https"]),
"label": " ".join(fake.words(nb=2)),
"new_window": True,
}

return generate_field("link", link_value)


class StreamfieldProvider(BaseProvider):
"""
A custom Faker Provider for relative image urls, for use with factory_boy
Expand Down Expand Up @@ -792,6 +803,7 @@ def streamfield(self, fields=None):
"newsletter_signup": generate_newsletter_signup_with_background_field,
"mixed_content": generate_mixed_content_field,
"app_install_download_button": generate_app_install_download_button_field,
"buyersguide_call_to_action_link": generate_bg_cta_link_field,
}

streamfield_data = []
Expand Down
3 changes: 1 addition & 2 deletions network-api/networkapi/wagtailpages/factory/buyersguide.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,7 @@ class Meta:

title = Faker("sentence", nb_words=7, variable_nb_words=True)
content = Faker("paragraph", nb_sentences=3, variable_nb_sentences=True)
link_label = Faker("sentence", nb_words=2)
link_target_url = Faker("url")
link = Faker("streamfield", fields=["buyersguide_call_to_action_link"])


class ProductUpdateFactory(DjangoModelFactory):
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
# Generated by Django 4.2.14 on 2024-08-28 01:05

import wagtail.blocks
import wagtail.documents.blocks
import wagtail.fields
from django.db import migrations

import networkapi.wagtailpages.validators


def migrate_link_field(apps, schema_editor):
BuyersGuideCallToAction = apps.get_model("wagtailpages", "BuyersGuideCallToAction")
for instance in BuyersGuideCallToAction.objects.all():

stream_data = []

if instance.link_label and (instance.link_target_url or instance.link_target_page):
if instance.link_target_url:
link_value = {
"link_to": "external_url",
"external_url": instance.link_target_url,
"label": instance.link_label,
"new_window": False,
}
stream_data = [("link", link_value)]

elif instance.link_target_page:
link_value = {
"link_to": "page",
"label": instance.link_label,
"new_window": False,
"page": instance.link_target_page,
}
stream_data = [("link", link_value)]

instance.link = stream_data
instance.save()


class Migration(migrations.Migration):

dependencies = [
("wagtailpages", "0158_alter_blogpage_body_and_more"),
]

operations = [
migrations.AddField(
model_name="buyersguidecalltoaction",
name="link",
field=wagtail.fields.StreamField(
[
(
"link",
wagtail.blocks.StructBlock(
[
("label", wagtail.blocks.CharBlock()),
(
"link_to",
wagtail.blocks.ChoiceBlock(
choices=[
("page", "Page"),
("external_url", "External URL"),
("relative_url", "Relative URL"),
("email", "Email"),
("anchor", "Anchor"),
("file", "File"),
("phone", "Phone"),
],
label="Link to",
),
),
("page", wagtail.blocks.PageChooserBlock(label="Page", required=False)),
(
"external_url",
wagtail.blocks.URLBlock(
help_text="Enter a full URL including http:// or https://",
label="External URL",
max_length=300,
required=False,
),
),
(
"relative_url",
wagtail.blocks.CharBlock(
help_text='A path relative to this domain. For example, "/foo/bar" or "?foo=bar".',
label="Relative URL",
max_length=300,
required=False,
validators=[networkapi.wagtailpages.validators.RelativeURLValidator()],
),
),
(
"anchor",
wagtail.blocks.CharBlock(
help_text='An id attribute of an element on the current page. For example, "#section-1"',
label="#",
max_length=300,
required=False,
validators=[networkapi.wagtailpages.validators.AnchorLinkValidator()],
),
),
("email", wagtail.blocks.EmailBlock(required=False)),
("file", wagtail.documents.blocks.DocumentChooserBlock(label="File", required=False)),
("phone", wagtail.blocks.CharBlock(label="Phone", max_length=30, required=False)),
(
"new_window",
wagtail.blocks.BooleanBlock(label="Open in new window", required=False),
),
]
),
)
],
blank=True,
use_json_field=True,
),
),
migrations.RunPython(migrate_link_field),
migrations.RemoveField(
model_name="buyersguidecalltoaction",
name="link_label",
),
migrations.RemoveField(
model_name="buyersguidecalltoaction",
name="link_target_page",
),
migrations.RemoveField(
model_name="buyersguidecalltoaction",
name="link_target_url",
),
]
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
from django.core.exceptions import ValidationError
from django.core.validators import RegexValidator
from django.db import models
from django.forms.utils import ErrorList
from wagtail.admin.panels import FieldPanel, MultiFieldPanel
from wagtail.fields import RichTextField
from wagtail.models import Page, TranslatableMixin
from wagtail.fields import RichTextField, StreamField
from wagtail.models import TranslatableMixin
from wagtail.search import index
from wagtail_localize.fields import SynchronizedField, TranslatableField

from networkapi.wagtailpages.constants import url_or_query_regex
from networkapi.wagtailpages.pagemodels.customblocks import LinkBlock
from networkapi.wagtailpages.pagemodels.customblocks.base_rich_text_options import (
base_rich_text_options,
)
Expand All @@ -30,26 +27,11 @@ class BuyersGuideCallToAction(index.Indexed, TranslatableMixin, models.Model):
)
title = models.CharField(max_length=200)
content = RichTextField(features=base_rich_text_options, blank=True)
link_label = models.CharField(max_length=2048, blank=True)
link_target_url = models.CharField(
max_length=255,
link = StreamField(
[("link", LinkBlock())],
use_json_field=True,
blank=True,
validators=[
RegexValidator(
regex=url_or_query_regex,
message=(
"Please enter a valid URL (starting with http:// or https://), "
"or a valid query string starting with ? (Ex: ?form=donate)."
),
),
],
)
link_target_page = models.ForeignKey(
Page,
null=True,
blank=True,
on_delete=models.SET_NULL,
related_name="cta_link_page",
max_num=1,
)

panels = [
Expand All @@ -58,9 +40,7 @@ class BuyersGuideCallToAction(index.Indexed, TranslatableMixin, models.Model):
FieldPanel("content"),
MultiFieldPanel(
[
FieldPanel("link_label"),
FieldPanel("link_target_url"),
FieldPanel("link_target_page"),
FieldPanel("link"),
],
heading="Call To Action Link",
),
Expand All @@ -70,9 +50,7 @@ class BuyersGuideCallToAction(index.Indexed, TranslatableMixin, models.Model):
SynchronizedField("sticker_image"),
TranslatableField("title"),
TranslatableField("content"),
TranslatableField("link_label"),
TranslatableField("link_target_url"),
TranslatableField("link_target_page"),
TranslatableField("link"),
]

search_fields = [
Expand All @@ -90,26 +68,14 @@ class Meta(TranslatableMixin.Meta):
def __str__(self):
return self.title

def get_target_url(self):
if self.link_target_url:
return self.link_target_url
else:
return self.link_target_page.url

def clean(self):
errors = {}
duplicate_link_target_error = ErrorList(["Please select a page OR enter a URL for the link (choose one)"])
# If user enters both link URL and link page:
if self.link_label and (self.link_target_url and self.link_target_page):
errors["link_target_url"] = duplicate_link_target_error
errors["link_target_page"] = duplicate_link_target_error
# If user enters link label but no page or URL to link to:
elif self.link_label and (not self.link_target_page and not self.link_target_url):
errors["link_target_url"] = duplicate_link_target_error
errors["link_target_page"] = duplicate_link_target_error
# If user does not enter a label but has set link URL or page.
elif not self.link_label and (self.link_target_page or self.link_target_url):
errors["link_label"] = ErrorList(["Please enter a label for the link"])
def link_label(self):
if self.link:
link_block = self.link[0]
return link_block.value["label"]
return ""

if errors:
raise ValidationError(errors)
def link_url(self):
if self.link:
link_block = self.link[0]
return link_block.value.url
return ""
Loading

0 comments on commit ad79139

Please sign in to comment.