From fce4e73d2d1b40d34d0365dee0951ffd9d5240cd Mon Sep 17 00:00:00 2001
From: George Katsikas <giorgakis.katsikas@gmail.com>
Date: Thu, 27 Jul 2023 19:46:17 +0300
Subject: [PATCH] apply eicrecom report filter only on publication

---
 scipost_django/submissions/forms/__init__.py  | 26 ++++++++---------
 .../submissions/models/submission.py          | 29 +++++++++++++++----
 2 files changed, 35 insertions(+), 20 deletions(-)

diff --git a/scipost_django/submissions/forms/__init__.py b/scipost_django/submissions/forms/__init__.py
index 61067189a..934925e9f 100644
--- a/scipost_django/submissions/forms/__init__.py
+++ b/scipost_django/submissions/forms/__init__.py
@@ -2597,6 +2597,18 @@ class EICRecommendationForm(forms.ModelForm):
                 raise forms.ValidationError(
                     "If you recommend Publish, please also provide a Tier."
                 )
+            if (
+                self.submission.nr_unique_thread_vetted_reports
+                < cleaned_data["for_journal"].minimal_nr_of_reports
+            ):
+                raise forms.ValidationError(
+                    "The number of latest vetted reports in this thread"
+                    " ({total_reports}) is too low for this journal"
+                    " ({min_reports}) to recommend publication.".format(
+                        total_reports=self.submission.nr_unique_thread_vetted_reports,
+                        min_reports=cleaned_data["for_journal"].minimal_nr_of_reports,
+                    )
+                )
         if (
             cleaned_data["recommendation"] in (EIC_REC_PUBLISH, EIC_REC_REJECT)
             and len(cleaned_data["remarks_for_editorial_college"]) < 10
@@ -2604,20 +2616,6 @@ class EICRecommendationForm(forms.ModelForm):
             raise forms.ValidationError(
                 "You must substantiate your recommendation to accept or reject the manuscript."
             )
-        
-        if (
-            self.submission.nr_unique_thread_reports
-            < cleaned_data["for_journal"].minimal_nr_of_reports
-        ):
-            raise forms.ValidationError(
-                "The number of reports in this thread is too low"
-                " ({total_reports}) for this journal ({min_reports})"
-                " to recomment publication.".format(
-                    total_reports=self.submission.nr_unique_thread_reports,
-                    min_reports=cleaned_data["for_journal"].minimal_nr_of_reports,
-                )
-            )
-
 
     def save(self):
         # If the cycle hadn't been chosen, set it to the DirectCycle
diff --git a/scipost_django/submissions/models/submission.py b/scipost_django/submissions/models/submission.py
index 8961bfa78..1cafff95d 100644
--- a/scipost_django/submissions/models/submission.py
+++ b/scipost_django/submissions/models/submission.py
@@ -35,6 +35,7 @@ from ..constants import (
     EVENT_FOR_AUTHOR,
     EVENT_FOR_EIC,
     SUBMISSION_TIERS,
+    STATUS_VETTED,
 )
 from ..exceptions import StageNotDefinedError
 from ..managers import SubmissionQuerySet, SubmissionEventQuerySet
@@ -725,13 +726,29 @@ class Submission(models.Model):
         return self.editor_in_charge is not None
 
     @property
-    def nr_unique_thread_reports(self):
-        return (
-            self.thread_full.filter(reports__isnull=False)
-            .values("reports__author")
-            .distinct()
-            .count()
+    def nr_unique_thread_vetted_reports(self):
+        """Return the number of vetted reports from the set of latest reports submitted by each author."""
+        # REFACTOR: I don't have access to Report objects here, so I have to be creative
+        thread_reports = self.thread_full.filter(reports__isnull=False).values(
+            "reports__status", "reports__author", "reports__date_submitted"
+        )
+        latest_reports = {}
+        for report in thread_reports:
+            if report["reports__author"] not in latest_reports:
+                latest_reports[report["reports__author"]] = report
+            elif (
+                report["reports__date_submitted"]
+                > latest_reports[report["reports__author"]]["reports__date_submitted"]
+            ):
+                latest_reports[report["reports__author"]] = report
+
+        vetted_reports = sum(
+            [
+                report["reports__status"] == STATUS_VETTED
+                for report in latest_reports.values()
+            ]
         )
+        return vetted_reports
 
     @property
     def thread_full(self):
-- 
GitLab