Skip to content
Snippets Groups Projects
Commit 2137dde9 authored by Todd Dembrey's avatar Todd Dembrey
Browse files

Provide a way to define fields on the model which are required

parent 031f4240
No related branches found
No related tags found
No related merge requests found
from django.utils.translation import ugettext_lazy as _ 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 from opentech.apply.categories.blocks import CategoryQuestionBlock
class CustomFormFieldsBlock(FormFieldsBlock): class CustomFormFieldsBlock(FormFieldsBlock):
category = CategoryQuestionBlock(group=_('Custom')) 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'
...@@ -8,7 +8,7 @@ from django.utils.translation import ugettext_lazy as _ ...@@ -8,7 +8,7 @@ from django.utils.translation import ugettext_lazy as _
from unidecode import unidecode from unidecode import unidecode
from wagtail.wagtailcore.blocks import ( from wagtail.wagtailcore.blocks import (
StructBlock, TextBlock, CharBlock, BooleanBlock, ListBlock, StreamBlock, StructBlock, TextBlock, CharBlock, BooleanBlock, ListBlock, StreamBlock,
DateBlock, TimeBlock, DateTimeBlock, ChoiceBlock, RichTextBlock, DateBlock, TimeBlock, DateTimeBlock, ChoiceBlock, RichTextBlock,StaticBlock
) )
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment