From c4d92f64b98944cec02d1ac50603ce0e6030a12e Mon Sep 17 00:00:00 2001
From: "J.-S. Caux" <J.S.Caux@uva.nl>
Date: Fri, 12 Oct 2018 06:53:45 +0200
Subject: [PATCH] Improve sponsors

---
 finances/views.py                             |  3 ++
 organizations/managers.py                     | 10 ++++++
 .../templates/sponsors/_sponsor_card.html     | 12 +++++++
 sponsors/templates/sponsors/sponsors.html     | 34 +++++++++++++------
 sponsors/views.py                             |  6 ++++
 5 files changed, 54 insertions(+), 11 deletions(-)
 create mode 100644 sponsors/templates/sponsors/_sponsor_card.html

diff --git a/finances/views.py b/finances/views.py
index 73d12841f..8c574883a 100644
--- a/finances/views.py
+++ b/finances/views.py
@@ -60,6 +60,9 @@ class SubsidyListView(ListView):
 
     def get_queryset(self):
         qs = super().get_queryset()
+        org = self.request.GET.get('org')
+        if org:
+            qs = qs.filter(organization__pk=org)
         order_by = self.request.GET.get('order_by')
         ordering = self.request.GET.get('ordering')
         if order_by == 'amount':
diff --git a/organizations/managers.py b/organizations/managers.py
index 84ccfc189..c45a475bd 100644
--- a/organizations/managers.py
+++ b/organizations/managers.py
@@ -11,3 +11,13 @@ class OrganizationQuerySet(models.QuerySet):
 
     def current_sponsors(self):
         return self.filter(subsidy__date_until__gte=datetime.date.today())
+
+    def with_subsidy_above_and_up_to(self, min_amount, max_amount=None):
+        """
+        List of sponsors with at least one subsidy above parameter:amount.
+        """
+        qs = self.annotate(max_subsidy=models.Max('subsidy__amount')
+        ).filter(max_subsidy__gte=min_amount)
+        if max_amount:
+            qs = qs.filter(max_subsidy__lt=max_amount)
+        return qs
diff --git a/sponsors/templates/sponsors/_sponsor_card.html b/sponsors/templates/sponsors/_sponsor_card.html
new file mode 100644
index 000000000..fdd04b43a
--- /dev/null
+++ b/sponsors/templates/sponsors/_sponsor_card.html
@@ -0,0 +1,12 @@
+<div class="card">
+  <img class="card-img-top {{ sponsor.css_class }} p-2" src="{% if sponsor.logo %}{{ sponsor.logo.url }}{% endif %}" alt="{{ sponsor.name }} logo">
+  <div class="card-body bg-light">
+    <h4 class="card-title">
+      {% if sponsor.name_original %}{{ sponsor.name_original }}{% else %}{{ sponsor.name }}{% endif %}</h4>
+    {% if sponsor.name_original %}
+    <p class="card-text">({{ sponsor.name }})</p>
+    {% endif %}
+    <img src="{{ sponsor.country.flag }}" alt="{{ sponsor.country }} flag"/>&nbsp;<span class="text-muted"><small>[{{ sponsor.country }}]</small></span>&nbsp;&nbsp;{{ sponsor.get_country_display }}
+    &nbsp;&nbsp;<a href="{% url 'finances:subsidies' %}?org={{ sponsor.id }}">See subsidies</a>
+  </div>
+</div>
diff --git a/sponsors/templates/sponsors/sponsors.html b/sponsors/templates/sponsors/sponsors.html
index 6e5b94fd1..891e85d97 100644
--- a/sponsors/templates/sponsors/sponsors.html
+++ b/sponsors/templates/sponsors/sponsors.html
@@ -90,19 +90,31 @@
   <div class="col-12">
     <h1 class="highlight">Our current Sponsors</h1>
 
+    <h3 class="highlight">Platinum sponsors (&euro;20000 and above):</h3>
+    <div class="card-columns">
+      {% for sponsor in sponsors_20kplus %}
+      {% include 'sponsors/_sponsor_card.html' with sponsor=sponsor %}
+      {% endfor %}
+    </div>
+
+    <h3 class="highlight">Gold sponsors (&euro;10000 and above):</h3>
+    <div class="card-columns">
+      {% for sponsor in sponsors_10kplus %}
+      {% include 'sponsors/_sponsor_card.html' with sponsor=sponsor %}
+      {% endfor %}
+    </div>
+
+    <h3 class="highlight">Silver sponsors (&euro;5000 and above):</h3>
+    <div class="card-columns">
+      {% for sponsor in sponsors_5kplus %}
+      {% include 'sponsors/_sponsor_card.html' with sponsor=sponsor %}
+      {% endfor %}
+    </div>
+
+    <h3 class="highlight">All current Sponsors</h3>
     <div class="card-columns">
       {% for sponsor in current_sponsors %}
-      <div class="card">
-	<img class="card-img-top {{ sponsor.css_class }} p-2" src="{% if sponsor.logo %}{{ sponsor.logo.url }}{% endif %}" alt="{{ sponsor.name }} logo">
-	<div class="card-body bg-light">
-	  <h4 class="card-title">
-	    {% if sponsor.name_original %}{{ sponsor.name_original }}{% else %}{{ sponsor.name }}{% endif %}</h4>
-	  {% if sponsor.name_original %}
-	  <p class="card-text">({{ sponsor.name }})</p>
-	  {% endif %}
-	  <img src="{{ sponsor.country.flag }}" alt="{{ sponsor.country }} flag"/>&nbsp;<span class="text-muted"><small>[{{ sponsor.country }}]</small></span>&nbsp;&nbsp;{{ sponsor.get_country_display }}
-	</div>
-      </div>
+      {% include 'sponsors/_sponsor_card.html' with sponsor=sponsor %}
       {% endfor %}
     </div>
 
diff --git a/sponsors/views.py b/sponsors/views.py
index d381acbe9..7768f060d 100644
--- a/sponsors/views.py
+++ b/sponsors/views.py
@@ -8,8 +8,14 @@ from organizations.models import Organization
 
 
 def sponsors(request):
+    sponsors_20kplus = Organization.objects.with_subsidy_above_and_up_to(20000)
+    sponsors_10kplus = Organization.objects.with_subsidy_above_and_up_to(10000, 20000)
+    sponsors_5kplus = Organization.objects.with_subsidy_above_and_up_to(5000, 10000)
     current_sponsors = Organization.objects.current_sponsors()
     context = {
+        'sponsors_20kplus': sponsors_20kplus,
+        'sponsors_10kplus': sponsors_10kplus,
+        'sponsors_5kplus': sponsors_5kplus,
         'current_sponsors': current_sponsors,
     }
     return render(request, 'sponsors/sponsors.html', context)
-- 
GitLab