From 70e920cd7cf5eb48ca190cf8f77141a3c43d1171 Mon Sep 17 00:00:00 2001 From: Alex Morega Date: Wed, 8 May 2024 00:40:47 +0300 Subject: [PATCH] feat: Composite title for topic & author feeds (#408) --- ietf/blog/feeds.py | 28 ++++++++++++++++++++++------ ietf/blog/models.py | 2 +- ietf/blog/tests.py | 36 +++++++++++++++++++++++------------- 3 files changed, 46 insertions(+), 20 deletions(-) diff --git a/ietf/blog/feeds.py b/ietf/blog/feeds.py index a80c9468..1275744e 100644 --- a/ietf/blog/feeds.py +++ b/ietf/blog/feeds.py @@ -1,5 +1,6 @@ from django.contrib.syndication.views import Feed from django.db.models.functions import Coalesce +from django.utils.functional import cached_property from wagtail.models import Site from ..blog.models import BlogPage @@ -9,10 +10,13 @@ class BlogFeed(Feed): link = "/blog/" + def get_title(self): + return self.feed_settings.blog_feed_title + def __call__(self, request, *args, **kwargs): - settings = FeedSettings.for_site(Site.find_for_request(request)) - self.title = settings.blog_feed_title - self.description = settings.blog_feed_description + self.feed_settings = FeedSettings.for_site(Site.find_for_request(request)) + self.title = self.get_title() + self.description = self.feed_settings.blog_feed_description return super().__call__(request, *args, **kwargs) def items(self): @@ -38,9 +42,15 @@ def item_pubdate(self, item): return item.date class TopicBlogFeed(BlogFeed): - def __call__(self, request, *args, **kwargs): - self.topic = kwargs.get('topic') - return super().__call__(request, *args, **kwargs) + def __init__(self, topic): + self.topic = topic + return super().__init__() + + def get_title(self): + title = super().get_title() + if title: + title = f"{title} – {self.topic}" + return title def items(self): return ( @@ -56,5 +66,11 @@ def __init__(self, person, queryset): self.queryset = queryset return super().__init__() + def get_title(self): + title = super().get_title() + if title: + title = f"{title} – {self.person.name}" + return title + def items(self): return self.queryset diff --git a/ietf/blog/models.py b/ietf/blog/models.py index 2c0a1a5d..e60c8a75 100644 --- a/ietf/blog/models.py +++ b/ietf/blog/models.py @@ -408,7 +408,7 @@ def feed_by_author(self, request, slug): def feed_with_topic(self, request, topic): from .feeds import TopicBlogFeed - return TopicBlogFeed()(request, topic=topic) + return TopicBlogFeed(topic=topic)(request) @route(r"^([-\w]+)/all/$") def filtered_entries(self, request, slug, *args, **kwargs): diff --git a/ietf/blog/tests.py b/ietf/blog/tests.py index e4bfdaf0..0e5beb7d 100644 --- a/ietf/blog/tests.py +++ b/ietf/blog/tests.py @@ -8,6 +8,7 @@ from ietf.snippets.factories import PersonFactory, TopicFactory from ietf.home.models import HomePage from ietf.snippets.models import Topic +from ietf.utils.models import FeedSettings from .factories import BlogIndexPageFactory, BlogPageFactory from .models import ( IESG_STATEMENT_TOPIC_ID, @@ -72,6 +73,11 @@ def set_up(self, home: HomePage, client: Client): BlogPageAuthor.objects.create(page=self.prev_blog_page, author=self.bob) BlogPageAuthor.objects.create(page=self.next_blog_page, author=self.bob) + self.feed_settings = FeedSettings.for_site(self.home.get_site()) + self.feed_settings.blog_feed_title = "Blog Feed Title" + self.feed_settings.blog_feed_description = "Blog Feed Description" + self.feed_settings.save() + def test_blog(self): index_response = self.client.get(path=self.blog_index.url) assert index_response.status_code == 200 @@ -111,27 +117,30 @@ def test_author_index(self): def test_blog_feed(self): response = self.client.get(path="/blog/feed/") assert response.status_code == 200 - html = response.content.decode() + feed = response.content.decode() - assert self.blog_page.url in html - assert self.other_blog_page.url in html + assert f"{self.feed_settings.blog_feed_title}" in feed + assert self.blog_page.url in feed + assert self.other_blog_page.url in feed def test_topic_feed(self): iab_response = self.client.get(path="/blog/iab/feed/") assert iab_response.status_code == 200 - iab_html = iab_response.content.decode() + iab_feed = iab_response.content.decode() - assert self.other_blog_page.url in iab_html - assert self.blog_page.url not in iab_html - assert self.next_blog_page.url not in iab_html + assert f"{self.feed_settings.blog_feed_title} – iab" in iab_feed + assert self.other_blog_page.url in iab_feed + assert self.blog_page.url not in iab_feed + assert self.next_blog_page.url not in iab_feed - ietf_response = self.client.get(path="/blog/iesg/feed/") - assert ietf_response.status_code == 200 - ietf_html = ietf_response.content.decode() + iesg_response = self.client.get(path="/blog/iesg/feed/") + assert iesg_response.status_code == 200 + iesg_feed = iesg_response.content.decode() - assert self.next_blog_page.url in ietf_html - assert self.blog_page.url not in ietf_html - assert self.other_blog_page.url not in ietf_html + assert f"{self.feed_settings.blog_feed_title} – iesg" in iesg_feed + assert self.next_blog_page.url in iesg_feed + assert self.blog_page.url not in iesg_feed + assert self.other_blog_page.url not in iesg_feed def test_author_feed(self): alice_url = self.blog_index.reverse_subpage( @@ -141,6 +150,7 @@ def test_author_feed(self): alice_resp = self.client.get(self.blog_index.url + alice_url) assert alice_resp.status_code == 200 feed = alice_resp.content.decode("utf8") + assert f"{self.feed_settings.blog_feed_title} – Alice" in feed assert self.other_blog_page.url in feed assert self.prev_blog_page.url in feed assert self.next_blog_page.url not in feed