Skip to content

Blocks

hypha.apply.determinations.blocks

DeterminationMustIncludeFieldBlock

DeterminationMustIncludeFieldBlock(*args, **kwargs)

Bases: MustIncludeFieldBlock

Source code in hypha/apply/utils/blocks.py
def __init__(self, *args, **kwargs):
    info_name = _("{} Field").format(
        self._meta_class.label if self._meta_class.label else self.name.title()
    )
    child_blocks = [
        ("info", SingleIncludeStatic(label=info_name, description=self.description))
    ]
    super().__init__(child_blocks, *args, **kwargs)

field_label class-attribute instance-attribute

field_label = CharBlock(label=_('Label'))

help_text class-attribute instance-attribute

help_text = RichTextBlock(required=False, label=_('Help text'))

field_class class-attribute instance-attribute

field_class = forms.CharField

widget class-attribute instance-attribute

widget = None

Meta

template class-attribute instance-attribute
template = 'stream_forms/render_field.html'

get_slug

get_slug(struct_value)
Source code in hypha/apply/stream_forms/blocks.py
def get_slug(self, struct_value):
    return force_str(slugify(anyascii(struct_value["field_label"])))

get_field_class

get_field_class(struct_value)
Source code in hypha/apply/stream_forms/blocks.py
def get_field_class(self, struct_value):
    return self.field_class

get_widget

get_widget(struct_value)
Source code in hypha/apply/stream_forms/blocks.py
def get_widget(self, struct_value):
    return self.widget

get_field_kwargs

get_field_kwargs(struct_value)
Source code in hypha/apply/utils/blocks.py
def get_field_kwargs(self, struct_value):
    kwargs = super().get_field_kwargs(struct_value)
    kwargs["required"] = True
    return kwargs

get_field

get_field(struct_value)
Source code in hypha/apply/stream_forms/blocks.py
def get_field(self, struct_value):
    field_kwargs = self.get_field_kwargs(struct_value)
    return self.get_field_class(struct_value)(**field_kwargs)

get_display_value

get_display_value(value)

The value to display the question with.

Blocks that derive parts of the question, such as a label taken from a linked object, apply those defaults here so that every way of rendering the field gets them.

Source code in hypha/apply/stream_forms/blocks.py
def get_display_value(self, value):
    """The value to display the question with.

    Blocks that derive parts of the question, such as a label taken from a
    linked object, apply those defaults here so that every way of rendering
    the field gets them.
    """
    return value

decode

decode(value)

Convert JSON representation into actual python objects

Source code in hypha/apply/stream_forms/blocks.py
def decode(self, value):
    """Convert JSON representation into actual python objects"""
    return value

serialize

serialize(value, context)
Source code in hypha/apply/stream_forms/blocks.py
def serialize(self, value, context):
    field_kwargs = self.get_field_kwargs(value)
    return {
        "question": field_kwargs["label"],
        "answer": context.get("data"),
        "type": self.name,
    }

serialize_no_response

serialize_no_response(value, context)
Source code in hypha/apply/stream_forms/blocks.py
def serialize_no_response(self, value, context):
    return {
        "question": value["field_label"],
        "answer": "-",
        "type": "no_response",
    }

prepare_data

prepare_data(value, data, serialize=False)
Source code in hypha/apply/stream_forms/blocks.py
def prepare_data(self, value, data, serialize=False):
    return nh3_value(str(data))

render

render(value, context)
Source code in hypha/apply/stream_forms/blocks.py
def render(self, value, context):
    data = context.get("data")
    data = self.prepare_data(value, data, context.get("serialize", False))

    context.update(data=data or self.no_response())

    if context.get("serialize"):
        if not data:
            return self.serialize_no_response(value, context)
        return self.serialize(value, context)

    return super().render(value, context)

get_searchable_content

get_searchable_content(value, data)
Source code in hypha/apply/stream_forms/blocks.py
def get_searchable_content(self, value, data):
    return str(data)

no_response

no_response()
Source code in hypha/apply/stream_forms/blocks.py
def no_response(self):
    return "-"

DeterminationBlock

DeterminationBlock(*args, **kwargs)

