From be3f259aa3a2cf13d645ba31e7b1d19f4e965ce8 Mon Sep 17 00:00:00 2001
From: Jorran de Wit <jorrandewit@outlook.com>
Date: Sun, 21 May 2017 12:44:50 +0200
Subject: [PATCH] Delete form fields is manuscript is not resubmission

---
 submissions/forms.py                          | 37 ++++++++++++++-----
 .../templates/submissions/new_submission.html | 13 +------
 2 files changed, 28 insertions(+), 22 deletions(-)

diff --git a/submissions/forms.py b/submissions/forms.py
index c024b162a..e340102f7 100644
--- a/submissions/forms.py
+++ b/submissions/forms.py
@@ -49,6 +49,12 @@ class SubmissionChecks:
     is_resubmission = False
     last_submission = None
 
+    def __init__(self, *args, **kwargs):
+        super().__init__(*args, **kwargs)
+        # Prefill `is_resubmission` property if data is coming from initial data
+        if kwargs.get('initial', {}).get('is_resubmission', None):
+            self.is_resubmission = kwargs['initial']['is_resubmission'] in ('True', True)
+
     def _submission_already_exists(self, identifier):
         if Submission.objects.filter(arxiv_identifier_w_vn_nr=identifier).exists():
             error_message = 'This preprint version has already been submitted to SciPost.'
@@ -184,27 +190,38 @@ class RequestSubmissionForm(SubmissionChecks, forms.ModelForm):
             'referees_suggested',
             'referees_flagged'
         ]
+        widgets = {
+            'is_resubmission': forms.HiddenInput(),
+            'arxiv_identifier_w_vn_nr': forms.HiddenInput(),
+            'secondary_areas': forms.SelectMultiple(choices=SCIPOST_SUBJECT_AREAS)
+        }
 
     def __init__(self, *args, **kwargs):
         self.requested_by = kwargs.pop('requested_by', None)
         super().__init__(*args, **kwargs)
-        self.fields['is_resubmission'].widget = forms.HiddenInput()
-        self.fields['arxiv_identifier_w_vn_nr'].widget = forms.HiddenInput()
-        self.fields['arxiv_link'].widget.attrs.update(
-            {'placeholder': 'ex.:  arxiv.org/abs/1234.56789v1'})
-        self.fields['secondary_areas'].widget = forms.SelectMultiple(choices=SCIPOST_SUBJECT_AREAS)
+
+        if self.submission_is_resubmission():
+            # These fields are only available for resubmissions
+            del self.fields['author_comments']
+            del self.fields['list_of_changes']
+        else:
+            self.fields['author_comments'].widget.attrs.update({
+                'placeholder': 'Your resubmission letter (will be viewable online)', })
+            self.fields['list_of_changes'].widget.attrs.update({
+                'placeholder': 'Give a point-by-point list of changes (will be viewable online)'})
+
+        # Update placeholder for the other fields
+        self.fields['arxiv_link'].widget.attrs.update({
+            'placeholder': 'ex.:  arxiv.org/abs/1234.56789v1'})
         self.fields['abstract'].widget.attrs.update({'cols': 100})
-        self.fields['author_comments'].widget.attrs.update({
-            'placeholder': 'Your resubmission letter (will be viewable online)', })
-        self.fields['list_of_changes'].widget.attrs.update({
-            'placeholder': 'Give a point-by-point list of changes (will be viewable online)', })
         self.fields['remarks_for_editors'].widget.attrs.update({
             'placeholder': 'Any private remarks (for the editors only)', })
         self.fields['referees_suggested'].widget.attrs.update({
             'placeholder': 'Optional: names of suggested referees',
             'rows': 3})
         self.fields['referees_flagged'].widget.attrs.update({
-            'placeholder': 'Optional: names of referees whose reports should be treated with caution (+ short reason)',
+            'placeholder': ('Optional: names of referees whose reports should'
+                            ' be treated with caution (+ short reason)'),
             'rows': 3})
 
     def clean(self, *args, **kwargs):
diff --git a/submissions/templates/submissions/new_submission.html b/submissions/templates/submissions/new_submission.html
index 7cb8592ad..82b781312 100644
--- a/submissions/templates/submissions/new_submission.html
+++ b/submissions/templates/submissions/new_submission.html
@@ -6,7 +6,6 @@
 
 {% block content %}
 
-
 <script>
 $(document).ready(function(){
   $('select#id_submitted_to_journal').on('change', function (){
@@ -19,16 +18,6 @@ $(document).ready(function(){
           $("#id_submission_type").parents('.form-group').hide()
       }
   }).trigger('change');
-
-  var isresub = $("#id_is_resubmission").val();
-  switch(isresub){
-    case "True":
-      $("#id_author_comments, #id_list_of_changes").parents('.form-group').show();
-      break;
-    default:
-      $("#id_author_comments, #id_list_of_changes").parents('.form-group').hide();
-  }
-
 });
 </script>
 
@@ -37,7 +26,7 @@ $(document).ready(function(){
         <div class="card card-grey">
             <div class="card-block">
                 <h1 class="card-title mb-0">Submit a manuscript to SciPost</h1>
-                {% if form.arxiv_identifier_w_vn_nr.value %}<h2 class="my-1 py-0 text-blue">{{form.arxiv_identifier_w_vn_nr.value}}{% if form.is_resubmission.value %} <small>(resubmission)</small>{% endif %}</h2>{% endif %}
+                {% if form.arxiv_identifier_w_vn_nr.value %}<h2 class="my-1 py-0 text-blue">{{form.arxiv_identifier_w_vn_nr.value}}{% if form.submission_is_resubmission %} <small>(resubmission)</small>{% endif %}</h2>{% endif %}
             </div>
         </div>
     </div>
-- 
GitLab