From f60d595b98677d431193895f0ab9294506aa7dd3 Mon Sep 17 00:00:00 2001
From: "J.-S. Caux" <J.S.Caux@uva.nl>
Date: Mon, 8 Oct 2018 14:34:34 +0200
Subject: [PATCH] Use subsidy instead of agreement in organization_list

---
 finances/constants.py                         | 11 ---------
 finances/forms.py                             |  2 +-
 .../migrations/0005_auto_20181008_1405.py     | 24 +++++++++++++++++++
 finances/models.py                            |  9 +++++--
 organizations/models.py                       |  9 +++++++
 .../organizations/organization_list.html      |  6 ++---
 6 files changed, 44 insertions(+), 17 deletions(-)
 create mode 100644 finances/migrations/0005_auto_20181008_1405.py

diff --git a/finances/constants.py b/finances/constants.py
index e5f21aad2..455a10931 100644
--- a/finances/constants.py
+++ b/finances/constants.py
@@ -25,14 +25,3 @@ SUBSIDY_STATUS = (
     (SUBSIDY_INVOICED, 'invoiced'),
     (SUBSIDY_RECEIVED, 'received'),
 )
-
-
-SUBSIDY_DURATION = (
-    (datetime.timedelta(days=365), '1 year'),
-    (datetime.timedelta(days=730), '2 years'),
-    (datetime.timedelta(days=1095), '3 years'),
-    (datetime.timedelta(days=1460), '4 years'),
-    (datetime.timedelta(days=1825), '5 years'),
-    (datetime.timedelta(days=3650), '10 years'),
-    (datetime.timedelta(days=36500), 'Indefinite (100 years)'),
-)
diff --git a/finances/forms.py b/finances/forms.py
index 38c63328f..70ac25a24 100644
--- a/finances/forms.py
+++ b/finances/forms.py
@@ -21,7 +21,7 @@ class SubsidyForm(forms.ModelForm):
     class Meta:
         model = Subsidy
         fields = ['organization', 'subsidy_type', 'description',
-                  'amount', 'status', 'date', 'duration']
+                  'amount', 'status', 'date', 'date_until']
 
 
 class WorkLogForm(forms.ModelForm):
diff --git a/finances/migrations/0005_auto_20181008_1405.py b/finances/migrations/0005_auto_20181008_1405.py
new file mode 100644
index 000000000..60fc25b12
--- /dev/null
+++ b/finances/migrations/0005_auto_20181008_1405.py
@@ -0,0 +1,24 @@
+# -*- coding: utf-8 -*-
+# Generated by Django 1.11.4 on 2018-10-08 12:05
+from __future__ import unicode_literals
+
+from django.db import migrations, models
+
+
+class Migration(migrations.Migration):
+
+    dependencies = [
+        ('finances', '0004_auto_20181007_2016'),
+    ]
+
+    operations = [
+        migrations.RemoveField(
+            model_name='subsidy',
+            name='duration',
+        ),
+        migrations.AddField(
+            model_name='subsidy',
+            name='date_until',
+            field=models.DateField(blank=True, null=True),
+        ),
+    ]
diff --git a/finances/models.py b/finances/models.py
index 53cf87379..3d5384553 100644
--- a/finances/models.py
+++ b/finances/models.py
@@ -10,7 +10,7 @@ from django.urls import reverse
 from django.utils import timezone
 from django.utils.html import format_html
 
-from .constants import SUBSIDY_TYPES, SUBSIDY_STATUS, SUBSIDY_DURATION
+from .constants import SUBSIDY_TYPES, SUBSIDY_STATUS
 from .utils import id_to_slug
 
 
@@ -26,6 +26,11 @@ class Subsidy(models.Model):
     - a development grant for a specific purpose
     - a Collaboration Agreement
     - a donation
+
+    The date field represents the date at which the Subsidy was formally agreed,
+    or the agreement enters into force.
+    The date_until field is optional, and represents (where applicable) the date
+    after which the object of the Subsidy is officially terminated.
     """
     organization = models.ForeignKey('organizations.Organization', on_delete=models.CASCADE)
     subsidy_type = models.CharField(max_length=256, choices=SUBSIDY_TYPES)
@@ -33,7 +38,7 @@ class Subsidy(models.Model):
     amount = models.PositiveSmallIntegerField(help_text="in &euro; (rounded)")
     status = models.CharField(max_length=32, choices=SUBSIDY_STATUS)
     date = models.DateField()
-    duration = models.DurationField(choices=SUBSIDY_DURATION, blank=True, null=True)
+    date_until = models.DateField(blank=True, null=True)
 
     class Meta:
         verbose_name_plural = 'subsidies'
diff --git a/organizations/models.py b/organizations/models.py
index 8297e3da9..07204100e 100644
--- a/organizations/models.py
+++ b/organizations/models.py
@@ -2,6 +2,8 @@ __copyright__ = "Copyright 2016-2018, Stichting SciPost (SciPost Foundation)"
 __license__ = "AGPL v3"
 
 
+import datetime
+
 from django.contrib.postgres.fields import JSONField
 from django.db import models
 from django.db.models import Sum
@@ -129,6 +131,13 @@ class Organization(models.Model):
             return False
         return self.partner.agreements.now_active().exists()
 
+    @property
+    def has_current_subsidy(self):
+        """
+        Check if this organization has a Subsidy with a still-running validity period.
+        """
+        return self.subsidy_set.filter(date_until__gte=datetime.date.today()).exists()
+
     def get_total_subsidies_obtained(self, n_years_part=None):
         """
         Computes the total amount received by SciPost, in the form
diff --git a/organizations/templates/organizations/organization_list.html b/organizations/templates/organizations/organization_list.html
index e5ea3aa4c..361d81c5c 100644
--- a/organizations/templates/organizations/organization_list.html
+++ b/organizations/templates/organizations/organization_list.html
@@ -76,7 +76,7 @@ $(document).ready(function($) {
 	    {% if request.GET.ordering != 'asc' %}</a> <a href="?order_by=nap&ordering=asc"><i class="fa fa-sort-asc"></i></a>{% endif %}
 {% if request.GET.ordering != 'desc' %}<a href="?order_by=nap&ordering=desc"><i class="fa fa-sort-desc"></i></a>{% endif %}
 </th>
-	  <th>Partner?</th>
+	  <th>SciPost sponsor?</th>
 	</tr>
       </thead>
       <tbody>
@@ -102,9 +102,9 @@ $(document).ready(function($) {
 	    {% endif %}
 	  </td>
 	  <td>{{ org.cf_nr_associated_publications }}</td>
-	  {% if org.has_current_agreement %}
+	  {% if org.has_current_subsidy %}
 	  <td class="bg-success">Yes</td>
-	  {% elif org.partner.agreements %}
+	  {% elif org.subsidy_set.all|length > 0 %}
 	  <td class="bg-primary text-white">Due for renewal</td>
 	  {% else %}
 	  <td class="bg-warning">Not yet</td>
-- 
GitLab