From ea1b311209a3d18d9772e72dc11b9fe7286ab34c Mon Sep 17 00:00:00 2001
From: George Katsikas <giorgakis.katsikas@gmail.com>
Date: Tue, 2 Apr 2024 18:14:32 +0200
Subject: [PATCH] improve hx_sponsors view on homepage

---
 .../templates/scipost/_hx_sponsors.html       | 49 ++++++++++---------
 scipost_django/scipost/views.py               |  8 +--
 2 files changed, 28 insertions(+), 29 deletions(-)

diff --git a/scipost_django/scipost/templates/scipost/_hx_sponsors.html b/scipost_django/scipost/templates/scipost/_hx_sponsors.html
index 0de4c8676..31ca59a26 100644
--- a/scipost_django/scipost/templates/scipost/_hx_sponsors.html
+++ b/scipost_django/scipost/templates/scipost/_hx_sponsors.html
@@ -1,30 +1,33 @@
 {% load static %}
 {% load scipost_extras %}
 
-<div id="sponsor-logos" class="sp-swap"
+<div id="sponsor-logos"
+     class="d-flex sp-swap"
      hx-get="{% url 'scipost:_hx_sponsors' %}?dt={{ request.GET.dt|increment_dt }}"
      hx-push-url="false"
      hx-trigger="every {{ request.GET.dt|increment_dt }}s"
-     hx-swap="outerHTML swap:1s"
->
-  <ul class="list list-unstyled">
-    {% for sponsor in current_sponsors %}
-      {% if sponsor.logos and sponsor.logo %}
-	<li class="p-2">
-	  <picture>
-	    {% for source in sponsor.logos.all %}
-	      <source type="{{ source.mimetype }}" srcset="{{ source.image.url }} {{ source.width }}w">
-	    {% endfor %}
-	    <img class="rounded" style="max-height: 8rem; max-width: 16rem;"
-		 src="{{ sponsor.logo.url }}" alt="{{ sponsor.name }} logo">
-	  </picture>
-	</li>
-      {% elif sponsor.logo %}
-	<li class="p-2">
-	  <img class="rounded" style="max-height: 8rem; max-width: 16rem;"
-	       src="{{ sponsor.logo.url }}" alt="{{ sponsor.name }} logo">
-	</li>
-      {% endif %}
-    {% endfor %}
-  </ul>
+     hx-swap="outerHTML swap:1s">
+
+  {% for source in sponsor.logos.all %}
+    <picture class="mx-auto d-flex flex-column">
+      <source type="{{ source.mimetype }}"
+              srcset="{{ source.image.url }} {{ source.width }}" />
+
+      <img class="rounded"
+           style="max-height: 100%;
+                  max-width: 100%;
+                  object-fit: contain"
+           src="{{ sponsor.logo.url }}"
+           alt="{{ sponsor.name }} logo" />
+
+      <caption>
+        <a href="{{ sponsor.get_absolute_url }}" target="_blank">{{ sponsor.name }}</a>
+      </caption>
+    </picture>
+  {% empty %}
+    <h2 class="mt-2">
+      <a href="{{ sponsor.get_absolute_url }}" target="_blank">{{ sponsor.name }}</a>
+    </h2>
+  {% endfor %}
+
 </div>
diff --git a/scipost_django/scipost/views.py b/scipost_django/scipost/views.py
index 60e21d7bb..9f9907134 100644
--- a/scipost_django/scipost/views.py
+++ b/scipost_django/scipost/views.py
@@ -486,12 +486,8 @@ def _hx_news(request):
 
 
 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": current_sponsors}
-    return render(request, "scipost/_hx_sponsors.html", context)
+    sponsor = Organization.objects.current_sponsors().order_by("?").first()
+    return render(request, "scipost/_hx_sponsors.html", {"sponsor": sponsor})
 
 
 def protected_serve(request, path, show_indexes=False):
-- 
GitLab