Bases: DeterminationMustIncludeFieldBlock

Source code in hypha/apply/utils/blocks.py
def __init__(self, *args, **kwargs):
    info_name = _("{} Field").format(
        self._meta_class.label if self._meta_class.label else self.name.title()
    )
    child_blocks = [
        ("info", SingleIncludeStatic(label=info_name, description=self.description))
    ]
    super().__init__(child_blocks, *args, **kwargs)

name class-attribute instance-attribute

name = 'determination'

description class-attribute instance-attribute

description = _('Overall determination')

field_class class-attribute instance-attribute

field_class = forms.TypedChoiceField

field_label class-attribute instance-attribute

field_label = CharBlock(label=_('Label'))

help_text class-attribute instance-attribute

help_text = RichTextBlock(required=False, label=_('Help text'))

widget class-attribute instance-attribute

widget = None

Meta

icon class-attribute instance-attribute
icon = 'pick'

get_field_kwargs

get_field_kwargs(struct_value)
Source code in hypha/apply/determinations/blocks.py
def get_field_kwargs(self, struct_value):
    kwargs = super().get_field_kwargs(struct_value)
    kwargs["choices"] = DETERMINATION_CHOICES
    return kwargs

render

render(value, context=None)
Source code in hypha/apply/determinations/blocks.py
def render(self, value, context=None):
    data = int(context["data"])
    choices = dict(DETERMINATION_CHOICES)
    context["data"] = choices[data]
    return super().render(value, context)

get_slug

get_slug(struct_value)
Source code in hypha/apply/stream_forms/blocks.py
def get_slug(self, struct_value):
    return force_str(slugify(anyascii(struct_value["field_label"])))

get_field_class

get_field_class(struct_value)
Source code in hypha/apply/stream_forms/blocks.py
def get_field_class(self, struct_value):
    return self.field_class

get_widget

get_widget(struct_value)
Source code in hypha/apply/stream_forms/blocks.py
def get_widget(self, struct_value):
    return self.widget

get_field

get_field(struct_value)
Source code in hypha/apply/stream_forms/blocks.py
def get_field(self, struct_value):
    field_kwargs = self.get_field_kwargs(struct_value)
    return self.get_field_class(struct_value)(**field_kwargs)

get_display_value

get_display_value(value)

The value to display the question with.

Blocks that derive parts of the question, such as a label taken from a linked object, apply those defaults here so that every way of rendering the field gets them.

Source code in hypha/apply/stream_forms/blocks.py
def get_display_value(self, value):
    """The value to display the question with.

    Blocks that derive parts of the question, such as a label taken from a
    linked object, apply those defaults here so that every way of rendering
    the field gets them.
    """
    return value

decode

decode(value)

Convert JSON representation into actual python objects

Source code in hypha/apply/stream_forms/blocks.py
def decode(self, value):
    """Convert JSON representation into actual python objects"""
    return value

serialize

serialize(value, context)
Source code in hypha/apply/stream_forms/blocks.py
def serialize(self, value, context):
    field_kwargs = self.get_field_kwargs(value)
    return {
        "question": field_kwargs["label"],
        "answer": context.get("data"),
        "type": self.name,
    }

serialize_no_response

serialize_no_response(value, context)
Source code in hypha/apply/stream_forms/blocks.py
def serialize_no_response(self, value, context):
    return {
        "question": value["field_label"],
        "answer": "-",
        "type": "no_response",
    }

prepare_data

prepare_data(value, data, serialize=False)
Source code in hypha/apply/stream_forms/blocks.py
def prepare_data(self, value, data, serialize=False):
    return nh3_value(str(data))

get_searchable_content

get_searchable_content(value, data)
Source code in hypha/apply/stream_forms/blocks.py
def get_searchable_content(self, value, data):
    return str(data)

no_response

no_response()
Source code in hypha/apply/stream_forms/blocks.py
def no_response(self):
    return "-"

DeterminationMessageBlock

DeterminationMessageBlock(*args, **kwargs)

Bases: DeterminationMustIncludeFieldBlock

