Skip to content

Commit

Permalink
feat: Composite title for topic & author feeds (#408)
Browse files Browse the repository at this point in the history
  • Loading branch information
mgax committed May 7, 2024
1 parent 1b73a1f commit 70e920c
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 20 deletions.
28 changes: 22 additions & 6 deletions ietf/blog/feeds.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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):
Expand All @@ -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 (
Expand All @@ -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
2 changes: 1 addition & 1 deletion ietf/blog/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
36 changes: 23 additions & 13 deletions ietf/blog/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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"<title>{self.feed_settings.blog_feed_title}</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"<title>{self.feed_settings.blog_feed_title} – iab</title>" 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"<title>{self.feed_settings.blog_feed_title} – iesg</title>" 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(
Expand All @@ -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"<title>{self.feed_settings.blog_feed_title} – Alice</title>" 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
Expand Down

0 comments on commit 70e920c

Please sign in to comment.