From 8996392794057231a70cd53f506b481200312f0e Mon Sep 17 00:00:00 2001
From: "J.-S. Caux" <J.S.Caux@uva.nl>
Date: Wed, 27 Jun 2018 06:41:43 +0200
Subject: [PATCH] Add citedby impact factor calculation for journal

---
 journals/models.py                    | 17 +++++++++++++++++
 stats/templates/stats/statistics.html |  3 +++
 stats/views.py                        |  1 +
 3 files changed, 21 insertions(+)

diff --git a/journals/models.py b/journals/models.py
index b3736a50e..c27612810 100644
--- a/journals/models.py
+++ b/journals/models.py
@@ -158,6 +158,23 @@ class Journal(models.Model):
                 deltat += (pub.latest_citedby_update.date() - pub.publication_date).days
         return (ncites * 365.25/deltat)
 
+    def citedby_impact_factor(self, year):
+        """
+        Computes the impact factor for a given year YYYY, from Crossref cited-by data.
+        This is defined as the total number of citations in year YYYY
+        for all papers published in years YYYY-1 and YYYY-2, divided
+        by the number of papers published in year YYYY.
+        """
+        publications = self.get_publications().filter(
+            models.Q(publication_date__year=year-1) | models.Q(publication_date__year=year-2))
+        nrpub = publications.count()
+        if nrpub == 0:
+            return 0
+        ncites = 0
+        for pub in publications:
+            if pub.citedby and pub.latest_citedby_update:
+                ncites += len(pub.citedby)
+        return ncites/nrpub
 
 class Volume(models.Model):
     """
diff --git a/stats/templates/stats/statistics.html b/stats/templates/stats/statistics.html
index 1e45058b4..a144617d1 100644
--- a/stats/templates/stats/statistics.html
+++ b/stats/templates/stats/statistics.html
@@ -42,6 +42,9 @@
 
 {% if journal %}
     <h2>Results:</h2>
+    {% if year %}
+    <h3>Impact factor (Cited-by) for {{ year }}: {{ citedby_impact_factor }}</h3>
+      {% endif %}
     <table class="table">
       <tr>
         <th>DOI label</th>
diff --git a/stats/views.py b/stats/views.py
index 4e2de0c8b..1d79d329c 100644
--- a/stats/views.py
+++ b/stats/views.py
@@ -22,6 +22,7 @@ def statistics(request, journal_doi_label=None, volume_nr=None, issue_nr=None, y
         context['journal'] = journal
         if year:
             context['year'] = year
+            context['citedby_impact_factor'] = journal.citedby_impact_factor(year)
             submissions = Submission.objects.filter(
                 submitted_to_journal=journal_doi_label).originally_submitted(
                 datetime.date(int(year), 1, 1), datetime.date(int(year), 12, 31))
-- 
GitLab