Example #1
0
    def set_up_fields(self, form, is_edition):
        for field_descriptor in self.fields:
            if is_actual_model_field(field_descriptor):
                # by default person fields are required
                form.fields[
                    field_descriptor["id"]].required = field_descriptor.get(
                        "required", True)
                if is_edition:
                    form.fields[field_descriptor[
                        "id"]].disabled = not field_descriptor.get(
                            "editable", False)

                if field_descriptor["id"] == "date_of_birth":
                    form.fields[field_descriptor["id"]].help_text = _(
                        "Format JJ/MM/AAAA")
            else:
                form.fields[field_descriptor["id"]] = get_form_field(
                    field_descriptor, is_edition, form.instance)

        fields = (Row(FullCol(field_descriptor["id"]))
                  for field_descriptor in self.fields)

        if self.intro_html is not None:
            fields = (HTML(self.intro_html), *fields)

        form.helper.layout.append(Fieldset(self.title, *fields))
def get_people_form_class(person_form_instance,
                          base_form=BasePersonForm,
                          include_inherited_model_fields=False):
    """Returns the form class for the specific person_form_instance

    :param person_form_instance: the person_form model object for which the form class must be generated
    :param base_form: an optional base form to use instead of the default BasePersonForm
    :return: a form class that can be used to generate a form for the person_form_instance
    """
    # the list of 'person_fields' that will also be saved on the person model when saving the form
    form_person_fields = [
        field["id"] for fieldset in person_form_instance.custom_fields
        for field in fieldset.get("fields", []) if is_actual_model_field(field)
    ]

    if include_inherited_model_fields is not None:
        form_person_fields.extend(base_form._meta.fields)

    form_class = forms.modelform_factory(Person,
                                         fields=form_person_fields,
                                         form=base_form)

    form_class.person_form_instance = person_form_instance

    return form_class
Example #3
0
    def additional_billing_fields(self):
        form_person_fields = {
            f
            for f, descriptor in
            self.event.subscription_form.fields_dict.items()
            if is_actual_model_field(descriptor)
        }

        return [
            f for f in NewParticipantForm._meta.fields
            if f not in form_person_fields
        ] + ["payment_mode"]
def validate_custom_fields(custom_fields):
    if not isinstance(custom_fields, list):
        raise ValidationError("La valeur doit être une liste")
    for k, fieldset in enumerate(custom_fields):
        if not (fieldset.get("title")):
            raise ValidationError(f"La section n°{k+1} n'a pas de titre !")

        title = fieldset["title"]

        if "fields" in fieldset:
            if not isinstance(fieldset["fields"], list):
                raise ValidationError(
                    f"La section '{title}' n'a pas de champs !")

            for i, field in enumerate(fieldset["fields"]):
                if field["id"] == "location":
                    initial_field = fieldset["fields"].pop(i)
                    for location_field in [
                            "location_country",
                            "location_state",
                            "location_city",
                            "location_zip",
                            "location_address2",
                            "location_address1",
                    ]:
                        fieldset["fields"].insert(
                            i,
                            {
                                "id":
                                location_field,
                                "person_field":
                                True,
                                "required":
                                False if location_field == "location_address2"
                                else initial_field.get("required", True),
                            },
                        )
                    continue
                if is_actual_model_field(field):
                    continue
                elif not field.get("label") and not field.get("type"):
                    raise ValidationError(
                        f"Section {title}: le champ n°{i+1} n'a ni label ni type"
                    )
                elif not field.get("label"):
                    raise ValidationError(
                        f"Section {title}: le champ n°{i+1} (de type {field['type']}) n'a pas de label"
                    )
                elif not field.get("type"):
                    raise ValidationError(
                        f"Section {title}: le champ {field['label']} n'a pas de type"
                    )
    def set_up_fields(self, form, is_edition):
        for field_descriptor in self.fields:
            if is_actual_model_field(field_descriptor):
                # Allow overriding of default form field by setting type on the field config
                if field_descriptor.get("type"):
                    if not field_descriptor.get("label"):
                        field_descriptor["label"] = str(
                            form.fields[field_descriptor["id"]].label
                        )
                    form.fields[field_descriptor["id"]] = get_form_field(
                        field_descriptor, is_edition, form.instance
                    )

                # by default person fields are required
                form.fields[field_descriptor["id"]].required = field_descriptor.get(
                    "required", True
                )
                if is_edition:
                    form.fields[
                        field_descriptor["id"]
                    ].disabled = not field_descriptor.get("editable", False)

                if field_descriptor["id"] == "date_of_birth":
                    form.fields[field_descriptor["id"]].help_text = _(
                        "Format JJ/MM/AAAA"
                    )

                if field_descriptor["id"] == "newsletters":
                    form.fields[
                        field_descriptor["id"]
                    ].initial = form.instance.newsletters

            else:
                form.fields[field_descriptor["id"]] = get_form_field(
                    field_descriptor, is_edition, form.instance
                )

        fields = (
            Row(FullCol(field_descriptor["id"])) for field_descriptor in self.fields
        )

        if self.intro_html is not None:
            fields = (HTML(self.intro_html), *fields)

        form.helper.layout.append(Fieldset(self.title, *fields))
Example #6
0
def validate_custom_fields(custom_fields):
    if not isinstance(custom_fields, list):
        raise ValidationError("La valeur doit être une liste")
    for fieldset in custom_fields:
        if not (fieldset.get("title")
                and isinstance(fieldset["fields"], list)):
            raise ValidationError(
                'Les sections doivent avoir un "title" et une liste "fields"')

        for i, field in enumerate(fieldset["fields"]):
            if field["id"] == "location":
                initial_field = fieldset["fields"].pop(i)
                for location_field in [
                        "location_country",
                        "location_state",
                        "location_city",
                        "location_zip",
                        "location_address2",
                        "location_address1",
                ]:
                    fieldset["fields"].insert(
                        i,
                        {
                            "id":
                            location_field,
                            "person_field":
                            True,
                            "required":
                            False if location_field == "location_address2" else
                            initial_field.get("required", True),
                        },
                    )
                continue
            if is_actual_model_field(field):
                continue
            elif not field.get("label") or not field.get("type"):
                raise ValidationError(
                    "Les champs doivent avoir un label et un type")