Exemplo n.º 1
0
    def test_unsaved_fields_in_form_builder_formfields(self):
        """Ensure unsaved FormField instances are added to FormBuilder.formfields dict
        with a clean_name as the key.
        """
        unsaved_field_1 = FormField(
            page=self.form_page,
            sort_order=14,
            label="Unsaved field 1",
            field_type="singleline",
            required=True,
        )
        self.form_page.form_fields.add(unsaved_field_1)

        unsaved_field_2 = FormField(
            page=self.form_page,
            sort_order=15,
            label="Unsaved field 2",
            field_type="singleline",
            required=True,
        )
        self.form_page.form_fields.add(unsaved_field_2)

        fb = FormBuilder(self.form_page.get_form_fields())
        self.assertIn(get_field_clean_name(unsaved_field_1.label), fb.formfields)
        self.assertIn(get_field_clean_name(unsaved_field_2.label), fb.formfields)
Exemplo n.º 2
0
    def __init__(self, *args, user=None, **kwargs):
        super().__init__(*args, **kwargs)
        if not user:
            raise ValueError(_('Please provide user object'))
        site_settings = get_site_settings_from_default_site()
        usermeta_dict = {}
        for um in user.metas.all():
            usermeta_dict[um.field_key] = um.field_value

        # construct user meta fields from site settings configuration
        usermetafields = site_settings.user_meta_fields.all()
        fb = FormBuilder(usermetafields)
        for key, val in fb.formfields.items():
            if isinstance(val, forms.MultipleChoiceField):
                self.fields["usermetalist_"+key] = val
                self.fields["usermetalist_" +
                            key].initial = usermeta_dict[key].split(',\n') if key in usermeta_dict else None
            else:
                self.fields["usermeta_"+key] = val
                self.fields["usermeta_" +
                            key].initial = usermeta_dict[key] if key in usermeta_dict else None

        # Fill up values
        self.fields["first_name"].widget.attrs['value'] = user.first_name
        self.fields["last_name"].widget.attrs['value'] = user.last_name
        self.fields["email"].widget.attrs['value'] = user.email
        self.fields["email"].widget.attrs['disabled'] = True
        self.fields["email"].label += ' ' + \
            str(user.email_verification_status())
        self.fields["opt_in_mailing_list"].initial = user.opt_in_mailing_list
        self.fields["language_preference"].initial = user.language_preference
Exemplo n.º 3
0
    def submit_form(self, request, pk):
        # Get the form
        instance = self.get_object()
        if not instance.live:
            raise ValidationError(
                detail=_("Submissions to unpublished forms are not allowed."))
        builder = FormBuilder(instance.form_fields.all())

        # Get the user or create a new one
        UserModel = get_user_model()
        uuid = request.data.get("uuid", None)
        user = None
        if uuid:
            user = UserModel.objects.get_or_create(username=uuid)[0]
            request.user = user

        if not user and not instance.allow_anonymous_submissions:
            raise ValidationError(
                detail="Anonymous submissions not allowed. Please send uuid."
            )

        if user and not instance.allow_multiple_submissions_per_user:
            if instance.has_user_submitted_form(request, instance.id):
                raise ValidationError(
                    detail="User has already submitted. "
                    "Multiple submissions not allowed."
                )

        # Populate the form with the submitted data
        form_class = builder.get_form_class()
        form = form_class(request.data)
        form.user = request.user

        # Validate and create the submission
        if form.is_valid():
            instance.process_form_submission(form)
            return Response(form.cleaned_data, status=status.HTTP_201_CREATED)
        else:
            raise ValidationError(detail=form.errors)
Exemplo n.º 4
0
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        site_settings = get_site_settings_from_default_site()
        # manually casting signup_footer_text from LazyI18nString to str to avoid the "richtext expects a string" error in the template
        self.footer_html = str(site_settings.signup_footer_text)

        # construct user meta fields from site settings configuration
        usermetafields = site_settings.user_meta_fields.all()
        fb = FormBuilder(usermetafields)
        for key, val in fb.formfields.items():
            if isinstance(val, forms.MultipleChoiceField):
                self.fields["usermetalist_" + key] = val
            else:
                self.fields["usermeta_" + key] = val
