From 338b8ff4baa0086a9f96c86361608b2257c02ada Mon Sep 17 00:00:00 2001
From: Parbhat Puri <parbhatpuri17@gmail.com>
Date: Thu, 28 Mar 2019 09:55:46 +0000
Subject: [PATCH] Show all group fields when clicking built it and select first
 option for grouper

---
 opentech/apply/funds/models/applications.py        | 14 ++++++++++++++
 opentech/apply/funds/models/mixins.py              |  6 ++++++
 .../funds/templates/funds/application_base.html    |  4 +++-
 opentech/apply/templates/forms/includes/field.html |  2 +-
 4 files changed, 24 insertions(+), 2 deletions(-)

diff --git a/opentech/apply/funds/models/applications.py b/opentech/apply/funds/models/applications.py
index 1c7de4cef..7d889d4e3 100644
--- a/opentech/apply/funds/models/applications.py
+++ b/opentech/apply/funds/models/applications.py
@@ -39,6 +39,7 @@ from ..admin_forms import RoundBasePageAdminForm, WorkflowFormAdminForm
 from ..edit_handlers import ReadOnlyPanel, ReadOnlyInlinePanel
 from ..workflow import OPEN_CALL_PHASES
 
+from .mixins import UnusedFieldException
 from .submissions import ApplicationSubmission
 from .utils import admin_url, EmailForm, SubmittableStreamForm, WorkflowStreamForm, LIMIT_TO_REVIEWERS, LIMIT_TO_STAFF
 
@@ -289,11 +290,22 @@ class RoundBase(WorkflowStreamForm, SubmittableStreamForm):  # type: ignore
             submission_class = self.get_submission_class()
             submission = submission_class.objects.get(id=submission_id)
             if submission.status in OPEN_CALL_PHASES and self.get_parent() == submission.page:
+                title_block_id = submission.named_blocks.get('title')
+                if title_block_id:
+                    field_data = submission.data(title_block_id)
+                    initial_values[title_block_id] = field_data
+
                 first_group_text_blocks = submission.first_group_normal_text_blocks
                 for field_id in first_group_text_blocks:
                     field_data = submission.data(field_id)
                     initial_values[field_id] = field_data
 
+                # Select first item in the Group toggle blocks
+                for toggle_block_id, toggle_field in submission.group_toggle_blocks:
+                    try:
+                        initial_values[toggle_block_id] = toggle_field.value['choices'][0]
+                    except (KeyError, IndexError):
+                        pass
         except (submission_class.DoesNotExist, ValueError):
             pass
 
@@ -336,6 +348,8 @@ class RoundBase(WorkflowStreamForm, SubmittableStreamForm):  # type: ignore
 
             context = self.get_context(request)
             context['form'] = form
+            copy_open_submission = request.GET.get('open_call_submission')
+            context['show_all_group_fields'] = True if copy_open_submission else False
             return render(
                 request,
                 self.get_template(request),
diff --git a/opentech/apply/funds/models/mixins.py b/opentech/apply/funds/models/mixins.py
index e48be0c5d..cecacf3b6 100644
--- a/opentech/apply/funds/models/mixins.py
+++ b/opentech/apply/funds/models/mixins.py
@@ -167,6 +167,12 @@ class AccessFormData:
             if field_id not in self.named_blocks
         ]
 
+    @property
+    def group_toggle_blocks(self):
+        for field_id, field in self.fields.items():
+            if isinstance(field.block, GroupToggleBlock):
+                yield field_id, field
+
     @property
     def first_group_normal_text_blocks(self):
         return [
diff --git a/opentech/apply/funds/templates/funds/application_base.html b/opentech/apply/funds/templates/funds/application_base.html
index 481536711..d368cca23 100644
--- a/opentech/apply/funds/templates/funds/application_base.html
+++ b/opentech/apply/funds/templates/funds/application_base.html
@@ -35,7 +35,7 @@
                     {% include "forms/includes/field.html" with is_application=True %}
                 {% else %}
                     {% if field.group_number > 1 %}
-                        <div class="field-group-{{ field.group_number }}" style="display:none;">
+                        <div class="field-group-{{ field.group_number }}" {% if not show_all_group_fields %}style="display:none;"{% endif %}>
                             {{ field }}
                         </div>
                     {% else %}
@@ -52,5 +52,7 @@
 {% block extra_js %}
     <script src="{% static 'js/apply/mailgun-validator.js' %}"></script>
     <script src="{% static 'js/apply/file-uploads.js' %}"></script>
+    {% if not show_all_group_fields %}
     <script src="{% static 'js/apply/form-group-toggle.js' %}"></script>
+    {% endif %}
 {% endblock %}
diff --git a/opentech/apply/templates/forms/includes/field.html b/opentech/apply/templates/forms/includes/field.html
index d80188d64..330406110 100644
--- a/opentech/apply/templates/forms/includes/field.html
+++ b/opentech/apply/templates/forms/includes/field.html
@@ -2,7 +2,7 @@
 
 {% with widget_type=field|widget_type field_type=field|field_type %}
 
-<div class="form__group {% if widget_type == 'checkbox_input' %}form__group--checkbox{% endif %} {% if widget_type == 'clearable_file_input' or widget_type == 'multi_file_input' %}form__group--file{% endif %} {% if field.help_text %}form__group--wrap{% endif %}{% if field.errors %}form__error{% endif %} {% if is_application and field.field.group_number > 1 %}field-group-{{ field.field.group_number }}{% endif %} {% if is_application and field.field.grouper_for %}form-fields-grouper{% endif %}" {% if is_application and field.field.grouper_for %}data-grouper-for="{{ field.field.grouper_for }}" data-toggle-on="{{ field.field.choices.0.0 }}" data-toggle-off="{{ field.field.choices.1.0 }}"{% endif %} {% if is_application and field.field.group_number > 1 %}style="display:none;"{% endif %}>
+<div class="form__group {% if widget_type == 'checkbox_input' %}form__group--checkbox{% endif %} {% if widget_type == 'clearable_file_input' or widget_type == 'multi_file_input' %}form__group--file{% endif %} {% if field.help_text %}form__group--wrap{% endif %}{% if field.errors %}form__error{% endif %} {% if is_application and field.field.group_number > 1 %}field-group-{{ field.field.group_number }}{% endif %} {% if is_application and field.field.grouper_for %}form-fields-grouper{% endif %}" {% if is_application and field.field.grouper_for %}data-grouper-for="{{ field.field.grouper_for }}" data-toggle-on="{{ field.field.choices.0.0 }}" data-toggle-off="{{ field.field.choices.1.0 }}"{% endif %} {% if is_application and field.field.group_number > 1 and not show_all_group_fields %}style="display:none;"{% endif %}>
     {% if widget_type == 'clearable_file_input' or widget_type == 'multi_file_input' %}
         <span class="form__question">{{ field.label }}</span>
         <label for="{{ field.id_for_label }}" class="form__question form__question--{{ field_type }} {{ widget_type }}" {% if field.field.required %}required{% endif %}>
-- 
GitLab