From a500ba2de3db3f92feec61d85006acf873e1d0bf Mon Sep 17 00:00:00 2001
From: "J.-S. Caux" <J.S.Caux@uva.nl>
Date: Tue, 6 Nov 2018 12:08:39 +0100
Subject: [PATCH] Try using .update and .refresh_from_db for Comment vetting

---
 comments/forms.py | 16 +++++++++++++++-
 comments/views.py | 18 +++++++++---------
 2 files changed, 24 insertions(+), 10 deletions(-)

diff --git a/comments/forms.py b/comments/forms.py
index 5cf2285e1..b693b4a08 100644
--- a/comments/forms.py
+++ b/comments/forms.py
@@ -4,7 +4,8 @@ __license__ = "AGPL v3"
 
 from django import forms
 
-from .constants import COMMENT_ACTION_CHOICES, COMMENT_REFUSAL_CHOICES
+from .constants import COMMENT_ACTION_CHOICES, COMMENT_ACTION_REFUSE, \
+    COMMENT_REFUSAL_CHOICES, COMMENT_REFUSAL_EMPTY
 from .models import Comment
 
 
@@ -35,3 +36,16 @@ class VetCommentForm(forms.Form):
     refusal_reason = forms.ChoiceField(choices=COMMENT_REFUSAL_CHOICES)
     email_response_field = forms.CharField(widget=forms.Textarea(),
                                            label='Justification (optional)', required=False)
+
+    def clean(self):
+        """
+        If the comment is refused, make sure a valid refusal reason is given.
+        """
+        data = super().clean()
+        print('Cleaning, action_option = %s' % data['action_option'])
+        if data['action_option'] == str(COMMENT_ACTION_REFUSE):
+            print('Refusing')
+            if data['refusal_reason'] == str(COMMENT_REFUSAL_EMPTY):
+                print('Flagging invalid reason')
+                self.add_error(None, 'Please choose a valid refusal reason')
+        return data
diff --git a/comments/views.py b/comments/views.py
index e9028b788..fcd5ba271 100644
--- a/comments/views.py
+++ b/comments/views.py
@@ -73,14 +73,15 @@ def vet_submitted_comment(request, comment_id):
     if form.is_valid():
         if form.cleaned_data['action_option'] == '1':
             # Accept the comment as is
-            comment.status = 1
-            comment.vetted_by = request.user.contributor
-            comment.save()
+            Comment.objects.filter(id=comment_id).update(status=1,
+                                                         vetted_by=request.user.contributor)
+            comment.refresh_from_db()
 
             # Update `latest_activity` fields
             content_object = comment.content_object
-            content_object.latest_activity = timezone.now()
-            content_object.save()
+            content_object.objects.filter(id=content_object.id).update(
+                latest_activity=timezone.now())
+            content_object.refresh_from_db()
 
             if isinstance(content_object, Submission):
                 # Add events to Submission and send mail to author of the Submission
@@ -121,10 +122,9 @@ def vet_submitted_comment(request, comment_id):
 
         elif form.cleaned_data['action_option'] == '2':
             # The comment request is simply rejected
-            comment.status = int(form.cleaned_data['refusal_reason'])
-            if comment.status == 0:
-                comment.status = -1  # Why's this here??
-            comment.save()
+            Comment.objects.filter(id=comment.id).update(
+                status=int(form.cleaned_data['refusal_reason']))
+            comment.refresh_from_db()
 
             # Send emails
             mail_sender = DirectMailUtil(
-- 
GitLab