Exemplo n.º 5
0
    def setUp(self):
        # Create a form page
        home_page = Page.objects.get(url_path="/home/")

        self.form_page = home_page.add_child(
            instance=FormPage(
                title="Contact us",
                slug="contact-us",
                to_address="*****@*****.**",
                from_address="*****@*****.**",
                subject="The subject",
            )
        )

        FormField.objects.create(
            page=self.form_page,
            sort_order=1,
            label="Your name",
            field_type="singleline",
            required=True,
        )
        FormField.objects.create(
            page=self.form_page,
            sort_order=2,
            label="Your message",
            field_type="multiline",
            required=True,
        )
        FormField.objects.create(
            page=self.form_page,
            sort_order=2,
            label="Your birthday",
            field_type="date",
            required=True,
        )
        FormField.objects.create(
            page=self.form_page,
            sort_order=2,
            label="Your birthtime :)",
            field_type="datetime",
            required=True,
        )
        FormField.objects.create(
            page=self.form_page,
            sort_order=1,
            label="Your email",
            field_type="email",
            required=True,
        )
        FormField.objects.create(
            page=self.form_page,
            sort_order=2,
            label="Your homepage",
            field_type="url",
            required=True,
        )
        FormField.objects.create(
            page=self.form_page,
            sort_order=2,
            label="Your favourite number",
            field_type="number",
            required=True,
        )
        FormField.objects.create(
            page=self.form_page,
            sort_order=2,
            label="Your favourite text editors",
            field_type="multiselect",
            required=True,
            choices="vim,nano,emacs",
        )
        FormField.objects.create(
            page=self.form_page,
            sort_order=2,
            label="Your favourite Python IDEs",
            field_type="dropdown",
            required=True,
            choices="PyCharm,vim,nano",
        )
        FormField.objects.create(
            page=self.form_page,
            sort_order=2,
            label="Ὕour favourite Ρython ÏÐÈ",  # unicode example
            help_text="Choose one",
            field_type="radio",
            required=True,
            choices="PyCharm,vim,nano",
        )
        FormField.objects.create(
            page=self.form_page,
            sort_order=3,
            label="Your choices",
            field_type="checkboxes",
            required=False,
            choices="foo,bar,baz",
        )
        FormField.objects.create(
            page=self.form_page,
            sort_order=3,
            label="I agree to the Terms of Use",
            field_type="checkbox",
            required=True,
        )
        FormField.objects.create(
            page=self.form_page,
            sort_order=1,
            label="A Hidden Field",
            field_type="hidden",
            required=False,
        )

        # Create a form builder
        self.fb = FormBuilder(self.form_page.get_form_fields())
Exemplo n.º 6
0
class TestFormBuilder(TestCase):
    def setUp(self):
        # Create a form page
        home_page = Page.objects.get(url_path="/home/")

        self.form_page = home_page.add_child(
            instance=FormPage(
                title="Contact us",
                slug="contact-us",
                to_address="*****@*****.**",
                from_address="*****@*****.**",
                subject="The subject",
            )
        )

        FormField.objects.create(
            page=self.form_page,
            sort_order=1,
            label="Your name",
            field_type="singleline",
            required=True,
        )
        FormField.objects.create(
            page=self.form_page,
            sort_order=2,
            label="Your message",
            field_type="multiline",
            required=True,
        )
        FormField.objects.create(
            page=self.form_page,
            sort_order=2,
            label="Your birthday",
            field_type="date",
            required=True,
        )
        FormField.objects.create(
            page=self.form_page,
            sort_order=2,
            label="Your birthtime :)",
            field_type="datetime",
            required=True,
        )
        FormField.objects.create(
            page=self.form_page,
            sort_order=1,
            label="Your email",
            field_type="email",
            required=True,
        )
        FormField.objects.create(
            page=self.form_page,
            sort_order=2,
            label="Your homepage",
            field_type="url",
            required=True,
        )
        FormField.objects.create(
            page=self.form_page,
            sort_order=2,
            label="Your favourite number",
            field_type="number",
            required=True,
        )
        FormField.objects.create(
            page=self.form_page,
            sort_order=2,
            label="Your favourite text editors",
            field_type="multiselect",
            required=True,
            choices="vim,nano,emacs",
        )
        FormField.objects.create(
            page=self.form_page,
            sort_order=2,
            label="Your favourite Python IDEs",
            field_type="dropdown",
            required=True,
            choices="PyCharm,vim,nano",
        )
        FormField.objects.create(
            page=self.form_page,
            sort_order=2,
            label="Ὕour favourite Ρython ÏÐÈ",  # unicode example
            help_text="Choose one",
            field_type="radio",
            required=True,
            choices="PyCharm,vim,nano",
        )
        FormField.objects.create(
            page=self.form_page,
            sort_order=3,
            label="Your choices",
            field_type="checkboxes",
            required=False,
            choices="foo,bar,baz",
        )
        FormField.objects.create(
            page=self.form_page,
            sort_order=3,
            label="I agree to the Terms of Use",
            field_type="checkbox",
            required=True,
        )
        FormField.objects.create(
            page=self.form_page,
            sort_order=1,
            label="A Hidden Field",
            field_type="hidden",
            required=False,
        )

        # Create a form builder
        self.fb = FormBuilder(self.form_page.get_form_fields())

    def test_fields(self):
        """
        This tests that all fields were added to the form with the correct types
        """
        form_class = self.fb.get_form_class()

        # All fields are present in form
        field_names = form_class.base_fields.keys()
        self.assertIn("your_name", field_names)
        self.assertIn("your_message", field_names)
        self.assertIn("your_birthday", field_names)
        self.assertIn("your_birthtime", field_names)
        self.assertIn("your_email", field_names)
        self.assertIn("your_homepage", field_names)
        self.assertIn("your_favourite_number", field_names)
        self.assertIn("your_favourite_text_editors", field_names)
        self.assertIn("your_favourite_python_ides", field_names)
        self.assertIn("u03a5our_favourite_u03a1ython_ixd0e", field_names)
        self.assertIn("your_choices", field_names)
        self.assertIn("i_agree_to_the_terms_of_use", field_names)
        self.assertIn("a_hidden_field", field_names)

        # All fields have proper type
        self.assertIsInstance(form_class.base_fields["your_name"], forms.CharField)
        self.assertIsInstance(form_class.base_fields["your_message"], forms.CharField)
        self.assertIsInstance(form_class.base_fields["your_birthday"], forms.DateField)
        self.assertIsInstance(
            form_class.base_fields["your_birthtime"], forms.DateTimeField
        )
        self.assertIsInstance(form_class.base_fields["your_email"], forms.EmailField)
        self.assertIsInstance(form_class.base_fields["your_homepage"], forms.URLField)
        self.assertIsInstance(
            form_class.base_fields["your_favourite_number"], forms.DecimalField
        )
        self.assertIsInstance(
            form_class.base_fields["your_favourite_text_editors"],
            forms.MultipleChoiceField,
        )
        self.assertIsInstance(
            form_class.base_fields["your_favourite_python_ides"], forms.ChoiceField
        )
        self.assertIsInstance(
            form_class.base_fields["u03a5our_favourite_u03a1ython_ixd0e"],
            forms.ChoiceField,
        )
        self.assertIsInstance(
            form_class.base_fields["your_choices"], forms.MultipleChoiceField
        )
        self.assertIsInstance(
            form_class.base_fields["i_agree_to_the_terms_of_use"], forms.BooleanField
        )
        self.assertIsInstance(form_class.base_fields["a_hidden_field"], forms.CharField)

        # Some fields have non-default widgets
        self.assertIsInstance(
            form_class.base_fields["your_message"].widget, forms.Textarea
        )
        self.assertIsInstance(
            form_class.base_fields["u03a5our_favourite_u03a1ython_ixd0e"].widget,
            forms.RadioSelect,
        )
        self.assertIsInstance(
            form_class.base_fields["your_choices"].widget, forms.CheckboxSelectMultiple
        )
        self.assertIsInstance(
            form_class.base_fields["a_hidden_field"].widget, forms.HiddenInput
        )

    def test_unsaved_fields_in_form_builder_formfields(self):
        """Ensure unsaved FormField instances are added to FormBuilder.formfields dict
        with a clean_name as the key.
        """
        unsaved_field_1 = FormField(
            page=self.form_page,
            sort_order=14,
            label="Unsaved field 1",
            field_type="singleline",
            required=True,
        )
        self.form_page.form_fields.add(unsaved_field_1)

        unsaved_field_2 = FormField(
            page=self.form_page,
            sort_order=15,
            label="Unsaved field 2",
            field_type="singleline",
            required=True,
        )
        self.form_page.form_fields.add(unsaved_field_2)

        fb = FormBuilder(self.form_page.get_form_fields())
        self.assertIn(get_field_clean_name(unsaved_field_1.label), fb.formfields)
        self.assertIn(get_field_clean_name(unsaved_field_2.label), fb.formfields)
