From 2137dde959900c9bb6727f6cf4e4d0af9326c79c Mon Sep 17 00:00:00 2001
From: Todd Dembrey <todd.dembrey@torchbox.com>
Date: Mon, 22 Jan 2018 19:30:26 +0000
Subject: [PATCH] Provide a way to define fields on the model which are
 required

---
 opentech/apply/funds/blocks.py        | 39 ++++++++++++++++++++++++++-
 opentech/apply/stream_forms/blocks.py |  2 +-
 2 files changed, 39 insertions(+), 2 deletions(-)

diff --git a/opentech/apply/funds/blocks.py b/opentech/apply/funds/blocks.py
index 06513827f..7750bb028 100644
--- a/opentech/apply/funds/blocks.py
+++ b/opentech/apply/funds/blocks.py
@@ -1,9 +1,46 @@
 from django.utils.translation import ugettext_lazy as _
+from django.utils.text import mark_safe
 
-from opentech.apply.stream_forms.blocks import FormFieldsBlock
+from wagtail.wagtailcore.blocks import StaticBlock
+from opentech.apply.stream_forms.blocks import FormFieldsBlock, FormFieldBlock
 
 from opentech.apply.categories.blocks import CategoryQuestionBlock
 
 
 class CustomFormFieldsBlock(FormFieldsBlock):
     category = CategoryQuestionBlock(group=_('Custom'))
+
+    def __init__(self, *args, **kwargs):
+        child_blocks = [(block.name, block(group=_('Required'))) for block in MustIncludeFieldBlock.__subclasses__()]
+        super().__init__(child_blocks, *args, **kwargs)
+
+
+class MustIncludeStatic(StaticBlock):
+    def __init__(self, *args, description='', **kwargs):
+        self.description = description
+        super().__init__(*args, **kwargs)
+
+    class Meta:
+        admin_text = 'Much be included in the form once.'
+
+    def render_form(self, *args, **kwargs):
+        form = super().render_form(*args, **kwargs)
+        form = '<br>'.join([self.description, form])
+        return mark_safe(form)
+
+
+class MustIncludeFieldBlock(FormFieldBlock):
+    def __init__(self, *args, **kwargs):
+        info_name = f'{self.name}_field'
+        child_blocks = [(info_name, MustIncludeStatic(description=self.description))]
+        super().__init__(child_blocks, *args, **kwargs)
+
+
+class TitleBlock(MustIncludeFieldBlock):
+    name = 'title'
+    description = 'The title of the project'
+
+
+class ValueBlock(MustIncludeFieldBlock):
+    name = 'value'
+    description = 'The value of the project'
diff --git a/opentech/apply/stream_forms/blocks.py b/opentech/apply/stream_forms/blocks.py
index 8340e7433..e5810e208 100644
--- a/opentech/apply/stream_forms/blocks.py
+++ b/opentech/apply/stream_forms/blocks.py
@@ -8,7 +8,7 @@ from django.utils.translation import ugettext_lazy as _
 from unidecode import unidecode
 from wagtail.wagtailcore.blocks import (
     StructBlock, TextBlock, CharBlock, BooleanBlock, ListBlock, StreamBlock,
-    DateBlock, TimeBlock, DateTimeBlock, ChoiceBlock, RichTextBlock,
+    DateBlock, TimeBlock, DateTimeBlock, ChoiceBlock, RichTextBlock,StaticBlock
 )
 
 
-- 
GitLab