From 864a5f964c917a0b354e96811ad67fb182b98918 Mon Sep 17 00:00:00 2001
From: Jorran de Wit <jorrandewit@outlook.com>
Date: Sun, 27 May 2018 20:40:38 +0200
Subject: [PATCH] Update managers

---
 .../contributor_assignments_as_td.html        |  4 +--
 .../scipost/Fellow_activity_overview.html     |  2 +-
 scipost/views.py                              |  2 +-
 .../commands/email_fellows_tasklist.py        |  2 +-
 submissions/managers.py                       | 35 ++++++++++---------
 submissions/models.py                         |  9 ++---
 submissions/templatetags/submissions_pool.py  |  2 +-
 submissions/views.py                          | 14 ++++----
 8 files changed, 37 insertions(+), 33 deletions(-)

diff --git a/scipost/templates/partials/scipost/contributor_assignments_as_td.html b/scipost/templates/partials/scipost/contributor_assignments_as_td.html
index 4f6d8b211..4c1c7c6b1 100644
--- a/scipost/templates/partials/scipost/contributor_assignments_as_td.html
+++ b/scipost/templates/partials/scipost/contributor_assignments_as_td.html
@@ -9,6 +9,6 @@
 <td>{{contributor.editorial_assignments.ongoing.count}}</td>
 <td>{{contributor.editorial_assignments.last_year.count}} / {{contributor.editorial_assignments.count}}</td>
 <td>{{contributor.editorial_assignments.last_year.accepted.count}} / {{contributor.editorial_assignments.accepted.count}}</td>
-<td>{{contributor.editorial_assignments.last_year.refused.count}} / {{contributor.editorial_assignments.refused.count}}</td>
-<td>{{contributor.editorial_assignments.last_year.ignored.count}} / {{contributor.editorial_assignments.ignored.count}}</td>
+<td>{{contributor.editorial_assignments.last_year.declined.count}} / {{contributor.editorial_assignments.declined.count}}</td>
+<td>{{contributor.editorial_assignments.last_year.deprecated.count}} / {{contributor.editorial_assignments.deprecated.count}}</td>
 <td>{{contributor.editorial_assignments.last_year.completed.count}} / {{contributor.editorial_assignments.completed.count}}</td>
diff --git a/scipost/templates/scipost/Fellow_activity_overview.html b/scipost/templates/scipost/Fellow_activity_overview.html
index 9530cbfc2..6734e2fce 100644
--- a/scipost/templates/scipost/Fellow_activity_overview.html
+++ b/scipost/templates/scipost/Fellow_activity_overview.html
@@ -28,7 +28,7 @@
           <th>Assignments<br/>last yr / tot</th>
           <th>Accepted<br/>last yr / tot</th>
           <th>Refused<br/>last yr / tot</th>
-          <th>Ignored<br/>last yr / tot</th>
+          <th>Deprecated<br/>last yr / tot</th>
           <th>Fulfilled<br/>last yr / tot</th>
       </tr>
     </thead>
diff --git a/scipost/views.py b/scipost/views.py
index 68344390f..d283ea3aa 100644
--- a/scipost/views.py
+++ b/scipost/views.py
@@ -457,7 +457,7 @@ def _personal_page_editorial_actions(request):
         context['nr_authorship_claims_to_vet'] = AuthorshipClaim.objects.awaiting_vetting().count()
 
     if contributor.is_MEC():
-        context['nr_assignments_to_consider'] = contributor.editorial_assignments.open().count()
+        context['nr_assignments_to_consider'] = contributor.editorial_assignments.invited().count()
         context['active_assignments'] = contributor.editorial_assignments.ongoing()
         context['nr_reports_to_vet'] = Report.objects.awaiting_vetting().filter(
             submission__editor_in_charge=contributor).count()