Exemplo n.º 7
0
class TestFormBuilder(TestCase):
    def setUp(self):
        # Create a form page
        home_page = Page.objects.get(url_path='/home/')

        self.form_page = home_page.add_child(instance=FormPage(
            title="Contact us",
            slug="contact-us",
            to_address="*****@*****.**",
            from_address="*****@*****.**",
            subject="The subject",
        ))

        FormField.objects.create(
            page=self.form_page,
            sort_order=1,
            label="Your name",
            field_type='singleline',
            required=True,
        )
        FormField.objects.create(
            page=self.form_page,
            sort_order=2,
            label="Your message",
            field_type='multiline',
            required=True,
        )
        FormField.objects.create(
            page=self.form_page,
            sort_order=2,
            label="Your birthday",
            field_type='date',
            required=True,
        )
        FormField.objects.create(
            page=self.form_page,
            sort_order=2,
            label="Your birthtime :)",
            field_type='datetime',
            required=True,
        )
        FormField.objects.create(
            page=self.form_page,
            sort_order=1,
            label="Your email",
            field_type='email',
            required=True,
        )
        FormField.objects.create(
            page=self.form_page,
            sort_order=2,
            label="Your homepage",
            field_type='url',
            required=True,
        )
        FormField.objects.create(
            page=self.form_page,
            sort_order=2,
            label="Your favourite number",
            field_type='number',
            required=True,
        )
        FormField.objects.create(
            page=self.form_page,
            sort_order=2,
            label="Your favourite text editors",
            field_type='multiselect',
            required=True,
            choices='vim,nano,emacs',
        )
        FormField.objects.create(
            page=self.form_page,
            sort_order=2,
            label="Your favourite Python IDEs",
            field_type='dropdown',
            required=True,
            choices='PyCharm,vim,nano',
        )
        FormField.objects.create(
            page=self.form_page,
            sort_order=2,
            label="Ὕour favourite Ρython ÏÐÈ",  # unicode example
            help_text="Choose one",
            field_type='radio',
            required=True,
            choices='PyCharm,vim,nano',
        )
        FormField.objects.create(
            page=self.form_page,
            sort_order=3,
            label="Your choices",
            field_type='checkboxes',
            required=False,
            choices='foo,bar,baz',
        )
        FormField.objects.create(
            page=self.form_page,
            sort_order=3,
            label="I agree to the Terms of Use",
            field_type='checkbox',
            required=True,
        )
        FormField.objects.create(
            page=self.form_page,
            sort_order=1,
            label="A Hidden Field",
            field_type='hidden',
            required=False,
        )

        # Create a form builder
        self.fb = FormBuilder(self.form_page.get_form_fields())

    def test_fields(self):
        """
        This tests that all fields were added to the form with the correct types
        """
        form_class = self.fb.get_form_class()

        # All fields are present in form
        field_names = form_class.base_fields.keys()
        self.assertIn('your_name', field_names)
        self.assertIn('your_message', field_names)
        self.assertIn('your_birthday', field_names)
        self.assertIn('your_birthtime', field_names)
        self.assertIn('your_email', field_names)
        self.assertIn('your_homepage', field_names)
        self.assertIn('your_favourite_number', field_names)
        self.assertIn('your_favourite_text_editors', field_names)
        self.assertIn('your_favourite_python_ides', field_names)
        self.assertIn('u03a5our_favourite_u03a1ython_ixd0e', field_names)
        self.assertIn('your_choices', field_names)
        self.assertIn('i_agree_to_the_terms_of_use', field_names)
        self.assertIn('a_hidden_field', field_names)

        # All fields have proper type
        self.assertIsInstance(form_class.base_fields['your_name'],
                              forms.CharField)
        self.assertIsInstance(form_class.base_fields['your_message'],
                              forms.CharField)
        self.assertIsInstance(form_class.base_fields['your_birthday'],
                              forms.DateField)
        self.assertIsInstance(form_class.base_fields['your_birthtime'],
                              forms.DateTimeField)
        self.assertIsInstance(form_class.base_fields['your_email'],
                              forms.EmailField)
        self.assertIsInstance(form_class.base_fields['your_homepage'],
                              forms.URLField)
        self.assertIsInstance(form_class.base_fields['your_favourite_number'],
                              forms.DecimalField)
        self.assertIsInstance(
            form_class.base_fields['your_favourite_text_editors'],
            forms.MultipleChoiceField)
        self.assertIsInstance(
            form_class.base_fields['your_favourite_python_ides'],
            forms.ChoiceField)
        self.assertIsInstance(
            form_class.base_fields['u03a5our_favourite_u03a1ython_ixd0e'],
            forms.ChoiceField)
        self.assertIsInstance(form_class.base_fields['your_choices'],
                              forms.MultipleChoiceField)
        self.assertIsInstance(
            form_class.base_fields['i_agree_to_the_terms_of_use'],
            forms.BooleanField)
        self.assertIsInstance(form_class.base_fields['a_hidden_field'],
                              forms.CharField)

        # Some fields have non-default widgets
        self.assertIsInstance(form_class.base_fields['your_message'].widget,
                              forms.Textarea)
        self.assertIsInstance(
            form_class.base_fields['u03a5our_favourite_u03a1ython_ixd0e'].
            widget, forms.RadioSelect)
        self.assertIsInstance(form_class.base_fields['your_choices'].widget,
                              forms.CheckboxSelectMultiple)
        self.assertIsInstance(form_class.base_fields['a_hidden_field'].widget,
                              forms.HiddenInput)
