From 7af08c8928f40bf2189954122d3f4db1049b7bbe Mon Sep 17 00:00:00 2001 From: George Katsikas <giorgakis.katsikas@gmail.com> Date: Mon, 9 Dec 2024 16:01:15 +0100 Subject: [PATCH] reassign editor tool replaces across all versions --- scipost_django/submissions/forms/__init__.py | 38 ++++++------------- .../submissions/models/submission.py | 25 +++++++++++- 2 files changed, 35 insertions(+), 28 deletions(-) diff --git a/scipost_django/submissions/forms/__init__.py b/scipost_django/submissions/forms/__init__.py index e76d859d3..b026e527e 100644 --- a/scipost_django/submissions/forms/__init__.py +++ b/scipost_django/submissions/forms/__init__.py @@ -2086,40 +2086,26 @@ class SubmissionReassignmentForm(forms.ModelForm): def save(self): """Update old/create new Assignment and send mails.""" - old_editor = self.submission.editor_in_charge - old_assignment = ( - self.submission.editorial_assignments.ongoing() - .filter(to=old_editor) - .first() + assignment, old_assignment = self.submission.set_editor_in_charge( + self.cleaned_data["new_editor"] ) - if old_assignment: - EditorialAssignment.objects.filter(id=old_assignment.id).update( - status=EditorialAssignment.STATUS_REPLACED - ) - - # Update Submission and update/create Editorial Assignments - now = timezone.now() - assignment = EditorialAssignment.objects.create( - submission=self.submission, - to=self.cleaned_data["new_editor"], - status=EditorialAssignment.STATUS_ACCEPTED, - date_invited=now, - date_answered=now, - ) - - self.submission.set_editor_in_charge(self.cleaned_data["new_editor"]) - # Email old and new editor + # Email old and new editor for the change in the latest submission if old_assignment and self.cleaned_data["email_old_eic"]: mail_sender = DirectMailUtil( "fellows/email_fellow_replaced_by_other", assignment=old_assignment ) mail_sender.send_mail() - mail_sender = DirectMailUtil( - "fellows/email_fellow_assigned_submission", assignment=assignment - ) - mail_sender.send_mail() + if assignment: + mail_sender = DirectMailUtil( + "fellows/email_fellow_assigned_submission", assignment=assignment + ) + mail_sender.send_mail() + + # Also update the editor for all other versions without sending mails + for submission in self.submission.get_other_versions(): + submission.set_editor_in_charge(self.cleaned_data["new_editor"]) class SubmissionTargetJournalForm(forms.ModelForm): diff --git a/scipost_django/submissions/models/submission.py b/scipost_django/submissions/models/submission.py index 1a84cc556..b585a53aa 100644 --- a/scipost_django/submissions/models/submission.py +++ b/scipost_django/submissions/models/submission.py @@ -965,7 +965,7 @@ class Submission(models.Model): def set_editor_in_charge( self, eic: Contributor | None, commit: bool = True - ) -> None: + ) -> tuple[EditorialAssignment | None, EditorialAssignment | None]: """ Set the Editor-in-Charge of the Submission. If `eic` is `None`, the EIC will be removed. @@ -981,7 +981,26 @@ class Submission(models.Model): # If the EIC has not changed, return early if not editor_changed: - return + return None, None + + # Create assignment and update old one + now = timezone.now() + assignment = None + if isinstance(eic, Contributor): + assignment = EditorialAssignment.objects.create( + submission=self, + to=eic, + status=EditorialAssignment.STATUS_ACCEPTED, + date_invited=now, + date_answered=now, + ) + if old_editor is not None and ( + old_assignment := EditorialAssignment.objects.filter( + submission=self, to=old_editor + ).first() + ): + old_assignment.status = EditorialAssignment.STATUS_REPLACED + old_assignment.save() # Add permission to new EIC, remove from old EIC if replaced if isinstance(eic, Contributor): @@ -1009,6 +1028,8 @@ class Submission(models.Model): if commit: self.save() + return assignment, old_assignment + def get_default_fellowship(self): """ Return the default *list* of Fellows for this Submission. -- GitLab