diff --git a/submissions/management/commands/email_fellows_tasklist.py b/submissions/management/commands/email_fellows_tasklist.py
index 7fb1beabf..50274add2 100644
--- a/submissions/management/commands/email_fellows_tasklist.py
+++ b/submissions/management/commands/email_fellows_tasklist.py
@@ -19,7 +19,7 @@ class Command(BaseCommand):
 
         for fellow in fellows:
             assignments_ongoing = fellow.editorial_assignments.ongoing()
-            assignments_to_consider = fellow.editorial_assignments.open()
+            assignments_to_consider = fellow.editorial_assignments.invited()
             assignments_upcoming_deadline = assignments_ongoing.refereeing_deadline_within(days=7)
             if assignments_ongoing or assignments_to_consider or assignments_upcoming_deadline:
                 SubmissionUtils.load(
diff --git a/submissions/managers.py b/submissions/managers.py
index 91b6d4e65..bbb289047 100644
--- a/submissions/managers.py
+++ b/submissions/managers.py
@@ -226,28 +226,31 @@ class EditorialAssignmentQuerySet(models.QuerySet):
     def last_year(self):
         return self.filter(date_created__gt=timezone.now() - timezone.timedelta(days=365))
 
-    def accepted(self):
-        return self.filter(accepted=True)
-
-    def refused(self):
-        return self.filter(accepted=False)
+    def refereeing_deadline_within(self, days=7):
+        return self.exclude(
+            submission__reporting_deadline__gt=timezone.now() + timezone.timedelta(days=days)
+            ).exclude(submission__reporting_deadline__lt=timezone.now())
 
-    def ignored(self):
-        return self.filter(accepted=None)
+    def preassigned(self):
+        return self.filter(status=constants.STATUS_PREASSIGNED)
 
-    def completed(self):
-        return self.filter(completed=True)
+    def open(self):
+        return self.filter(status=constants.STATUS_INVITED)
 
     def ongoing(self):
-        return self.filter(completed=False, deprecated=False).accepted()
+        return self.filter(status=constants.STATUS_ACCEPTED)
 
-    def open(self):
-        return self.filter(accepted=None, deprecated=False)
+    def accepted(self):
+        return self.filter(status__in=[constants.STATUS_ACCEPTED, constants.STATUS_COMPLETED])
 
-    def refereeing_deadline_within(self, days=7):
-        return self.exclude(
-            submission__reporting_deadline__gt=timezone.now() + timezone.timedelta(days=days)
-            ).exclude(submission__reporting_deadline__lt=timezone.now())
+    def declined(self):
+        return self.filter(status=constants.STATUS_DECLINED)
+
+    def deprecated(self):
+        return self.filter(status=constants.STATUS_DECLINED)
+
+    def completed(self):
+        return self.filter(status=constants.STATUS_COMPLETED)
 
 
 class EICRecommendationQuerySet(models.QuerySet):
diff --git a/submissions/models.py b/submissions/models.py
index 22a30f4bd..5974cfce1 100644
--- a/submissions/models.py
+++ b/submissions/models.py
@@ -365,11 +365,7 @@ class EditorialAssignment(SubmissionRelatedObjectMixin, models.Model):
 
     submission = models.ForeignKey('submissions.Submission', on_delete=models.CASCADE)
     to = models.ForeignKey('scipost.Contributor', on_delete=models.CASCADE)
-    accepted = models.NullBooleanField(choices=ASSIGNMENT_NULLBOOL, default=None)
 
-    # attribute `deprecated' becomes True if another Fellow becomes Editor-in-charge
-    deprecated = models.BooleanField(default=False)
-    completed = models.BooleanField(default=False)
     status = models.CharField(
         max_length=16, choices=ASSIGNMENT_STATUSES, default=STATUS_PREASSIGNED)
     refusal_reason = models.CharField(
@@ -382,6 +378,11 @@ class EditorialAssignment(SubmissionRelatedObjectMixin, models.Model):
 
     objects = EditorialAssignmentQuerySet.as_manager()
 
+    # Deprecated fields
+    accepted = models.NullBooleanField(choices=ASSIGNMENT_NULLBOOL, default=None)
+    deprecated = models.BooleanField(default=False)
+    completed = models.BooleanField(default=False)
+
     class Meta:
         default_related_name = 'editorial_assignments'
         ordering = ['-date_created']
diff --git a/submissions/templatetags/submissions_pool.py b/submissions/templatetags/submissions_pool.py
index d27a642d7..50b9a4ac2 100644
--- a/submissions/templatetags/submissions_pool.py
+++ b/submissions/templatetags/submissions_pool.py
@@ -14,4 +14,4 @@ def get_editor_invitations(submission, user):
     """Check if the User invited to become EIC for Submission."""
     if not user.is_authenticated or not hasattr(user, 'contributor'):
         return EditorialAssignment.objects.none()
-    return EditorialAssignment.objects.filter(to__user=user, submission=submission).open()
+    return EditorialAssignment.objects.filter(to__user=user, submission=submission).invited()
diff --git a/submissions/views.py b/submissions/views.py
index 72b489469..79c3db17d 100644
--- a/submissions/views.py
+++ b/submissions/views.py
@@ -402,7 +402,7 @@ def pool(request, arxiv_identifier_w_vn_nr=None):
 
     recs_to_vote_on = EICRecommendation.objects.user_may_vote_on(request.user).filter(
         submission__in=submissions)
-    assignments_to_consider = EditorialAssignment.objects.open().filter(
+    assignments_to_consider = EditorialAssignment.objects.invited().filter(
         to=request.user.contributor)
 
     # Forms
@@ -525,11 +525,11 @@ def editorial_assignment(request, arxiv_identifier_w_vn_nr, assignment_id=None):
     if assignment_id:
         # Process existing EditorialAssignment.
         assignment = get_object_or_404(
-            submission.editorial_assignments.open(), to=request.user.contributor, pk=assignment_id)
+            submission.editorial_assignments.invited(), to=request.user.contributor, pk=assignment_id)
     else:
         # Get or create EditorialAssignment for user.
         try:
-            assignment = submission.editorial_assignments.open().filter(
+            assignment = submission.editorial_assignments.invited().filter(
                 to__user=request.user).first()
         except EditorialAssignment.DoesNotExist:
             assignment = EditorialAssignment()
@@ -576,7 +576,7 @@ def assignment_request(request, assignment_id):
 
     Exists for historical reasons; email are send with this url construction.
     """
-    assignment = get_object_or_404(EditorialAssignment.objects.open(),
+    assignment = get_object_or_404(EditorialAssignment.objects.invited(),
                                    to=request.user.contributor, pk=assignment_id)
     return redirect(reverse('submissions:editorial_assignment', kwargs={
         'arxiv_identifier_w_vn_nr': assignment.submission.arxiv_identifier_w_vn_nr,
@@ -636,7 +636,7 @@ def volunteer_as_EIC(request, arxiv_identifier_w_vn_nr):
         latest_activity=timezone.now())
 
     # Deprecate old Editorial Assignments
-    EditorialAssignment.objects.filter(submission=submission).open().update(deprecated=True)
+    EditorialAssignment.objects.filter(submission=submission).invited().update(deprecated=True)
 
     # Send emails to EIC and authors regarding the EIC assignment.
     SubmissionUtils.load({'assignment': assignment})
@@ -668,7 +668,7 @@ def assignment_failed(request, arxiv_identifier_w_vn_nr):
         header_template='partials/submissions/admin/editorial_assignment_failed.html')
     if mail_request.is_valid():
         # Deprecate old Editorial Assignments
-        EditorialAssignment.objects.filter(submission=submission).open().update(deprecated=True)
+        EditorialAssignment.objects.filter(submission=submission).invited().update(deprecated=True)
 
         # Update status of Submission
         submission.touch()
@@ -695,7 +695,7 @@ def assignments(request):
     """
     assignments = EditorialAssignment.objects.filter(
         to=request.user.contributor).order_by('-date_created')
-    assignments_to_consider = assignments.open()
+    assignments_to_consider = assignments.invited()
     current_assignments = assignments.ongoing()
 
     context = {
-- 
GitLab