From c80c29690d49bd9734efb7eaadd676d5d0d89300 Mon Sep 17 00:00:00 2001
From: "J.-S. Caux" <J.S.Caux@uva.nl>
Date: Sat, 2 May 2020 06:58:08 +0200
Subject: [PATCH] Add journal cost_info and cost_per_publication

---
 finances/models.py                            |  1 +
 journals/migrations/0086_journal_cost_info.py | 20 +++++++++++++++++++
 journals/models/journal.py                    | 14 +++++++++++++
 organizations/api/serializers.py              |  9 ++++++---
 4 files changed, 41 insertions(+), 3 deletions(-)
 create mode 100644 journals/migrations/0086_journal_cost_info.py

diff --git a/finances/models.py b/finances/models.py
index c1f6ace9c..294759945 100644
--- a/finances/models.py
+++ b/finances/models.py
@@ -100,6 +100,7 @@ def subsidy_attachment_path(instance, filename):
         instance.subsidy.date.strftime('%Y'),
         instance.subsidy.organization.country, filename)
 
+
 class SubsidyAttachment(models.Model):
     """
     A document related to a Subsidy.
diff --git a/journals/migrations/0086_journal_cost_info.py b/journals/migrations/0086_journal_cost_info.py
new file mode 100644
index 000000000..e46b554b0
--- /dev/null
+++ b/journals/migrations/0086_journal_cost_info.py
@@ -0,0 +1,20 @@
+# Generated by Django 2.2.11 on 2020-05-02 04:40
+
+import django.contrib.postgres.fields.jsonb
+from django.db import migrations
+import journals.models.journal
+
+
+class Migration(migrations.Migration):
+
+    dependencies = [
+        ('journals', '0085_auto_20191017_0949'),
+    ]
+
+    operations = [
+        migrations.AddField(
+            model_name='journal',
+            name='cost_info',
+            field=django.contrib.postgres.fields.jsonb.JSONField(default=journals.models.journal.cost_default_value),
+        ),
+    ]
diff --git a/journals/models/journal.py b/journals/models/journal.py
index cfa723a30..bdbc5747d 100644
--- a/journals/models/journal.py
+++ b/journals/models/journal.py
@@ -4,6 +4,7 @@ __license__ = "AGPL v3"
 
 import datetime
 
+from django.contrib.postgres.fields import JSONField
 from django.db import models
 from django.db.models import Avg, F
 from django.urls import reverse
@@ -15,6 +16,10 @@ from ..managers import JournalQuerySet
 from ..validators import doi_journal_validator
 
 
+def cost_default_value():
+    return { 'default': 400 }
+
+
 class Journal(models.Model):
     """Journal is a container of Publications with a unique issn and doi_label.
 
@@ -58,6 +63,9 @@ class Journal(models.Model):
         help_text='Gzipped tarball of the LaTeX template package',
         upload_to='UPLOADS/TEMPLATES/latex/%Y/', max_length=256, blank=True)
 
+    # Cost per publication information
+    cost_info = JSONField(default=cost_default_value)
+
     objects = JournalQuerySet.as_manager()
 
     class Meta:
@@ -166,3 +174,9 @@ class Journal(models.Model):
                     if citation['year'] == year:
                         ncites += 1
         return ncites / nrpub
+
+    def cost_per_publication(self, year):
+        try:
+            return int(self.cost_info[str(year)])
+        except KeyError:
+            return int(self.cost_info['default'])
diff --git a/organizations/api/serializers.py b/organizations/api/serializers.py
index 69b37501a..bbab8e362 100644
--- a/organizations/api/serializers.py
+++ b/organizations/api/serializers.py
@@ -3,6 +3,7 @@ __license__ = "AGPL v3"
 
 
 from django.db.models import Sum
+from django.shortcuts import get_object_or_404
 from django.utils import timezone
 
 from django_countries.serializer_fields import CountryField
@@ -11,7 +12,7 @@ from rest_framework import serializers
 from ..models import Organization
 
 from journals.api.serializers import OrgPubFractionSerializer
-from journals.models import OrgPubFraction
+from journals.models import Journal, OrgPubFraction
 
 
 class OrganizationSerializer(serializers.HyperlinkedModelSerializer):
@@ -44,11 +45,13 @@ class OrganizationBalanceSerializer(serializers.BaseSerializer):
                 sumpf = pfy.filter(
                     publication__doi_label__istartswith=journal_label + '.'
                 ).aggregate(Sum('fraction'))['fraction__sum']
-                expenditure = 400* sumpf
+                costperpaper = get_object_or_404(Journal,
+                    doi_label=journal_label).cost_per_publication(year)
+                expenditure = int(costperpaper* sumpf)
                 if sumpf > 0:
                     rep[str(year)]['expenditures'][journal_label] = {
                         'pubfractions': sumpf,
-                        'costperpaper': 400,
+                        'costperpaper': costperpaper,
                         'expenditure': expenditure,
                     }
                 summed_expenditure += expenditure
-- 
GitLab