diff --git a/comments/forms.py b/comments/forms.py index 5cf2285e16822c25c14525b99e648f8533bfa255..b693b4a087ab182e3fed3a2ce060691bdb5de1e8 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 e9028b7881d061d5ef4957af664782727f0d1787..fcd5ba2718f20b04d43ab6550e56db7fc0e5b718 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(