Skip to content

Blocks

hypha.apply.funds.blocks

REQUIRED_BLOCK_NAMES module-attribute

REQUIRED_BLOCK_NAMES = [name for block in __subclasses__()]

SINGLE_BLOCK_NAMES module-attribute

SINGLE_BLOCK_NAMES = [name for block in __subclasses__()]

NAMED_BLOCKS module-attribute

ApplicationSingleIncludeFieldBlock

ApplicationSingleIncludeFieldBlock(*args, **kwargs)

Bases: SingleIncludeBlock

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

ApplicationMustIncludeFieldBlock

ApplicationMustIncludeFieldBlock(*args, **kwargs)

Bases: MustIncludeFieldBlock

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

TitleBlock

TitleBlock(*args, **kwargs)

Bases: ApplicationMustIncludeFieldBlock

Source code in hypha/apply/utils/blocks.py
def __init__(self, *args, **kwargs):
    info_name = (
        f"{self._meta_class.label} Field"
        if self._meta_class.label
        else f"{self.name.title()} Field"
    )
    child_blocks = [
        ("info", SingleIncludeStatic(label=info_name, description=self.description))
    ]
    super().__init__(child_blocks, *args, **kwargs)
help_link = URLBlock(required=False, label=gettext_lazy('Help link'))

field_class class-attribute instance-attribute

field_class = CharField

widget class-attribute instance-attribute

widget = None

name class-attribute instance-attribute

name = 'title'

description class-attribute instance-attribute

description = 'The title of the project'

field_label class-attribute instance-attribute

field_label = CharBlock(label=gettext_lazy('Label'), default=gettext_lazy('What is the title of your application?'))

help_text class-attribute instance-attribute

help_text = TextBlock(required=False, label=gettext_lazy('Help text'), default=gettext_lazy('This project name can be changed if a full proposal is requested.'))

Meta

label class-attribute instance-attribute
label = gettext_lazy('Application title')
icon class-attribute instance-attribute
icon = 'tag'

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)

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 "-"

ValueBlock

ValueBlock(*args, **kwargs)

Bases: ApplicationSingleIncludeFieldBlock

Source code in hypha/apply/utils/blocks.py
def __init__(self, *args, **kwargs):
    info_name = (
        f"{self._meta_class.label} Field"
        if self._meta_class.label
        else f"{self.name.title()} Field"
    )
    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=gettext_lazy('Label'))

help_text class-attribute instance-attribute

help_text = TextBlock(required=False, label=gettext_lazy('Help text'))
help_link = URLBlock(required=False, label=gettext_lazy('Help link'))

required class-attribute instance-attribute

required = BooleanBlock(label=gettext_lazy('Required'), required=False)

name class-attribute instance-attribute

name = 'value'

description class-attribute instance-attribute

description = 'The value of the project'

widget class-attribute instance-attribute

widget = NumberInput(attrs={'min': 0})

field_class class-attribute instance-attribute

field_class = FloatField

Meta

label class-attribute instance-attribute
label = gettext_lazy('Requested amount')
icon class-attribute instance-attribute
icon = 'decimal'

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/stream_forms/blocks.py
def get_field_kwargs(self, struct_value):
    kwargs = {
        "label": struct_value["field_label"],
        "help_text": conditional_escape(struct_value["help_text"]),
        "required": struct_value.get("required", False),
    }
    if "default_value" in struct_value:
        kwargs["initial"] = struct_value["default_value"]
    form_widget = self.get_widget(struct_value)
    if form_widget is not None:
        kwargs["widget"] = form_widget
    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)

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",
    }

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 "-"

prepare_data

prepare_data(value, data, serialize)
Source code in hypha/apply/funds/blocks.py
def prepare_data(self, value, data, serialize):
    if not data:
        return data
    return format_number_as_currency(str(data))

OrganizationNameBlock

OrganizationNameBlock(*args, **kwargs)

Bases: ApplicationSingleIncludeFieldBlock

Source code in hypha/apply/utils/blocks.py
def __init__(self, *args, **kwargs):
    info_name = (
        f"{self._meta_class.label} Field"
        if self._meta_class.label
        else f"{self.name.title()} Field"
    )
    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=gettext_lazy('Label'))

help_text class-attribute instance-attribute

help_text = TextBlock(required=False, label=gettext_lazy('Help text'))
help_link = URLBlock(required=False, label=gettext_lazy('Help link'))