Source code in hypha/apply/utils/blocks.py
def __init__(self, *args, **kwargs):
    info_name = _("{} Field").format(
        self._meta_class.label if self._meta_class.label else self.name.title()
    )
    child_blocks = [
        ("info", SingleIncludeStatic(label=info_name, description=self.description))
    ]
    super().__init__(child_blocks, *args, **kwargs)

name class-attribute instance-attribute

name = 'message'

description class-attribute instance-attribute

description = _('Determination message')

widget class-attribute instance-attribute

field_label class-attribute instance-attribute

field_label = CharBlock(label=_('Label'))

help_text class-attribute instance-attribute

help_text = RichTextBlock(required=False, label=_('Help text'))

field_class class-attribute instance-attribute

field_class = forms.CharField

Meta

icon class-attribute instance-attribute
icon = 'openquote'
template class-attribute instance-attribute
template = 'stream_forms/render_unsafe_field.html'

get_field_kwargs

get_field_kwargs(struct_value)
Source code in hypha/apply/determinations/blocks.py
def get_field_kwargs(self, struct_value):
    kwargs = super().get_field_kwargs(struct_value)
    kwargs["required"] = False
    return kwargs

get_slug

get_slug(struct_value)
Source code in hypha/apply/stream_forms/blocks.py
def get_slug(self, struct_value):
    return force_str(slugify(anyascii(struct_value["field_label"])))

get_field_class

get_field_class(struct_value)
Source code in hypha/apply/stream_forms/blocks.py
def get_field_class(self, struct_value):
    return self.field_class

get_widget

get_widget(struct_value)
Source code in hypha/apply/stream_forms/blocks.py
def get_widget(self, struct_value):
    return self.widget

get_field

get_field(struct_value)
Source code in hypha/apply/stream_forms/blocks.py
def get_field(self, struct_value):
    field_kwargs = self.get_field_kwargs(struct_value)
    return self.get_field_class(struct_value)(**field_kwargs)

get_display_value

get_display_value(value)

The value to display the question with.

Blocks that derive parts of the question, such as a label taken from a linked object, apply those defaults here so that every way of rendering the field gets them.

Source code in hypha/apply/stream_forms/blocks.py
def get_display_value(self, value):
    """The value to display the question with.

    Blocks that derive parts of the question, such as a label taken from a
    linked object, apply those defaults here so that every way of rendering
    the field gets them.
    """
    return value

decode

decode(value)

Convert JSON representation into actual python objects

Source code in hypha/apply/stream_forms/blocks.py
def decode(self, value):
    """Convert JSON representation into actual python objects"""
    return value

serialize

serialize(value, context)
Source code in hypha/apply/stream_forms/blocks.py
def serialize(self, value, context):
    field_kwargs = self.get_field_kwargs(value)
    return {
        "question": field_kwargs["label"],
        "answer": context.get("data"),
        "type": self.name,
    }

serialize_no_response

serialize_no_response(value, context)
Source code in hypha/apply/stream_forms/blocks.py
def serialize_no_response(self, value, context):
    return {
        "question": value["field_label"],
        "answer": "-",
        "type": "no_response",
    }

prepare_data

prepare_data(value, data, serialize=False)
Source code in hypha/apply/stream_forms/blocks.py
def prepare_data(self, value, data, serialize=False):
    return nh3_value(str(data))

render

render(value, context)
Source code in hypha/apply/stream_forms/blocks.py
def render(self, value, context):
    data = context.get("data")
    data = self.prepare_data(value, data, context.get("serialize", False))

    context.update(data=data or self.no_response())

    if context.get("serialize"):
        if not data:
            return self.serialize_no_response(value, context)
        return self.serialize(value, context)

    return super().render(value, context)

get_searchable_content

get_searchable_content(value, data)
Source code in hypha/apply/stream_forms/blocks.py
def get_searchable_content(self, value, data):
    return str(data)

no_response

no_response()
Source code in hypha/apply/stream_forms/blocks.py
def no_response(self):
    return "-"

SendNoticeBlock

SendNoticeBlock(*args, **kwargs)

Bases: DeterminationMustIncludeFieldBlock

