Skip to content

Blocks

hypha.apply.categories.blocks

ModelChooserBlock

Bases: ChoiceBlock

CategoryQuestionBlock

Bases: OptionalFormFieldBlock

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

required class-attribute instance-attribute

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

category class-attribute instance-attribute

category = ModelChooserBlock(required=True, choices=get_categories_as_choices)

multi class-attribute instance-attribute

multi = BooleanBlock(label=gettext_lazy('Multi select'), required=False)

field_label class-attribute instance-attribute

field_label = CharBlock(label=gettext_lazy('Label'), required=False, help_text=gettext_lazy('Leave blank to use the default Category label'))

help_text class-attribute instance-attribute

help_text = TextBlock(label=gettext_lazy('Help text'), required=False, help_text=gettext_lazy('Leave blank to use the default Category help text'))

Meta

template class-attribute instance-attribute
template = 'stream_forms/render_list_field.html'
icon class-attribute instance-attribute
icon = 'folder-open-1'

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

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

model_class

model_class()
Source code in hypha/apply/categories/blocks.py
@cached_property
def model_class(self):
    return resolve_model_string("categories.Category")

get_instance

get_instance(id)
Source code in hypha/apply/categories/blocks.py
def get_instance(self, id):
    return self.model_class.objects.get(id=id)

get_field_class

get_field_class(struct_value)
Source code in hypha/apply/categories/blocks.py
def get_field_class(self, struct_value):
    return forms.MultipleChoiceField if struct_value["multi"] else forms.ChoiceField

use_defaults_from_category

use_defaults_from_category(kwargs, category)
Source code in hypha/apply/categories/blocks.py
def use_defaults_from_category(self, kwargs, category):
    category_fields = {"label": "name", "help_text": "help_text"}

    for field in category_fields.keys():
        if not kwargs.get(field):
            kwargs[field] = getattr(category, category_fields[field])

    return kwargs

get_field_kwargs

get_field_kwargs(struct_value)
Source code in hypha/apply/categories/blocks.py
def get_field_kwargs(self, struct_value):
    kwargs = super().get_field_kwargs(struct_value)
    category = self.get_instance(id=struct_value["category"])
    kwargs = self.use_defaults_from_category(kwargs, category)
    choices = category.options.values_list("id", "value")
    kwargs.update({"choices": choices})
    return kwargs

get_widget

get_widget(struct_value)
Source code in hypha/apply/categories/blocks.py
def get_widget(self, struct_value):
    if struct_value["multi"]:
        category = self.get_instance(id=struct_value["category"])
        category_size = category.options.count()
        # Pick widget according to number of options to maintain good usability.
        if category_size < 32:
            return forms.CheckboxSelectMultiple
        else:
            return Select2MultipleWidget
    else:
        return forms.RadioSelect

prepare_data

prepare_data(value, data, serialize)
Source code in hypha/apply/categories/blocks.py
def prepare_data(self, value, data, serialize):
    if not data:
        return data
    if isinstance(data, str):
        data = [data]
    category = self.get_instance(id=value["category"])
    data = category.options.filter(id__in=data).values_list("value", flat=True)
    return data

render

render(value, context)
Source code in hypha/apply/categories/blocks.py
def render(self, value, context):
    # Overwriting field_label and help_text with default for empty values
    category_fields = {"field_label": "name", "help_text": "help_text"}

    for field in category_fields.keys():
        if not value.get(field):
            category = value["category"]
            if isinstance(category, int) or isinstance(category, str):
                category = self.get_instance(id=category)
            value[field] = getattr(category, category_fields[field])
    return super().render(value, context)

get_searchable_content

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

no_response

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

get_categories_as_choices

get_categories_as_choices()
Source code in hypha/apply/categories/blocks.py
def get_categories_as_choices():
    Category = resolve_model_string("categories.Category")
    return [(cat.id, cat.name) for cat in Category.objects.all()]