From 68006ca4f947793e257b786cf02986783ba7c8e5 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Jean-S=C3=A9bastien=20Caux?= <git@jscaux.org>
Date: Fri, 29 Oct 2021 05:40:11 +0200
Subject: [PATCH] Solidify `_hx_` news and sponsors

---
 scipost_django/scipost/views.py | 16 ++++++++++++----
 1 file changed, 12 insertions(+), 4 deletions(-)

diff --git a/scipost_django/scipost/views.py b/scipost_django/scipost/views.py
index 557c03c77..189a1b250 100644
--- a/scipost_django/scipost/views.py
+++ b/scipost_django/scipost/views.py
@@ -223,17 +223,25 @@ def _hx_submissions_page(request):
 
 
 def _hx_news(request):
-    latest_newsitem_id = NewsItem.objects.homepage().order_by('-date').first().id
+    if NewsItem.objects.homepage().exists():
+        latest_newsitem_id = NewsItem.objects.homepage().order_by('-date').first().id
+        news = NewsItem.objects.homepage().exclude(
+            pk=latest_newsitem_id).order_by('?').first()
+    else:
+        news = NewsItem.objects.none()
     context = {
-        'news': NewsItem.objects.homepage().exclude(
-            pk=latest_newsitem_id).order_by('?').first(),
+        'news': news
     }
     return render(request, 'scipost/_hx_news.html', context)
 
 
 def _hx_sponsors(request):
+    if Organization.objects.current_sponsors().exists():
+        current_sponsors = Organization.objects.current_sponsors().order_by('?')[:1]
+    else:
+        current_sponsors = Organization.objects.none()
     context = {
-        'current_sponsors': Organization.objects.current_sponsors().order_by('?')[:1]
+        'current_sponsors': current_sponsors
     }
     return render(request, 'scipost/_hx_sponsors.html', context)
 
-- 
GitLab