Exemplo n.º 8
0
    def test_newline_value_separation_in_choices_and_default_value_fields(
            self):
        """Ensure that the new line present between input choices or values gets formatted into choices or value list
        respectively as an alternative to commas.
        """
        multiselect_field = FormField.objects.create(
            page=self.form_page,
            sort_order=2,
            label="Your favorite colours",
            field_type="multiselect",
            required=True,
            choices="red\r\nblue\r\ngreen",
        )
        self.form_page.form_fields.add(multiselect_field)

        dropdown_field = FormField.objects.create(
            page=self.form_page,
            sort_order=2,
            label="Pick your next destination",
            field_type="dropdown",
            required=True,
            choices="hawaii\r\nparis\r\nabuja",
        )
        self.form_page.form_fields.add(dropdown_field)

        checkboxes_field = FormField.objects.create(
            page=self.form_page,
            sort_order=3,
            label="Do you possess these attributes",
            field_type="checkboxes",
            required=False,
            choices="good, kind and gentle.\r\nstrong, bold and brave.",
        )
        self.form_page.form_fields.add(checkboxes_field)

        radio_field = FormField.objects.create(
            page=self.form_page,
            sort_order=2,
            label="Your favorite animal",
            help_text="Choose one",
            field_type="radio",
            required=True,
            choices="cat\r\ndog\r\nbird",
        )
        self.form_page.form_fields.add(radio_field)

        checkboxes_field_with_default_value = FormField.objects.create(
            page=self.form_page,
            sort_order=3,
            label="Choose the correct answer",
            field_type="checkboxes",
            required=False,
            choices="a\r\nb\r\nc",
            default_value="a\r\nc",
        )
        self.form_page.form_fields.add(checkboxes_field_with_default_value)

        fb = FormBuilder(self.form_page.get_form_fields())
        form_class = fb.get_form_class()

        self.assertEqual(
            [("red", "red"), ("blue", "blue"), ("green", "green")],
            form_class.base_fields["your_favorite_colours"].choices,
        )
        self.assertEqual(
            [("cat", "cat"), ("dog", "dog"), ("bird", "bird")],
            form_class.base_fields["your_favorite_animal"].choices,
        )
        self.assertEqual(
            [
                ("good, kind and gentle.", "good, kind and gentle."),
                ("strong, bold and brave.", "strong, bold and brave."),
            ],
            form_class.base_fields["do_you_possess_these_attributes"].choices,
        )
        self.assertEqual(
            [("hawaii", "hawaii"), ("paris", "paris"), ("abuja", "abuja")],
            form_class.base_fields["pick_your_next_destination"].choices,
        )
        self.assertEqual(
            ["a", "c"],
            form_class.base_fields["choose_the_correct_answer"].initial,
        )