Source code in hypha/apply/utils/blocks.py
def __init__(self, *args, **kwargs):
    info_name = _("{} Field").format(
        self._meta_class.label if self._meta_class.label else self.name.title()
    )
    child_blocks = [
        ("info", SingleIncludeStatic(label=info_name, description=self.description))
    ]
    super().__init__(child_blocks, *args, **kwargs)

name class-attribute instance-attribute

name = 'send_notice'

description class-attribute instance-attribute

description = _('Send Notice')

default_value class-attribute instance-attribute

default_value = BooleanBlock(default=True, required=False)

field_class class-attribute instance-attribute

field_class = forms.BooleanField

field_label class-attribute instance-attribute

field_label = CharBlock(label=_('Label'))

help_text class-attribute instance-attribute

help_text = RichTextBlock(required=False, label=_('Help text'))

widget class-attribute instance-attribute

widget = None

Meta

label class-attribute instance-attribute
label = _('Send Notice')
icon class-attribute instance-attribute
icon = 'tick-inverse'

get_field_kwargs

get_field_kwargs(struct_value)
Source code in hypha/apply/determinations/blocks.py
def get_field_kwargs(self, struct_value):
    kwargs = super().get_field_kwargs(struct_value)
    # A required BooleanField has to be checked to validate, which would
    # make it impossible to submit a determination without notifying the
    # applicant - the only thing this field is for.
    kwargs["required"] = False
    return kwargs

get_searchable_content

get_searchable_content(value, data)
Source code in hypha/apply/determinations/blocks.py
def get_searchable_content(self, value, data):
    return None

no_response

no_response()
Source code in hypha/apply/determinations/blocks.py
def no_response(self):
    return False

get_slug

get_slug(struct_value)
Source code in hypha/apply/stream_forms/blocks.py
def get_slug(self, struct_value):
    return force_str(slugify(anyascii(struct_value["field_label"])))

get_field_class

get_field_class(struct_value)
Source code in hypha/apply/stream_forms/blocks.py
def get_field_class(self, struct_value):
    return self.field_class

get_widget

get_widget(struct_value)
Source code in hypha/apply/stream_forms/blocks.py
def get_widget(self, struct_value):
    return self.widget

get_field

get_field(struct_value)
Source code in hypha/apply/stream_forms/blocks.py
def get_field(self, struct_value):
    field_kwargs = self.get_field_kwargs(struct_value)
    return self.get_field_class(struct_value)(**field_kwargs)

get_display_value

get_display_value(value)

The value to display the question with.

Blocks that derive parts of the question, such as a label taken from a linked object, apply those defaults here so that every way of rendering the field gets them.

Source code in hypha/apply/stream_forms/blocks.py
def get_display_value(self, value):
    """The value to display the question with.

    Blocks that derive parts of the question, such as a label taken from a
    linked object, apply those defaults here so that every way of rendering
    the field gets them.
    """
    return value

decode

decode(value)

Convert JSON representation into actual python objects

Source code in hypha/apply/stream_forms/blocks.py
def decode(self, value):
    """Convert JSON representation into actual python objects"""
    return value

serialize

serialize(value, context)
Source code in hypha/apply/stream_forms/blocks.py
def serialize(self, value, context):
    field_kwargs = self.get_field_kwargs(value)
    return {
        "question": field_kwargs["label"],
        "answer": context.get("data"),
        "type": self.name,
    }

serialize_no_response

serialize_no_response(value, context)
Source code in hypha/apply/stream_forms/blocks.py
def serialize_no_response(self, value, context):
    return {
        "question": value["field_label"],
        "answer": "-",
        "type": "no_response",
    }

prepare_data

prepare_data(value, data, serialize=False)
Source code in hypha/apply/stream_forms/blocks.py
def prepare_data(self, value, data, serialize=False):
    return nh3_value(str(data))

render

render(value, context)
Source code in hypha/apply/stream_forms/blocks.py
def render(self, value, context):
    data = context.get("data")
    data = self.prepare_data(value, data, context.get("serialize", False))

    context.update(data=data or self.no_response())

    if context.get("serialize"):
        if not data:
            return self.serialize_no_response(value, context)
        return self.serialize(value, context)

    return super().render(value, context)