field_class class-attribute instance-attribute

field_class = CharField

required class-attribute instance-attribute

required = BooleanBlock(label=gettext_lazy('Required'), required=False)

name class-attribute instance-attribute

name = 'organization_name'

description class-attribute instance-attribute

description = 'The name of the organization'

widget class-attribute instance-attribute

widget = TextInput()

Meta

label class-attribute instance-attribute
label = gettext_lazy('Organization name')

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/stream_forms/blocks.py
def get_field_kwargs(self, struct_value):
    kwargs = {
        "label": struct_value["field_label"],
        "help_text": conditional_escape(struct_value["help_text"]),
        "required": struct_value.get("required", False),
    }
    if "default_value" in struct_value:
        kwargs["initial"] = struct_value["default_value"]
    form_widget = self.get_widget(struct_value)
    if form_widget is not None:
        kwargs["widget"] = form_widget
    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)

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 "-"

EmailBlock

EmailBlock(*args, **kwargs)

Bases: ApplicationMustIncludeFieldBlock

Source code in hypha/apply/utils/blocks.py
def __init__(self, *args, **kwargs):
    info_name = (
        f"{self._meta_class.label} Field"
        if self._meta_class.label
        else f"{self.name.title()} Field"
    )
    child_blocks = [
        ("info", SingleIncludeStatic(label=info_name, description=self.description))
    ]
    super().__init__(child_blocks, *args, **kwargs)
help_link = URLBlock(required=False, label=gettext_lazy('Help link'))

name class-attribute instance-attribute

name = 'email'

description class-attribute instance-attribute

description = 'The applicant email address'

field_label class-attribute instance-attribute

field_label = CharBlock(label=gettext_lazy('Label'), default=gettext_lazy('What email address should we use to contact you?'))

help_text class-attribute instance-attribute

help_text = TextBlock(required=False, label=gettext_lazy('Help text'), default=gettext_lazy('We will use this email address to communicate with you about your proposal.'))

widget class-attribute instance-attribute

widget = EmailInput

field_class class-attribute instance-attribute

field_class = EmailField

Meta

icon class-attribute instance-attribute
icon = 'mail'

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)

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 "-"

AddressFieldBlock

AddressFieldBlock(*args, **kwargs)

Bases: ApplicationSingleIncludeFieldBlock

Source code in hypha/apply/utils/blocks.py
def __init__(self, *args, **kwargs):
    info_name = (
        f"{self._meta_class.label} Field"
        if self._meta_class.label
        else f"{self.name.title()} Field"
    )
    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=gettext_lazy('Label'))

help_text class-attribute instance-attribute

help_text = TextBlock(required=False, label=gettext_lazy('Help text'))
help_link = URLBlock(required=False, label=gettext_lazy('Help link'))

widget class-attribute instance-attribute

widget = None

required class-attribute instance-attribute

required = BooleanBlock(label=gettext_lazy('Required'), required=False)

name class-attribute instance-attribute

name = 'address'

description class-attribute instance-attribute

description = 'The postal address of the user'

field_class class-attribute instance-attribute

field_class = AddressField

Meta

label class-attribute instance-attribute
label = gettext_lazy('Address')
icon class-attribute instance-attribute
icon = 'home'

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/stream_forms/blocks.py
def get_field_kwargs(self, struct_value):
    kwargs = {
        "label": struct_value["field_label"],
        "help_text": conditional_escape(struct_value["help_text"]),
        "required": struct_value.get("required", False),
    }
    if "default_value" in struct_value:
        kwargs["initial"] = struct_value["default_value"]
    form_widget = self.get_widget(struct_value)
    if form_widget is not None:
        kwargs["widget"] = form_widget
    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)

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",
    }

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 "-"

format_data

format_data(data)
Source code in hypha/apply/funds/blocks.py
def format_data(self, data):
    # Based on the fields listed in addressfields/widgets.py
    return ", ".join(data[field] for field in ADDRESS_FIELDS_ORDER if data[field])

prepare_data

prepare_data(value, data, serialize)
Source code in hypha/apply/funds/blocks.py
def prepare_data(self, value, data, serialize):
    if not data:
        return data
    data = json.loads(data)
    data = {field: data[field] for field in ADDRESS_FIELDS_ORDER}

    if serialize:
        return data

    return ", ".join(value for value in data.values() if value)

