Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Use IETF IAB blog feed on the home page #319

Merged
merged 3 commits into from
Aug 14, 2023
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
13 changes: 13 additions & 0 deletions ietf/blog/feeds.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,16 @@ def item_author_name(self, item):

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 items(self):
return (
BlogPage.objects.live()
.filter(topics__topic__slug=self.topic)
.annotate(d=Coalesce("date_published", "first_published_at"))
.order_by("-d")
)
35 changes: 34 additions & 1 deletion ietf/blog/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
from wagtail.models import Page, Site

from ..home.models import HomePage
from .models import BlogIndexPage, BlogPage
from ..snippets.models import Topic
from .models import BlogIndexPage, BlogPage, BlogPageTopic


class BlogTests(TestCase):
Expand Down Expand Up @@ -100,3 +101,35 @@ def test_previous_next_links_correct(self):
blog = BlogPage.objects.get(pk=self.blog.pk)
self.assertEquals(self.prevblog, blog.previous)
self.assertEquals(self.nextblog, blog.next)

def test_blog_feed(self):
r = self.client.get(path='/blog/feed/')
self.assertEqual(r.status_code, 200)
self.assertIn(self.blog.url.encode(), r.content)
self.assertIn(self.otherblog.url.encode(), r.content)

def test_topic_feed(self):
iab_topic = Topic(title="iab", slug="iab")
iab_topic.save()
iab_bptopic = BlogPageTopic(topic=iab_topic, page=self.otherblog)
iab_bptopic.save()
self.otherblog.topics = [iab_bptopic, ]
self.otherblog.save()
iesg_topic = Topic(title="iesg", slug="iesg")
iesg_topic.save()
iesg_bptopic = BlogPageTopic(topic=iesg_topic, page=self.otherblog)
iesg_bptopic.save()
self.nextblog.topics = [iesg_bptopic, ]
self.nextblog.save()

r = self.client.get(path='/blog/iab/feed/')
self.assertEqual(r.status_code, 200)
self.assertIn(self.otherblog.url.encode(), r.content)
self.assertNotIn(self.blog.url.encode(), r.content)
self.assertNotIn(self.nextblog.url.encode(), r.content)

r = self.client.get(path='/blog/iesg/feed/')
self.assertEqual(r.status_code, 200)
self.assertIn(self.nextblog.url.encode(), r.content)
self.assertNotIn(self.blog.url.encode(), r.content)
self.assertNotIn(self.otherblog.url.encode(), r.content)
38 changes: 27 additions & 11 deletions ietf/home/models.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
from __future__ import unicode_literals

from datetime import datetime
from xml.etree import ElementTree

from django.conf import settings
from django.db import models
from django.db.models.expressions import RawSQL
from modelcluster.fields import ParentalKey
from requests import get as get_request
from wagtail.admin.panels import FieldPanel, InlinePanel, MultiFieldPanel
from wagtail.models import Page
from wagtail.search import index
Expand Down Expand Up @@ -204,6 +206,8 @@ class Meta:
index.SearchField("heading"),
]

blog_index_url = settings.IAB_IETF_BLOG_URL

def announcements(self):
return (
IABAnnouncementPage.objects.all()
Expand All @@ -218,17 +222,29 @@ def blog_index(self):
return BlogIndexPage.objects.live().first()

def blogs(self, bp_kwargs={}):
return (
BlogPage.objects.live()
.filter(topics__topic__slug="iab")
.annotate(
date_sql=RawSQL(
"CASE WHEN (date_published IS NOT NULL) THEN date_published ELSE first_published_at END",
(),
)
)
.order_by("-date_sql")[:2]
)
entries = []
try:
response = get_request(settings.IAB_FEED_URL)
xml_data = response.text

root = ElementTree.fromstring(xml_data)
for item in root.iter('item'):
title = item.find('title').text
description = item.find('description').text
link = item.find('link').text
published_date = datetime.strptime(item.find('pubDate').text, '%a, %d %b %Y %H:%M:%S %z')

entry_data = {
'title': title,
'description': description,
'link': link,
'published_date': published_date,
}
entries.append(entry_data)
except Exception as _:
pass

return entries

content_panels = Page.content_panels + [
MultiFieldPanel(
Expand Down
2 changes: 1 addition & 1 deletion ietf/home/templates/home/iab_home_page.html
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ <h2 class="mb-4">Blog posts</h2>
</ul>
<a
class="btn iab-btn-secondary text-white"
href="{{ self.blog_index.url }}iab/"
href="{{ self.blog_index_url }}"
>All IAB Posts on the IETF Blog</a
>
</section>
Expand Down
24 changes: 3 additions & 21 deletions ietf/home/templates/includes/post_iab.html
Original file line number Diff line number Diff line change
@@ -1,31 +1,13 @@
{% load wagtailimages_tags %}

<li class="card mb-4 row g-0 align-items-stretch iab-blog-post">
<div>
{% if post.feed_image and show_image %}
{% image post.feed_image height-245 as post_image %}
<img class="blog-post-image-iab u-obj-cover w-100 h-100" src="{{ post_image.url }}" alt="{{ post.main_image.title }}">
{% endif %}
</div>
{# col-* classes include positioning, position static cancels that and allows the stretched link to cover the image #}
<div class="card-body card-body--iab">
<h3 class="mb-3 h5"><a class="stretched-link" href="{{ post.url }}">{{ post.title }}</a></h3>
<p class="iab-card-text">{{ post.introduction|truncatechars:250 }}</p>
<h3 class="mb-3 h5"><a class="stretched-link" href="{{ post.link }}">{{ post.title }}</a></h3>
<p class="iab-card-text">{{ post.description|truncatechars:250 }}</p>
<div class="d-flex justify-content-between align-items-center iab-date-text">
<small>
{% if post.date %}
<p>{{ post.date|date:"DATE_FORMAT" }}</p>
{% elif post.start_date %}
<p>{{ post.start_date|date:"DATE_FORMAT" }}</p>
{% endif %}

{% if post.listing_location %}
<p>{{ post.listing_location }}</p>
{% endif %}

{% if post.host %}
<p>{{ post.host.title }}</p>
{% endif %}
<p>{{ post.published_date|date:"DATE_FORMAT" }}</p>
</small>
</div>
</div>
Expand Down
3 changes: 3 additions & 0 deletions ietf/settings/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -248,4 +248,7 @@
"home.IABHomePage",
]

IAB_FEED_URL = "https://www.ietf.org/blog/iab/feed"
IAB_IETF_BLOG_URL = "https://www.ietf.org/blog/iab/"

NOTE_WELL_REPO = "https://raw.githubusercontent.com/ietf/note-well/main/note-well.md"
3 changes: 2 additions & 1 deletion ietf/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from wagtail.documents import urls as wagtaildocs_urls

from ietf.bibliography import urls as bibliography_urls
from ietf.blog.feeds import BlogFeed
from ietf.blog.feeds import BlogFeed, TopicBlogFeed
from ietf.search.views import search
from ietf.snippets import urls as snippet_urls

Expand All @@ -21,6 +21,7 @@
url(r"^bibliography/", include(bibliography_urls)),
url(r"^django-admin/", admin.site.urls),
url(r"^blog/feed/$", BlogFeed(), name="blog_feed"),
url(r"^blog/(?P<topic>.+)/feed/$", TopicBlogFeed(), name="blog_feed_with_topic"),
url(r"^admin/", include(wagtailadmin_urls)),
url(r"^documents/", include(wagtaildocs_urls)),
url(r"^search/$", search, name="search"),
Expand Down
Loading