diff --git a/ietf/blog/feeds.py b/ietf/blog/feeds.py index 438259bc..c2107ff8 100644 --- a/ietf/blog/feeds.py +++ b/ietf/blog/feeds.py @@ -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") + ) diff --git a/ietf/blog/tests.py b/ietf/blog/tests.py index f93e7f2f..f3c7bb50 100644 --- a/ietf/blog/tests.py +++ b/ietf/blog/tests.py @@ -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): @@ -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) diff --git a/ietf/home/models.py b/ietf/home/models.py index de8eb0a1..fae373b7 100644 --- a/ietf/home/models.py +++ b/ietf/home/models.py @@ -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 @@ -204,6 +206,8 @@ class Meta: index.SearchField("heading"), ] + blog_index_url = settings.IAB_IETF_BLOG_URL + def announcements(self): return ( IABAnnouncementPage.objects.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( diff --git a/ietf/home/templates/home/iab_home_page.html b/ietf/home/templates/home/iab_home_page.html index 29105978..504d2399 100644 --- a/ietf/home/templates/home/iab_home_page.html +++ b/ietf/home/templates/home/iab_home_page.html @@ -46,7 +46,7 @@

Blog posts

All IAB Posts on the IETF Blog diff --git a/ietf/home/templates/includes/post_iab.html b/ietf/home/templates/includes/post_iab.html index 382ba922..2e25e15d 100644 --- a/ietf/home/templates/includes/post_iab.html +++ b/ietf/home/templates/includes/post_iab.html @@ -1,31 +1,13 @@ {% load wagtailimages_tags %}
  • -
    - {% if post.feed_image and show_image %} - {% image post.feed_image height-245 as post_image %} - {{ post.main_image.title }} - {% endif %} -
    {# col-* classes include positioning, position static cancels that and allows the stretched link to cover the image #}
    -

    {{ post.title }}

    -

    {{ post.introduction|truncatechars:250 }}

    +

    {{ post.title }}

    +

    {{ post.description|truncatechars:250 }}

    - {% if post.date %} -

    {{ post.date|date:"DATE_FORMAT" }}

    - {% elif post.start_date %} -

    {{ post.start_date|date:"DATE_FORMAT" }}

    - {% endif %} - - {% if post.listing_location %} -

    {{ post.listing_location }}

    - {% endif %} - - {% if post.host %} -

    {{ post.host.title }}

    - {% endif %} +

    {{ post.published_date|date:"DATE_FORMAT" }}

    diff --git a/ietf/settings/base.py b/ietf/settings/base.py index 2c271d25..90ea2f14 100644 --- a/ietf/settings/base.py +++ b/ietf/settings/base.py @@ -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" diff --git a/ietf/urls.py b/ietf/urls.py index d1621e1f..1077214b 100644 --- a/ietf/urls.py +++ b/ietf/urls.py @@ -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 @@ -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.+)/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"),