Exemplo n.º 9
0
class TestFormBuilder(TestCase):
    def setUp(self):
        # Create a form page
        home_page = Page.objects.get(url_path="/home/")

        self.form_page = home_page.add_child(instance=FormPage(
            title="Contact us",
            slug="contact-us",
            to_address="*****@*****.**",
            from_address="*****@*****.**",
            subject="The subject",
        ))

        FormField.objects.create(
            page=self.form_page,
            sort_order=1,
            label="Your name",
            field_type="singleline",
            required=True,
        )
        FormField.objects.create(
            page=self.form_page,
            sort_order=2,
            label="Your message",
            field_type="multiline",
            required=True,
        )
        FormField.objects.create(
            page=self.form_page,
            sort_order=2,
            label="Your birthday",
            field_type="date",
            required=True,
        )
        FormField.objects.create(
            page=self.form_page,
            sort_order=2,
            label="Your birthtime :)",
            field_type="datetime",
            required=True,
        )
        FormField.objects.create(
            page=self.form_page,
            sort_order=1,
            label="Your email",
            field_type="email",
            required=True,
        )
        FormField.objects.create(
            page=self.form_page,
            sort_order=2,
            label="Your homepage",
            field_type="url",
            required=True,
        )
        FormField.objects.create(
            page=self.form_page,
            sort_order=2,
            label="Your favourite number",
            field_type="number",
            required=True,
        )
        FormField.objects.create(
            page=self.form_page,
            sort_order=2,
            label="Your favourite text editors",
            field_type="multiselect",
            required=True,
            choices="vim,nano,emacs",
        )
        FormField.objects.create(
            page=self.form_page,
            sort_order=2,
            label="Your favourite Python IDEs",
            field_type="dropdown",
            required=True,
            choices="PyCharm,vim,nano",
        )
        FormField.objects.create(
            page=self.form_page,
            sort_order=2,
            label="Ὕour favourite Ρython ÏÐÈ",  # unicode example
            help_text="Choose one",
            field_type="radio",
            required=True,
            choices="PyCharm,vim,nano",
        )
        FormField.objects.create(
            page=self.form_page,
            sort_order=3,
            label="Your choices",
            field_type="checkboxes",
            required=False,
            choices="foo,bar,baz",
        )
        FormField.objects.create(
            page=self.form_page,
            sort_order=3,
            label="I agree to the Terms of Use",
            field_type="checkbox",
            required=True,
        )
        FormField.objects.create(
            page=self.form_page,
            sort_order=1,
            label="A Hidden Field",
            field_type="hidden",
            required=False,
        )

        # Create a form builder
        self.fb = FormBuilder(self.form_page.get_form_fields())

    def test_fields(self):
        """
        This tests that all fields were added to the form with the correct types
        """
        form_class = self.fb.get_form_class()

        # All fields are present in form
        field_names = form_class.base_fields.keys()
        self.assertIn("your_name", field_names)
        self.assertIn("your_message", field_names)
        self.assertIn("your_birthday", field_names)
        self.assertIn("your_birthtime", field_names)
        self.assertIn("your_email", field_names)
        self.assertIn("your_homepage", field_names)
        self.assertIn("your_favourite_number", field_names)
        self.assertIn("your_favourite_text_editors", field_names)
        self.assertIn("your_favourite_python_ides", field_names)
        self.assertIn("u03a5our_favourite_u03a1ython_ixd0e", field_names)
        self.assertIn("your_choices", field_names)
        self.assertIn("i_agree_to_the_terms_of_use", field_names)
        self.assertIn("a_hidden_field", field_names)

        # All fields have proper type
        self.assertIsInstance(form_class.base_fields["your_name"],
                              forms.CharField)
        self.assertIsInstance(form_class.base_fields["your_message"],
                              forms.CharField)
        self.assertIsInstance(form_class.base_fields["your_birthday"],
                              forms.DateField)
        self.assertIsInstance(form_class.base_fields["your_birthtime"],
                              forms.DateTimeField)
        self.assertIsInstance(form_class.base_fields["your_email"],
                              forms.EmailField)
        self.assertIsInstance(form_class.base_fields["your_homepage"],
                              forms.URLField)
        self.assertIsInstance(form_class.base_fields["your_favourite_number"],
                              forms.DecimalField)
        self.assertIsInstance(
            form_class.base_fields["your_favourite_text_editors"],
            forms.MultipleChoiceField,
        )
        self.assertIsInstance(
            form_class.base_fields["your_favourite_python_ides"],
            forms.ChoiceField)
        self.assertIsInstance(
            form_class.base_fields["u03a5our_favourite_u03a1ython_ixd0e"],
            forms.ChoiceField,
        )
        self.assertIsInstance(form_class.base_fields["your_choices"],
                              forms.MultipleChoiceField)
        self.assertIsInstance(
            form_class.base_fields["i_agree_to_the_terms_of_use"],
            forms.BooleanField)
        self.assertIsInstance(form_class.base_fields["a_hidden_field"],
                              forms.CharField)

        # Some fields have non-default widgets
        self.assertIsInstance(form_class.base_fields["your_message"].widget,
                              forms.Textarea)
        self.assertIsInstance(
            form_class.base_fields["u03a5our_favourite_u03a1ython_ixd0e"].
            widget,
            forms.RadioSelect,
        )
        self.assertIsInstance(form_class.base_fields["your_choices"].widget,
                              forms.CheckboxSelectMultiple)
        self.assertIsInstance(form_class.base_fields["a_hidden_field"].widget,
                              forms.HiddenInput)

    def test_unsaved_fields_in_form_builder_formfields(self):
        """Ensure unsaved FormField instances are added to FormBuilder.formfields dict
        with a clean_name as the key.
        """
        unsaved_field_1 = FormField(
            page=self.form_page,
            sort_order=14,
            label="Unsaved field 1",
            field_type="singleline",
            required=True,
        )
        self.form_page.form_fields.add(unsaved_field_1)

        unsaved_field_2 = FormField(
            page=self.form_page,
            sort_order=15,
            label="Unsaved field 2",
            field_type="singleline",
            required=True,
        )
        self.form_page.form_fields.add(unsaved_field_2)

        fb = FormBuilder(self.form_page.get_form_fields())
        self.assertIn(get_field_clean_name(unsaved_field_1.label),
                      fb.formfields)
        self.assertIn(get_field_clean_name(unsaved_field_2.label),
                      fb.formfields)

    def test_newline_value_separation_in_choices_and_default_value_fields(
            self):
        """Ensure that the new line present between input choices or values gets formatted into choices or value list
        respectively as an alternative to commas.
        """
        multiselect_field = FormField.objects.create(
            page=self.form_page,
            sort_order=2,
            label="Your favorite colours",
            field_type="multiselect",
            required=True,
            choices="red\r\nblue\r\ngreen",
        )
        self.form_page.form_fields.add(multiselect_field)

        dropdown_field = FormField.objects.create(
            page=self.form_page,
            sort_order=2,
            label="Pick your next destination",
            field_type="dropdown",
            required=True,
            choices="hawaii\r\nparis\r\nabuja",
        )
        self.form_page.form_fields.add(dropdown_field)

        checkboxes_field = FormField.objects.create(
            page=self.form_page,
            sort_order=3,
            label="Do you possess these attributes",
            field_type="checkboxes",
            required=False,
            choices="good, kind and gentle.\r\nstrong, bold and brave.",
        )
        self.form_page.form_fields.add(checkboxes_field)

        radio_field = FormField.objects.create(
            page=self.form_page,
            sort_order=2,
            label="Your favorite animal",
            help_text="Choose one",
            field_type="radio",
            required=True,
            choices="cat\r\ndog\r\nbird",
        )
        self.form_page.form_fields.add(radio_field)

        checkboxes_field_with_default_value = FormField.objects.create(
            page=self.form_page,
            sort_order=3,
            label="Choose the correct answer",
            field_type="checkboxes",
            required=False,
            choices="a\r\nb\r\nc",
            default_value="a\r\nc",
        )
        self.form_page.form_fields.add(checkboxes_field_with_default_value)

        fb = FormBuilder(self.form_page.get_form_fields())
        form_class = fb.get_form_class()

        self.assertEqual(
            [("red", "red"), ("blue", "blue"), ("green", "green")],
            form_class.base_fields["your_favorite_colours"].choices,
        )
        self.assertEqual(
            [("cat", "cat"), ("dog", "dog"), ("bird", "bird")],
            form_class.base_fields["your_favorite_animal"].choices,
        )
        self.assertEqual(
            [
                ("good, kind and gentle.", "good, kind and gentle."),
                ("strong, bold and brave.", "strong, bold and brave."),
            ],
            form_class.base_fields["do_you_possess_these_attributes"].choices,
        )
        self.assertEqual(
            [("hawaii", "hawaii"), ("paris", "paris"), ("abuja", "abuja")],
            form_class.base_fields["pick_your_next_destination"].choices,
        )
        self.assertEqual(
            ["a", "c"],
            form_class.base_fields["choose_the_correct_answer"].initial,
        )
