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 @@
{{ post.introduction|truncatechars:250 }}
+{{ post.description|truncatechars:250 }}
{{ 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" }}