From fd0f9dcc10c772f0dd547a6bd86ca9be7b25c440 Mon Sep 17 00:00:00 2001
From: Shrikrishna Singh <krishnasingh.ss30@gmail.com>
Date: Mon, 8 Jun 2020 22:55:23 +0530
Subject: [PATCH] Allow draft to be saved without filling required fields

---
 hypha/apply/funds/models/applications.py |  6 +++---
 hypha/apply/stream_forms/models.py       | 11 +++++++----
 2 files changed, 10 insertions(+), 7 deletions(-)

diff --git a/hypha/apply/funds/models/applications.py b/hypha/apply/funds/models/applications.py
index b3d5ca429..a38bad904 100644
--- a/hypha/apply/funds/models/applications.py
+++ b/hypha/apply/funds/models/applications.py
@@ -342,14 +342,14 @@ class RoundBase(WorkflowStreamForm, SubmittableStreamForm):  # type: ignore
         return form_parameters
 
     def get_form(self, *args, **kwargs):
-        form_class = self.get_form_class()
+        draft = kwargs.pop('draft', False)
+        form_class = self.get_form_class(draft)
         submission_id = kwargs.pop('submission_id', None)
         if submission_id:
             form_params = self.get_form_parameters(submission_id=submission_id)
         else:
             form_params = self.get_form_parameters()
         form_params.update(kwargs)
-
         return form_class(*args, **form_params)
 
     def serve(self, request, *args, **kwargs):
@@ -358,7 +358,7 @@ class RoundBase(WorkflowStreamForm, SubmittableStreamForm):  # type: ignore
             copy_open_submission = request.GET.get('open_call_submission')
             if request.method == 'POST':
                 draft = request.POST.get('draft', False)
-                form = self.get_form(request.POST, request.FILES, page=self, user=request.user)
+                form = self.get_form(request.POST, request.FILES, page=self, user=request.user, draft=draft)
 
                 if form.is_valid():
                     form_submission = self.process_form_submission(form, draft=draft)
diff --git a/hypha/apply/stream_forms/models.py b/hypha/apply/stream_forms/models.py
index 890b150bb..333c7b6a9 100644
--- a/hypha/apply/stream_forms/models.py
+++ b/hypha/apply/stream_forms/models.py
@@ -3,6 +3,8 @@ from collections import OrderedDict
 
 from wagtail.contrib.forms.models import AbstractForm
 
+from hypha.apply.funds.blocks import EmailBlock, FullNameBlock, TitleBlock
+
 from .blocks import (
     FormFieldBlock,
     GroupToggleBlock,
@@ -42,7 +44,7 @@ class BaseStreamForm:
     def get_defined_fields(self):
         return self.form_fields
 
-    def get_form_fields(self):
+    def get_form_fields(self, draft=False):
         form_fields = OrderedDict()
         field_blocks = self.get_defined_fields()
         group_counter = 1
@@ -50,9 +52,10 @@ class BaseStreamForm:
         for struct_child in field_blocks:
             block = struct_child.block
             struct_value = struct_child.value
-
             if isinstance(block, FormFieldBlock):
                 field_from_block = block.get_field(struct_value)
+                if draft and not isinstance(block, (EmailBlock, FullNameBlock, TitleBlock)):
+                    field_from_block.required = False
                 field_from_block.help_link = struct_value.get('help_link')
                 field_from_block.group_number = group_counter if is_in_group else 1
                 if isinstance(block, GroupToggleBlock) and not is_in_group:
@@ -74,8 +77,8 @@ class BaseStreamForm:
 
         return form_fields
 
-    def get_form_class(self):
-        return type('WagtailStreamForm', (self.submission_form_class,), self.get_form_fields())
+    def get_form_class(self, draft=False):
+        return type('WagtailStreamForm', (self.submission_form_class,), self.get_form_fields(draft))
 
 
 class AbstractStreamForm(BaseStreamForm, AbstractForm):
-- 
GitLab