Exemplo n.º 10
0
    def setUp(self):
        # Create a form page
        home_page = Page.objects.get(url_path='/home/')

        self.form_page = home_page.add_child(instance=FormPage(
            title="Contact us",
            slug="contact-us",
            to_address="*****@*****.**",
            from_address="*****@*****.**",
            subject="The subject",
        ))

        FormField.objects.create(
            page=self.form_page,
            sort_order=1,
            label="Your name",
            field_type='singleline',
            required=True,
        )
        FormField.objects.create(
            page=self.form_page,
            sort_order=2,
            label="Your message",
            field_type='multiline',
            required=True,
        )
        FormField.objects.create(
            page=self.form_page,
            sort_order=2,
            label="Your birthday",
            field_type='date',
            required=True,
        )
        FormField.objects.create(
            page=self.form_page,
            sort_order=2,
            label="Your birthtime :)",
            field_type='datetime',
            required=True,
        )
        FormField.objects.create(
            page=self.form_page,
            sort_order=1,
            label="Your email",
            field_type='email',
            required=True,
        )
        FormField.objects.create(
            page=self.form_page,
            sort_order=2,
            label="Your homepage",
            field_type='url',
            required=True,
        )
        FormField.objects.create(
            page=self.form_page,
            sort_order=2,
            label="Your favourite number",
            field_type='number',
            required=True,
        )
        FormField.objects.create(
            page=self.form_page,
            sort_order=2,
            label="Your favourite text editors",
            field_type='multiselect',
            required=True,
            choices='vim,nano,emacs',
        )
        FormField.objects.create(
            page=self.form_page,
            sort_order=2,
            label="Your favourite Python IDEs",
            field_type='dropdown',
            required=True,
            choices='PyCharm,vim,nano',
        )
        FormField.objects.create(
            page=self.form_page,
            sort_order=2,
            label="Your favourite Python IDE",
            help_text="Choose one",
            field_type='radio',
            required=True,
            choices='PyCharm,vim,nano',
        )
        FormField.objects.create(
            page=self.form_page,
            sort_order=3,
            label="Your choices",
            field_type='checkboxes',
            required=False,
            choices='foo,bar,baz',
        )
        FormField.objects.create(
            page=self.form_page,
            sort_order=3,
            label="I agree to the Terms of Use",
            field_type='checkbox',
            required=True,
        )
        FormField.objects.create(
            page=self.form_page,
            sort_order=1,
            label="A Hidden Field",
            field_type='hidden',
            required=False,
        )

        # Create a form builder
        self.fb = FormBuilder(self.form_page.get_form_fields())
