From c88381f20dfdfcc332f159e4c65f06ecfe2d0069 Mon Sep 17 00:00:00 2001 From: Todd Dembrey <todd.dembrey@torchbox.com> Date: Wed, 24 Jan 2018 20:29:52 +0000 Subject: [PATCH] Allow the __iter__ method to return display objects --- opentech/apply/stream_forms/forms.py | 23 ++++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-) diff --git a/opentech/apply/stream_forms/forms.py b/opentech/apply/stream_forms/forms.py index 907375327..03f3f94a5 100644 --- a/opentech/apply/stream_forms/forms.py +++ b/opentech/apply/stream_forms/forms.py @@ -16,14 +16,23 @@ class MixedFieldMetaclass(DeclarativeFieldsMetaclass): class StreamBaseForm(BaseForm, metaclass=MixedFieldMetaclass): + def swap_fields_for_display(func): + def wrapped(self, *args, **kwargs): + # Replaces the form fields with the display fields + # should only add new streamblocks and wont affect validation + fields = self.fields.copy() + self.fields = self.display + yield from func(self, *args, **kwargs) + self.fields = fields + return wrapped + + @swap_fields_for_display + def __iter__(self): + yield from super().__iter__() + + @swap_fields_for_display def _html_output(self, *args, **kwargs): - # Replaces the form fields with the display fields - # should only add new streamblocks and wont affect validation - fields = self.fields.copy() - self.fields = self.display - render = super()._html_output(*args, **kwargs) - self.fields = fields - return render + return super()._html_output(*args, **kwargs) class BlockFieldWrapper: -- GitLab