DeterminationCustomFormFieldsBlock

DeterminationCustomFormFieldsBlock(*args, **kwargs)

Bases: CustomFormFieldsBlock

Source code in hypha/apply/utils/blocks.py
def __init__(self, *args, **kwargs):
    # The space before " Required" is to make the group sort first. Ugly but easy, and it works.
    child_blocks = [
        (block.name, block(group=_(" Required"))) for block in self.required_blocks
    ]
    child_blocks += [
        (block.name, block(group=_("Custom"))) for block in self.single_blocks
    ]
    self.required_block_names = [block.name for block in self.required_blocks]
    self.single_block_names = [
        block.name for block in self.single_blocks
    ] + self.required_block_names

    super().__init__(child_blocks, *args, **kwargs)

char class-attribute instance-attribute

char = CharFieldBlock(group=_('Fields'))

text class-attribute instance-attribute

text = TextFieldBlock(group=_('Fields'))

text_markup class-attribute instance-attribute

text_markup = RichTextBlock(group=_('Fields'), label=_('Paragraph'))

checkbox class-attribute instance-attribute

checkbox = CheckboxFieldBlock(group=_('Fields'))

dropdown class-attribute instance-attribute

dropdown = DropdownFieldBlock(group=_('Fields'))

required_blocks class-attribute instance-attribute

required_blocks = DeterminationMustIncludeFieldBlock.__subclasses__()

rich_text class-attribute instance-attribute

rich_text = RichTextFieldBlock(group=_('Fields'))

markdown_text class-attribute instance-attribute

markdown_text = MarkdownTextFieldBlock(group=_('Fields'))

single_blocks class-attribute instance-attribute

single_blocks = []

required_block_names instance-attribute

required_block_names = [block.name for block in self.required_blocks]

single_block_names instance-attribute

single_block_names = [block.name for block in self.single_blocks] + self.required_block_names

clean

clean(value, ignore_required_constraints=False)
Source code in hypha/apply/utils/blocks.py
def clean(self, value, ignore_required_constraints=False):
    try:
        value = super().clean(value, ignore_required_constraints)
    except ValidationError as e:
        error_dict = e.params or {}
    else:
        error_dict = {}

    block_types = [block.block_type for block in value]
    missing = set(self.required_block_names) - set(block_types)

    duplicates = [
        name
        for name in find_duplicates(block_types)
        if name in self.single_block_names
    ]

    all_errors = []
    if missing:
        all_errors.append(
            _("You are missing the following required fields: {}").format(
                ", ".join(prettify_names(missing))
            )
        )

    if duplicates:
        all_errors.append(
            _("The following fields must be included only once: {}").format(
                ", ".join(prettify_names(duplicates))
            )
        )
        for i, block_name in enumerate(block_types):
            if block_name in duplicates:
                self.add_error_to_child(error_dict, i, "info", _("Duplicate field"))

    for block in value:
        if hasattr(block.block, "child_blocks"):
            for child_block_name, child_block in block.block.child_blocks.items():
                if child_block.required and not block.value[child_block_name]:
                    all_errors.append(
                        _("{child} cannot be empty for {parent}").format(
                            child=child_block.label, parent=block.block.label
                        )
                    )
                if (
                    isinstance(child_block, ListBlock)
                    and child_block.child_block.required
                ):
                    for child_value in block.value[child_block_name]:
                        if not child_value:
                            all_errors.append(
                                _("{child} cannot be empty for {parent}").format(
                                    child=child_block.label,
                                    parent=block.block.label,
                                )
                            )

    if all_errors or error_dict:
        error_dict["__all__"] = all_errors
        raise ValidationError(all_errors, params=error_dict)

    return value

add_error_to_child

add_error_to_child(errors, child_number, field, message)
Source code in hypha/apply/utils/blocks.py
def add_error_to_child(self, errors, child_number, field, message):
    new_error = ErrorList([message])
    try:
        errors[child_number].data[0].params[field] = new_error
    except KeyError:
        errors[child_number] = ErrorList(
            [ValidationError("Error", params={field: new_error})]
        )