Exemplo n.º 11
0
class TestFormBuilder(TestCase):
    def setUp(self):
        # Create a form page
        home_page = Page.objects.get(url_path='/home/')

        self.form_page = home_page.add_child(instance=FormPage(
            title="Contact us",
            slug="contact-us",
            to_address="*****@*****.**",
            from_address="*****@*****.**",
            subject="The subject",
        ))

        FormField.objects.create(
            page=self.form_page,
            sort_order=1,
            label="Your name",
            field_type='singleline',
            required=True,
        )
        FormField.objects.create(
            page=self.form_page,
            sort_order=2,
            label="Your message",
            field_type='multiline',
            required=True,
        )
        FormField.objects.create(
            page=self.form_page,
            sort_order=2,
            label="Your birthday",
            field_type='date',
            required=True,
        )
        FormField.objects.create(
            page=self.form_page,
            sort_order=2,
            label="Your birthtime :)",
            field_type='datetime',
            required=True,
        )
        FormField.objects.create(
            page=self.form_page,
            sort_order=1,
            label="Your email",
            field_type='email',
            required=True,
        )
        FormField.objects.create(
            page=self.form_page,
            sort_order=2,
            label="Your homepage",
            field_type='url',
            required=True,
        )
        FormField.objects.create(
            page=self.form_page,
            sort_order=2,
            label="Your favourite number",
            field_type='number',
            required=True,
        )
        FormField.objects.create(
            page=self.form_page,
            sort_order=2,
            label="Your favourite text editors",
            field_type='multiselect',
            required=True,
            choices='vim,nano,emacs',
        )
        FormField.objects.create(
            page=self.form_page,
            sort_order=2,
            label="Your favourite Python IDEs",
            field_type='dropdown',
            required=True,
            choices='PyCharm,vim,nano',
        )
        FormField.objects.create(
            page=self.form_page,
            sort_order=2,
            label="Your favourite Python IDE",
            help_text="Choose one",
            field_type='radio',
            required=True,
            choices='PyCharm,vim,nano',
        )
        FormField.objects.create(
            page=self.form_page,
            sort_order=3,
            label="Your choices",
            field_type='checkboxes',
            required=False,
            choices='foo,bar,baz',
        )
        FormField.objects.create(
            page=self.form_page,
            sort_order=3,
            label="I agree to the Terms of Use",
            field_type='checkbox',
            required=True,
        )
        FormField.objects.create(
            page=self.form_page,
            sort_order=1,
            label="A Hidden Field",
            field_type='hidden',
            required=False,
        )

        # Create a form builder
        self.fb = FormBuilder(self.form_page.get_form_fields())

    def test_fields(self):
        """
        This tests that all fields were added to the form with the correct types
        """
        form_class = self.fb.get_form_class()

        # All fields are present in form
        field_names = form_class.base_fields.keys()
        self.assertIn('your-name', field_names)
        self.assertIn('your-message', field_names)
        self.assertIn('your-birthday', field_names)
        self.assertIn('your-birthtime', field_names)
        self.assertIn('your-email', field_names)
        self.assertIn('your-homepage', field_names)
        self.assertIn('your-favourite-number', field_names)
        self.assertIn('your-favourite-text-editors', field_names)
        self.assertIn('your-favourite-python-ides', field_names)
        self.assertIn('your-favourite-python-ide', field_names)
        self.assertIn('your-choices', field_names)
        self.assertIn('i-agree-to-the-terms-of-use', field_names)
        self.assertIn('a-hidden-field', field_names)

        # All fields have proper type
        self.assertIsInstance(form_class.base_fields['your-name'], forms.CharField)
        self.assertIsInstance(form_class.base_fields['your-message'], forms.CharField)
        self.assertIsInstance(form_class.base_fields['your-birthday'], forms.DateField)
        self.assertIsInstance(form_class.base_fields['your-birthtime'], forms.DateTimeField)
        self.assertIsInstance(form_class.base_fields['your-email'], forms.EmailField)
        self.assertIsInstance(form_class.base_fields['your-homepage'], forms.URLField)
        self.assertIsInstance(form_class.base_fields['your-favourite-number'], forms.DecimalField)
        self.assertIsInstance(form_class.base_fields['your-favourite-text-editors'], forms.MultipleChoiceField)
        self.assertIsInstance(form_class.base_fields['your-favourite-python-ides'], forms.ChoiceField)
        self.assertIsInstance(form_class.base_fields['your-favourite-python-ide'], forms.ChoiceField)
        self.assertIsInstance(form_class.base_fields['your-choices'], forms.MultipleChoiceField)
        self.assertIsInstance(form_class.base_fields['i-agree-to-the-terms-of-use'], forms.BooleanField)
        self.assertIsInstance(form_class.base_fields['a-hidden-field'], forms.CharField)

        # Some fields have non-default widgets
        self.assertIsInstance(form_class.base_fields['your-message'].widget, forms.Textarea)
        self.assertIsInstance(form_class.base_fields['your-favourite-python-ide'].widget, forms.RadioSelect)
        self.assertIsInstance(form_class.base_fields['your-choices'].widget, forms.CheckboxSelectMultiple)
        self.assertIsInstance(form_class.base_fields['a-hidden-field'].widget, forms.HiddenInput)
