From 6b02184be7baec192f3145f795d86eb5c1cefd2a Mon Sep 17 00:00:00 2001
From: Todd Dembrey <todd.dembrey@torchbox.com>
Date: Sun, 24 Dec 2017 19:09:16 +0000
Subject: [PATCH] Add a block to add the Category to the form

---
 opentech/apply/blocks.py | 44 ++++++++++++++++++++++++++++++++++++++++
 opentech/apply/models.py |  7 +++++--
 2 files changed, 49 insertions(+), 2 deletions(-)
 create mode 100644 opentech/apply/blocks.py

diff --git a/opentech/apply/blocks.py b/opentech/apply/blocks.py
new file mode 100644
index 000000000..cc82f4366
--- /dev/null
+++ b/opentech/apply/blocks.py
@@ -0,0 +1,44 @@
+from django import forms
+from django.utils.functional import cached_property
+from django.utils.translation import ugettext_lazy as _
+
+from wagtail.wagtailcore.blocks import BooleanBlock, ChooserBlock
+from wagtail.wagtailcore.utils import resolve_model_string
+
+from opentech.stream_forms.blocks import FormFieldBlock, FormFieldsBlock
+
+
+class CategoryChooserBlock(ChooserBlock):
+    widget = forms.Select
+
+    def __init__(self, target_model, **kwargs):
+        self._target_model = target_model
+        super().__init__(**kwargs)
+
+    @cached_property
+    def target_model(self):
+        return resolve_model_string(self._target_model)
+
+
+class CategoryQuestionBlock(FormFieldBlock):
+    category = CategoryChooserBlock('apply.Category')
+    multi = BooleanBlock(label='Multi select', required=False)
+
+    field_class = forms.MultipleChoiceField
+
+    def get_field_kwargs(self, struct_value):
+        kwargs = super().get_field_kwargs(struct_value)
+        options = struct_value['category'].options.all()
+        choices = ((option.id, option.value) for option in options)
+        kwargs.update({'choices': choices})
+        return kwargs
+
+    def get_widget(self, struct_value):
+        if struct_value['multi']:
+            return forms.CheckboxSelectMultiple
+        else:
+            return forms.RadioSelect
+
+
+class CustomFormFieldsBlock(FormFieldsBlock):
+    category = CategoryQuestionBlock(group=_('Custom'))
diff --git a/opentech/apply/models.py b/opentech/apply/models.py
index 692a2a489..2bb3c7c42 100644
--- a/opentech/apply/models.py
+++ b/opentech/apply/models.py
@@ -10,10 +10,10 @@ from wagtail.wagtailcore.fields import StreamField
 from wagtail.wagtailcore.models import Orderable, Page
 from wagtail.wagtailsearch import index
 
-from opentech.stream_forms.blocks import FormFieldsBlock
 from opentech.stream_forms.models import AbstractStreamForm
 from opentech.utils.models import SocialFields, ListingFields
 
+from .blocks import CustomFormFieldsBlock
 from .workflow import SingleStage, DoubleStage
 
 
@@ -52,7 +52,7 @@ class FundPage(AbstractStreamForm):
     )
 
     workflow = models.CharField(choices=WORKFLOWS, max_length=100, default=WORKFLOWS[0][0])
-    form_fields = StreamField(FormFieldsBlock())
+    form_fields = StreamField(CustomFormFieldsBlock())
 
     @property
     def workflow_class(self):
@@ -76,3 +76,6 @@ class Category(ClusterableModel):
         FieldPanel('name'),
         InlinePanel('options', label='Options'),
     ]
+
+    def __str__(self):
+        return self.name
-- 
GitLab