FullNameBlock

FullNameBlock(*args, **kwargs)

Bases: ApplicationMustIncludeFieldBlock

Source code in hypha/apply/utils/blocks.py
def __init__(self, *args, **kwargs):
    info_name = (
        f"{self._meta_class.label} Field"
        if self._meta_class.label
        else f"{self.name.title()} Field"
    )
    child_blocks = [
        ("info", SingleIncludeStatic(label=info_name, description=self.description))
    ]
    super().__init__(child_blocks, *args, **kwargs)
help_link = URLBlock(required=False, label=gettext_lazy('Help link'))

field_class class-attribute instance-attribute

field_class = CharField

widget class-attribute instance-attribute

widget = None

name class-attribute instance-attribute

name = 'full_name'

description class-attribute instance-attribute

description = 'Full name'

field_label class-attribute instance-attribute

field_label = CharBlock(label=gettext_lazy('Label'), default=gettext_lazy('What is your name?'))

help_text class-attribute instance-attribute

help_text = TextBlock(required=False, label=gettext_lazy('Help text'), default=gettext_lazy('We will use this name when we communicate with you about your proposal.'))

Meta

label class-attribute instance-attribute
label = gettext_lazy('Full name')
icon class-attribute instance-attribute
icon = 'user'

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)

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 "-"

DurationBlock

DurationBlock(*args, **kwargs)

Bases: ApplicationSingleIncludeFieldBlock

Source code in hypha/apply/utils/blocks.py
def __init__(self, *args, **kwargs):
    info_name = (
        f"{self._meta_class.label} Field"
        if self._meta_class.label
        else f"{self.name.title()} Field"
    )
    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=gettext_lazy('Label'))

help_text class-attribute instance-attribute

help_text = TextBlock(required=False, label=gettext_lazy('Help text'))
help_link = URLBlock(required=False, label=gettext_lazy('Help link'))

widget class-attribute instance-attribute

widget = None

required class-attribute instance-attribute

required = BooleanBlock(label=gettext_lazy('Required'), required=False)

name class-attribute instance-attribute

name = 'duration'

description class-attribute instance-attribute

description = 'Duration'

DAYS class-attribute instance-attribute

DAYS = 'days'

WEEKS class-attribute instance-attribute

WEEKS = 'weeks'

MONTHS class-attribute instance-attribute

MONTHS = 'months'

DURATION_TYPE_CHOICES class-attribute instance-attribute

DURATION_TYPE_CHOICES = ((DAYS, 'Days'), (WEEKS, 'Weeks'), (MONTHS, 'Months'))

DURATION_DAY_OPTIONS class-attribute instance-attribute

DURATION_DAY_OPTIONS = {1: '1 day', 2: '2 days', 3: '3 days', 4: '4 days', 5: '5 days', 6: '6 days', 7: '7 days'}

DURATION_WEEK_OPTIONS class-attribute instance-attribute

DURATION_WEEK_OPTIONS = {1: '1 week', 2: '2 weeks', 3: '3 weeks', 4: '4 weeks', 5: '5 weeks', 6: '6 weeks', 7: '7 weeks', 8: '8 weeks', 9: '9 weeks', 10: '10 weeks', 11: '11 weeks', 12: '12 weeks'}

DURATION_MONTH_OPTIONS class-attribute instance-attribute

DURATION_MONTH_OPTIONS = {1: '1 month', 2: '2 months', 3: '3 months', 4: '4 months', 5: '5 months', 6: '6 months', 7: '7 months', 8: '8 months', 9: '9 months', 10: '10 months', 11: '11 months', 12: '12 months', 18: '18 months', 24: '24 months'}

field_class class-attribute instance-attribute

field_class = ChoiceField

duration_type class-attribute instance-attribute

duration_type = ChoiceBlock(help_text='Duration type is used to display duration choices in Days, Weeks or Months in application forms. Be careful, changing the duration type in the active round can result in data inconsistency.', choices=DURATION_TYPE_CHOICES, default=MONTHS)

Meta

icon class-attribute instance-attribute
icon = 'date'

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)

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",
    }

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 "-"

get_field_kwargs