Exemplo n.º 12
0
    def setUp(self):
        # Create a form page
        home_page = Page.objects.get(url_path='/home/')

        self.form_page = home_page.add_child(instance=FormPage(
            title="Contact us",
            slug="contact-us",
            to_address="*****@*****.**",
            from_address="*****@*****.**",
            subject="The subject",
        ))

        FormField.objects.create(
            page=self.form_page,
            sort_order=1,
            label="Your name",
            field_type='singleline',
            required=True,
        )
        FormField.objects.create(
            page=self.form_page,
            sort_order=2,
            label="Your message",
            field_type='multiline',
            required=True,
        )
        FormField.objects.create(
            page=self.form_page,
            sort_order=2,
            label="Your birthday",
            field_type='date',
            required=True,
        )
        FormField.objects.create(
            page=self.form_page,
            sort_order=2,
            label="Your birthtime :)",
            field_type='datetime',
            required=True,
        )
        FormField.objects.create(
            page=self.form_page,
            sort_order=1,
            label="Your email",
            field_type='email',
            required=True,
        )
        FormField.objects.create(
            page=self.form_page,
            sort_order=2,
            label="Your homepage",
            field_type='url',
            required=True,
        )
        FormField.objects.create(
            page=self.form_page,
            sort_order=2,
            label="Your favourite number",
            field_type='number',
            required=True,
        )
        FormField.objects.create(
            page=self.form_page,
            sort_order=2,
            label="Your favourite text editors",
            field_type='multiselect',
            required=True,
            choices='vim,nano,emacs',
        )
        FormField.objects.create(
            page=self.form_page,
            sort_order=2,
            label="Your favourite Python IDEs",
            field_type='dropdown',
            required=True,
            choices='PyCharm,vim,nano',
        )
        FormField.objects.create(
            page=self.form_page,
            sort_order=2,
            label="Your favourite Python IDE",
            help_text="Choose one",
            field_type='radio',
            required=True,
            choices='PyCharm,vim,nano',
        )
        FormField.objects.create(
            page=self.form_page,
            sort_order=3,
            label="Your choices",
            field_type='checkboxes',
            required=False,
            choices='foo,bar,baz',
        )
        FormField.objects.create(
            page=self.form_page,
            sort_order=3,
            label="I agree to the Terms of Use",
            field_type='checkbox',
            required=True,
        )
        FormField.objects.create(
            page=self.form_page,
            sort_order=1,
            label="A Hidden Field",
            field_type='hidden',
            required=False,
        )

        # Create a form builder
        self.fb = FormBuilder(self.form_page.get_form_fields())