-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add featured series, explore more, related articles/methodologies * Exclude local settings from mypy * Add toc, featured article and processed articles/methodologies * Translate missed string in template * Add component for featured article * Add a label/release date properties to relevant page models * Update the topic template * Wire in the Explore More block * Handle highlighted articles/methodologies * Add tests * Tweak the featured and highlight choosers to relevant pages only * Tweak so related articles display the full title (with series prefix) * Add basic functional tests
- Loading branch information
Showing
32 changed files
with
1,472 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
from datetime import datetime | ||
|
||
from django.test import TestCase | ||
from django.utils.formats import date_format | ||
|
||
from cms.core.models.base import BasePage | ||
from cms.core.utils import get_formatted_pages_list | ||
|
||
|
||
# DummyPage mimics the minimum attributes and methods of a Wagtail Page. | ||
class DummyPage(BasePage): | ||
def __init__(self, title, summary="", listing_summary="", url="https://ons.gov.uk", **kwargs): # pylint: disable=super-init-not-called | ||
# this just set attributes manually. | ||
self.title = title | ||
self.summary = summary | ||
self.listing_summary = listing_summary | ||
self._url = url | ||
self._release_date = kwargs.get("release_date") | ||
|
||
def get_url(self, request=None, current_site=None): | ||
return self._url | ||
|
||
@property | ||
def release_date(self): | ||
return self._release_date | ||
|
||
class Meta: | ||
abstract = True | ||
|
||
|
||
class DummyPageWithNoReleaseDate(DummyPage): | ||
label = "Dummy Page" | ||
|
||
class Meta: | ||
abstract = True | ||
|
||
|
||
class GetFormattedPagesListTests(TestCase): | ||
def test_without_release_date_and_listing_summary(self): | ||
# When no listing_summary and release_date, should use summary for description, | ||
# and use the default label. | ||
page = DummyPage(title="Test Page", summary="Test summary", listing_summary="") | ||
result = get_formatted_pages_list([page]) | ||
expected = { | ||
"title": {"text": "Test Page", "url": "https://ons.gov.uk"}, | ||
"metadata": {"object": {"text": "Page"}}, | ||
"description": "Test summary", | ||
} | ||
self.assertEqual(len(result), 1) | ||
self.assertDictEqual(result[0], expected) | ||
|
||
def test_with_listing_summary_overrides_summary(self): | ||
# When listing_summary is provided, that should be used as description. | ||
page = DummyPage(title="Test Page", summary="Test summary", listing_summary="Listing summary") | ||
result = get_formatted_pages_list([page]) | ||
expected = { | ||
"title": {"text": "Test Page", "url": "https://ons.gov.uk"}, | ||
"metadata": {"object": {"text": "Page"}}, | ||
"description": "Listing summary", | ||
} | ||
self.assertEqual(len(result), 1) | ||
self.assertDictEqual(result[0], expected) | ||
|
||
def test_with_custom_label(self): | ||
# When a custom label is defined, it should be used in metadata. | ||
page = DummyPageWithNoReleaseDate(title="Test Page", summary="Test summary", listing_summary="") | ||
result = get_formatted_pages_list([page]) | ||
expected = { | ||
"title": {"text": "Test Page", "url": "https://ons.gov.uk"}, | ||
"metadata": {"object": {"text": "Dummy Page"}}, | ||
"description": "Test summary", | ||
} | ||
self.assertEqual(len(result), 1) | ||
self.assertDictEqual(result[0], expected) | ||
|
||
def test_with_release_date(self): | ||
# When release_date is provided, metadata should include date formatting. | ||
test_date = datetime(2024, 1, 1, 12, 30) | ||
page = DummyPage(title="Test Page", summary="Test summary", listing_summary="", release_date=test_date) | ||
result = get_formatted_pages_list([page]) | ||
|
||
expected_iso = date_format(test_date, "c") | ||
expected_short = date_format(test_date, "DATE_FORMAT") | ||
|
||
expected = { | ||
"title": {"text": "Test Page", "url": "https://ons.gov.uk"}, | ||
"metadata": { | ||
"object": {"text": "Page"}, | ||
"date": { | ||
"prefix": "Released", | ||
"showPrefix": True, | ||
"iso": expected_iso, | ||
"short": expected_short, | ||
}, | ||
}, | ||
"description": "Test summary", | ||
} | ||
self.assertEqual(len(result), 1) | ||
self.assertDictEqual(result[0], expected) | ||
|
||
def test_multiple_pages(self): | ||
# Test processing multiple dummy pages | ||
test_date = datetime(2024, 1, 1, 12, 30) | ||
page1 = DummyPage(title="Page One", summary="Summary One", listing_summary="", release_date=test_date) | ||
page2 = DummyPageWithNoReleaseDate(title="Page Two", summary="Summary Two", listing_summary="Listing Two") | ||
pages = [page1, page2] | ||
result = get_formatted_pages_list(pages) | ||
|
||
expected_iso = date_format(test_date, "c") | ||
expected_short = date_format(test_date, "DATE_FORMAT") | ||
|
||
expected_page1 = { | ||
"title": {"text": "Page One", "url": "https://ons.gov.uk"}, | ||
"metadata": { | ||
"object": {"text": "Page"}, | ||
"date": { | ||
"prefix": "Released", | ||
"showPrefix": True, | ||
"iso": expected_iso, | ||
"short": expected_short, | ||
}, | ||
}, | ||
"description": "Summary One", | ||
} | ||
expected_page2 = { | ||
"title": {"text": "Page Two", "url": "https://ons.gov.uk"}, | ||
"metadata": {"object": {"text": "Dummy Page"}}, | ||
"description": "Listing Two", | ||
} | ||
self.assertEqual(len(result), 2) | ||
self.assertDictEqual(result[0], expected_page1) | ||
self.assertDictEqual(result[1], expected_page2) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
from typing import TYPE_CHECKING, Any, Optional, TypedDict | ||
|
||
from django.db.models import QuerySet | ||
from django.utils.formats import date_format | ||
from django.utils.translation import gettext_lazy as _ | ||
|
||
if TYPE_CHECKING: | ||
from django.http import HttpRequest | ||
from wagtail.models import Page | ||
|
||
|
||
class DocumentListItem(TypedDict): | ||
title: dict[str, str] | ||
metadata: dict[str, Any] | ||
description: str | ||
|
||
|
||
def get_formatted_pages_list( | ||
pages: list["Page"] | QuerySet["Page"], request: Optional["HttpRequest"] = None | ||
) -> list[DocumentListItem]: | ||
"""Returns a formatted list of page data for the documentList DS macro. | ||
See the search results section in https://service-manual.ons.gov.uk/design-system/components/document-list. | ||
""" | ||
data = [] | ||
for page in pages: | ||
datum: DocumentListItem = { | ||
"title": { | ||
"text": getattr(page, "display_title", page.title), | ||
"url": page.get_url(request=request), | ||
}, | ||
"metadata": { | ||
"object": {"text": getattr(page, "label", _("Page"))}, | ||
}, | ||
"description": getattr(page, "listing_summary", "") or getattr(page, "summary", ""), | ||
} | ||
if release_date := page.release_date: | ||
datum["metadata"]["date"] = { | ||
"prefix": _("Released"), | ||
"showPrefix": True, | ||
"iso": date_format(release_date, "c"), | ||
"short": date_format(release_date, "DATE_FORMAT"), | ||
} | ||
data.append(datum) | ||
return data |
26 changes: 26 additions & 0 deletions
26
cms/jinja2/templates/components/featured/featured-article.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<div class="featured-article"> | ||
<h3 class="ons-u-fs-m ons-u-mt-no ons-u-mb-2xs"> | ||
<a href="{{ pageurl(article) }}">{{ article.display_title }}</a> | ||
</h3> | ||
<ul class="featured-article__metadata"> | ||
<li class="featured-article__attribute ons-u-fs-s"> | ||
<span class="ons-u-fw-b">{{ _("Released") }}:</span> | ||
<time datetime="{{ article.release_date|date("c") }}">{{ article.release_date|date("DATE_FORMAT") }}</time> | ||
</li> | ||
<li class="featured-article__attribute ons-u-fs-s"> | ||
<span class="ons-u-fw-b">{{ _("Article") }}</span> | ||
</li> | ||
</ul> | ||
|
||
{% if article.listing_image %} | ||
<div class="featured-article__embed-container"> | ||
{{ image(article.listing_image, "width-1000") }} | ||
</div> | ||
{% endif %} | ||
|
||
{% if article.main_points_summary %} | ||
<div class="featured-article__description"> | ||
{{ article.main_points_summary|richtext }} | ||
</div> | ||
{% endif %} | ||
</div> |
2 changes: 2 additions & 0 deletions
2
cms/jinja2/templates/components/streamfield/explore_more_stream_block.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
{% from "components/document-list/_macro.njk" import onsDocumentList %} | ||
{{ onsDocumentList({"documents": formatted_items}) }} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.