get_field_kwargs(struct_value, *args, **kwargs)
Source code in hypha/apply/funds/blocks.py
def get_field_kwargs(self, struct_value, *args, **kwargs):
    field_kwargs = super().get_field_kwargs(struct_value, *args, **kwargs)
    if struct_value["duration_type"] == self.DAYS:
        field_kwargs["choices"] = self.DURATION_DAY_OPTIONS.items()
    elif struct_value["duration_type"] == self.WEEKS:
        field_kwargs["choices"] = self.DURATION_WEEK_OPTIONS.items()
    else:
        field_kwargs["choices"] = self.DURATION_MONTH_OPTIONS.items()
    return field_kwargs

prepare_data

prepare_data(value, data, serialize)
Source code in hypha/apply/funds/blocks.py
def prepare_data(self, value, data, serialize):
    if not data:
        return data
    if value["duration_type"] == self.DAYS:
        return self.DURATION_DAY_OPTIONS[int(data)]
    if value["duration_type"] == self.WEEKS:
        return self.DURATION_WEEK_OPTIONS[int(data)]
    return self.DURATION_MONTH_OPTIONS[int(data)]

ApplicationCustomFormFieldsBlock

ApplicationCustomFormFieldsBlock(*args, **kwargs)

Bases: CustomFormFieldsBlock, FormFieldsBlock

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)

text_markup class-attribute instance-attribute

text_markup = RichTextBlock(group=gettext_lazy('Custom'), label=gettext_lazy('Paragraph'))

header_markup class-attribute instance-attribute

header_markup = HeadingBlock(group=gettext_lazy('Custom'), label=gettext_lazy('Section header'))

char class-attribute instance-attribute

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

multi_inputs_char class-attribute instance-attribute

multi_inputs_char = MultiInputCharFieldBlock(group=gettext_lazy('Fields'))

text class-attribute instance-attribute

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

number class-attribute instance-attribute

number = NumberFieldBlock(group=gettext_lazy('Fields'))

checkbox class-attribute instance-attribute

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

radios class-attribute instance-attribute

radios = RadioButtonsFieldBlock(group=gettext_lazy('Fields'))

dropdown class-attribute instance-attribute

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

checkboxes class-attribute instance-attribute

checkboxes = CheckboxesFieldBlock(group=gettext_lazy('Fields'))

date class-attribute instance-attribute

date = DateFieldBlock(group=gettext_lazy('Fields'))

time class-attribute instance-attribute

time = TimeFieldBlock(group=gettext_lazy('Fields'))

datetime class-attribute instance-attribute

datetime = DateTimeFieldBlock(group=gettext_lazy('Fields'))

image class-attribute instance-attribute

image = ImageFieldBlock(group=gettext_lazy('Fields'))

file class-attribute instance-attribute

file = FileFieldBlock(group=gettext_lazy('Fields'))

multi_file class-attribute instance-attribute

multi_file = MultiFileFieldBlock(group=gettext_lazy('Fields'))

group_toggle class-attribute instance-attribute

group_toggle = GroupToggleBlock(group=gettext_lazy('Custom'))

group_toggle_end class-attribute instance-attribute

group_toggle_end = GroupToggleEndBlock(group=gettext_lazy('Custom'))

markdown_text class-attribute instance-attribute

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

required_block_names instance-attribute

required_block_names = [name for block in required_blocks]

single_block_names instance-attribute

single_block_names = [name for block in single_blocks] + required_block_names

category class-attribute instance-attribute

category = CategoryQuestionBlock(group=gettext_lazy('Custom'))

rich_text class-attribute instance-attribute

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

required_blocks class-attribute instance-attribute

required_blocks = __subclasses__()

single_blocks class-attribute instance-attribute

single_blocks = __subclasses__()

Meta

label class-attribute instance-attribute
label = gettext_lazy('Form fields')

clean

clean(value)
Source code in hypha/apply/utils/blocks.py
def clean(self, value):
    try:
        value = super().clean(value)
    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(
                        "{} cannot be empty for {}".format(
                            child_block.label, 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(
                                "{} cannot be empty for {}".format(
                                    child_block.label, 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})]
        )

to_python

to_python(value)

This allows historic data to still be accessible even if a custom field type is removed from the code in the future.

Source code in hypha/apply/utils/blocks.py
def to_python(self, value):
    """
    This allows historic data to still be accessible even
    if a custom field type is removed from the code in the future.
    """
    # If the data type is missing, fallback to a CharField
    for child_data in value:
        if child_data["type"] not in self.child_blocks:
            child_data["type"] = "char"

    return StreamValue(self, value, is_lazy=True)