From 5d14562837a91fabe2fa045201c3759bb932ee73 Mon Sep 17 00:00:00 2001
From: Todd Dembrey <todd.dembrey@torchbox.com>
Date: Wed, 21 Feb 2018 09:26:24 +0000
Subject: [PATCH] Delegate to the blocks for defining the searchable content

---
 opentech/apply/categories/blocks.py   |  3 +++
 opentech/apply/funds/models.py        |  5 ++---
 opentech/apply/stream_forms/blocks.py | 26 ++++++++++++++++++++++++++
 3 files changed, 31 insertions(+), 3 deletions(-)

diff --git a/opentech/apply/categories/blocks.py b/opentech/apply/categories/blocks.py
index 0d3be36bf..5a0c5cf11 100644
--- a/opentech/apply/categories/blocks.py
+++ b/opentech/apply/categories/blocks.py
@@ -72,3 +72,6 @@ class CategoryQuestionBlock(OptionalFormFieldBlock):
         category = value['category']
         context['data'] = category.options.filter(id__in=data).values_list('value', flat=True)
         return super().render(value, context)
+
+    def get_searchable_content(self, value, data):
+        return None
diff --git a/opentech/apply/funds/models.py b/opentech/apply/funds/models.py
index 869009607..5b5a3535e 100644
--- a/opentech/apply/funds/models.py
+++ b/opentech/apply/funds/models.py
@@ -508,10 +508,9 @@ class ApplicationSubmission(WorkflowHelpers, AbstractFormSubmission):
         return mark_safe(''.join(fields))
 
     def prepare_search_values(self):
-        excluded_fields = ['data', 'time', 'datetime', 'value', 'category', 'number', 'file', 'multifile']
         for data, stream in self.data_and_fields():
-            if stream.block_type not in excluded_fields and data:
-                value = stream.block.get_searchable_content(stream.value, data)
+            value = stream.block.get_searchable_content(stream.value, data)
+            if value:
                 if isinstance(value, list):
                     yield ', '.join(value)
                 else:
diff --git a/opentech/apply/stream_forms/blocks.py b/opentech/apply/stream_forms/blocks.py
index 94fa52a96..aaf6b4fe8 100644
--- a/opentech/apply/stream_forms/blocks.py
+++ b/opentech/apply/stream_forms/blocks.py
@@ -55,6 +55,9 @@ class FormFieldBlock(StructBlock):
 class OptionalFormFieldBlock(FormFieldBlock):
     required = BooleanBlock(label=_('Required'), required=False)
 
+    def get_searchable_content(self, value, data):
+        return data
+
 
 CHARFIELD_FORMATS = [
     ('email', _('Email')),
@@ -79,6 +82,8 @@ class CharFieldBlock(OptionalFormFieldBlock):
         return super().get_field_class(struct_value)
 
     def get_searchable_content(self, value, data):
+        # CharField acts as a fallback. Force data to string
+        data = str(data)
         return bleach.clean(data, tags=[], strip=True)
 
 
@@ -103,6 +108,9 @@ class NumberFieldBlock(OptionalFormFieldBlock):
     class Meta:
         label = _('Number field')
 
+    def get_searchable_content(self, value, data):
+        return None
+
 
 class CheckboxFieldBlock(FormFieldBlock):
     default_value = BooleanBlock(required=False)
@@ -113,6 +121,9 @@ class CheckboxFieldBlock(FormFieldBlock):
         label = _('Checkbox field')
         icon = 'tick-inverse'
 
+    def get_searchable_content(self, value, data):
+        return None
+
 
 class RadioButtonsFieldBlock(OptionalFormFieldBlock):
     choices = ListBlock(CharBlock(label=_('Choice')))
@@ -191,6 +202,9 @@ class DateFieldBlock(OptionalFormFieldBlock):
         label = _('Date field')
         icon = 'date'
 
+    def get_searchable_content(self, value, data):
+        return None
+
 
 class HTML5TimeInput(forms.TimeInput):
     input_type = 'time'
@@ -206,6 +220,9 @@ class TimeFieldBlock(OptionalFormFieldBlock):
         label = _('Time field')
         icon = 'time'
 
+    def get_searchable_content(self, value, data):
+        return None
+
 
 class DateTimePickerInput(forms.SplitDateTimeWidget):
     def __init__(self, attrs=None, date_format=None, time_format=None):
@@ -232,6 +249,9 @@ class DateTimeFieldBlock(OptionalFormFieldBlock):
         label = _('Date+time field')
         icon = 'date'
 
+    def get_searchable_content(self, value, data):
+        return None
+
 
 class ImageFieldBlock(OptionalFormFieldBlock):
     field_class = forms.ImageField
@@ -240,6 +260,9 @@ class ImageFieldBlock(OptionalFormFieldBlock):
         label = _('Image field')
         icon = 'image'
 
+    def get_searchable_content(self, value, data):
+        return None
+
 
 class FileFieldBlock(OptionalFormFieldBlock):
     field_class = forms.FileField
@@ -248,6 +271,9 @@ class FileFieldBlock(OptionalFormFieldBlock):
         label = _('File field')
         icon = 'download'
 
+    def get_searchable_content(self, value, data):
+        return None
+
 
 class FormFieldsBlock(StreamBlock):
     text_markup = RichTextBlock(group=_('Other'), label=_('Paragraph'))
-- 
GitLab