Skip to content

Forms

hypha.apply.stream_forms.forms

MixedFieldMetaclass

Bases: DeclarativeFieldsMetaclass

Stores all fields passed to the class and not just the field type. This allows the form to be rendered when Field-like blocks are passed in as part of the definition

StreamBaseForm

Bases: FileFormMixin, Form

swap_fields_for_display

swap_fields_for_display(func)
Source code in hypha/apply/stream_forms/forms.py
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

hidden_fields

hidden_fields()
Source code in hypha/apply/stream_forms/forms.py
def hidden_fields(self):
    # No hidden fields are returned by default because of MixedFieldMetaclass
    return [self[f] for f in self.fields.keys() if self[f].is_hidden]

delete_temporary_files

delete_temporary_files()

Overridden method of django_file_form's FileFormMixin, to handle multiple forms on the same page.

Source code in hypha/apply/stream_forms/forms.py
def delete_temporary_files(self):
    """
    Overridden method of django_file_form's FileFormMixin, to handle multiple forms on the same page.
    """
    form_id = self.data.getlist(self.add_prefix("form_id"))

    if not form_id:
        return

    form_id = form_id[0]
    for field_name, field in self.fields.items():
        if hasattr(field, "delete_file_data"):
            prefixed_field_name = self.add_prefix(field_name)
            field.delete_file_data(prefixed_field_name, form_id)

PageStreamBaseForm

Bases: BaseForm, StreamBaseForm

Adds page and user reference to the form class

swap_fields_for_display

swap_fields_for_display(func)
Source code in hypha/apply/stream_forms/forms.py
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

hidden_fields

hidden_fields()
Source code in hypha/apply/stream_forms/forms.py
def hidden_fields(self):
    # No hidden fields are returned by default because of MixedFieldMetaclass
    return [self[f] for f in self.fields.keys() if self[f].is_hidden]

delete_temporary_files

delete_temporary_files()

Overridden method of django_file_form's FileFormMixin, to handle multiple forms on the same page.

Source code in hypha/apply/stream_forms/forms.py
def delete_temporary_files(self):
    """
    Overridden method of django_file_form's FileFormMixin, to handle multiple forms on the same page.
    """
    form_id = self.data.getlist(self.add_prefix("form_id"))

    if not form_id:
        return

    form_id = form_id[0]
    for field_name, field in self.fields.items():
        if hasattr(field, "delete_file_data"):
            prefixed_field_name = self.add_prefix(field_name)
            field.delete_file_data(prefixed_field_name, form_id)

clean

clean()
Source code in hypha/apply/stream_forms/forms.py
def clean(self):
    cleaned_data = super().clean()
    for field, value in self.fields.items():
        # email validation of submission form
        if isinstance(value, EmailField):
            email = self.data.get(field)
            if email:
                is_registered, _ = is_user_already_registered(
                    email=self.data.get(field)
                )
                if is_registered:
                    user = get_user_by_email(email=email)
                    if not user:
                        self.add_error(field, "Found multiple account")
                        raise ValidationError(
                            mark_safe(
                                "Found multiple account for the same email. "
                                "Please login with the correct credentials or "
                                '<a href="mailto:{}">'
                                "contact to the support team"
                                "</a>.".format(settings.ORG_EMAIL)
                            )
                        )

                    elif not user.is_active:
                        self.add_error(field, "Found an inactive account")
                        raise ValidationError(
                            mark_safe(
                                "Found an inactive account for the same email. "
                                "Please use different email or "
                                '<a href="mailto:{}">'
                                "contact to the support team"
                                "</a>.".format(settings.ORG_EMAIL)
                            )
                        )

    return cleaned_data

BlockFieldWrapper

BlockFieldWrapper(block)

Wraps stream blocks so that they can be rendered as a field within a form

Source code in hypha/apply/stream_forms/forms.py
def __init__(self, block):
    self.block = block

is_hidden class-attribute instance-attribute

is_hidden = False

label class-attribute instance-attribute

label = None

help_text class-attribute instance-attribute

help_text = None

block instance-attribute

block = block

errors property

errors

html_name property

html_name

get_bound_field

get_bound_field(*args, **kwargs)
Source code in hypha/apply/stream_forms/forms.py
def get_bound_field(self, *args, **kwargs):
    return self

css_classes

css_classes()
Source code in hypha/apply/stream_forms/forms.py
def css_classes(self):
    return []