From 255b8ff114e5f572ce0574325cdebe4bb7414ba8 Mon Sep 17 00:00:00 2001
From: Fredrik Jonsson <frjo@xdeb.org>
Date: Tue, 30 Apr 2019 16:10:28 +0200
Subject: [PATCH] Inherit from NewsFeed to avoid duplication and use Coalesce
 to order_by publication_date if present.

---
 opentech/public/news/views.py | 16 ++++------------
 1 file changed, 4 insertions(+), 12 deletions(-)

diff --git a/opentech/public/news/views.py b/opentech/public/news/views.py
index 5e1929eac..7e9c7ef37 100644
--- a/opentech/public/news/views.py
+++ b/opentech/public/news/views.py
@@ -1,4 +1,5 @@
 from django.contrib.syndication.views import Feed
+from django.db.models.functions import Coalesce
 
 from wagtail.core.models import Site
 
@@ -14,7 +15,7 @@ class NewsFeed(Feed):
     description = news_feed_settings.news_description
 
     def items(self):
-        return NewsPage.objects.live().order_by('-first_published_at')[:20]
+        return NewsPage.objects.live().public().annotate(date=Coalesce('publication_date', 'first_published_at')).order_by('-date')[:20]
 
     def item_title(self, item):
         return item.title
@@ -26,7 +27,7 @@ class NewsFeed(Feed):
         return item.display_date
 
 
-class NewsTypesFeed(Feed):
+class NewsTypesFeed(NewsFeed):
     site = Site.objects.get(is_default_site=True)
     news_feed_settings = NewsFeedSettings.for_site(site=site)
     news_index = NewsIndex.objects.first()
@@ -44,13 +45,4 @@ class NewsTypesFeed(Feed):
         return self.news_feed_settings.news_per_type_description.format(news_type=obj)
 
     def items(self, obj):
-        return NewsPage.objects.live().filter(news_types__news_type=obj).order_by('-first_published_at')[:20]
-
-    def item_title(self, item):
-        return item.title
-
-    def item_description(self, item):
-        return item.body
-
-    def item_pubdate(self, item):
-        return item.display_date
+        return NewsPage.objects.live().public().filter(news_types__news_type=obj).annotate(date=Coalesce('publication_date', 'first_published_at')).order_by('-date')[:20]
-- 
GitLab