Esempio n. 1
0
    def test_edit_record_validation_fail(self):
        # User is editing an existing a new record.
        # Test the case where a user specifies an
        # image in the ImageField but some other
        # field fails validation.   We want this field
        # to reset since at this point there is no
        # way for this simiple field type to save to
        # keep the image that was specified at this
        # point.  Next, the user fixes the field that failed
        # and does not re-specify the image, and the validation
        # should pass but there is no image.
        fields = Fields(
            TextField('Title', v.MinimumLength(10)),
            ImageField('Photo'),
            URLField('Site'),
        )
        client = zoom.utils.Record(
            title='Old Company Name',
            photo=b'old photo',
            site='http://localhost',
            url='/clients/dsi'
        )
        fields.initialize(client)
        self.assertTrue(fields.valid())

        form_data = dict(
            title='DSI',
            photo=b'new photo',
            site='http://localhost',
            url='/clients/dsi'
        )
        self.assertFalse(fields.validate(form_data))

        # The record has been rejected because
        # of the title length.  The image field has not had
        # the chance to save anything so there is nothing to
        # show.  With this simple field it's not smart enough
        # to save the image anywhere so it goes back to being
        # empty.
        self.assertEqual('minimum length 10', fields['title'].msg)
        self.assertEqual(b'old photo', fields['photo'].value)
        target = (
            '<input '
                'class="image-field" '
                'id="photo" '
                'name="photo" '
                'type="file" '
            '/>'
            '<div class="image-field-delete-link">'
                '<a href="delete-image?name=photo">delete photo</a>'
            '</div>'
            '<img '
                'alt="Photo" '
                'class="image-field-image" '
                'src="/clients/dsi/image?name=photo" '
            '/>'
        )
        self.assertEqual(target, fields['photo'].widget())
Esempio n. 2
0
    def test_create_record_validation_fail_and_retry(self):
        # Test the case where a user specifies an
        # image in the ImageField but some other
        # field fails validation.   We want this field
        # to reset since at this point there is no
        # way for this simiple field type to save to
        # keep the image that was specified at this
        # point.  Next, the user fixes the field that failed
        # and re-specifies the image, and the validation
        # should pass.
        fields = Fields(
            TextField('Title', v.MinimumLength(10)),
            ImageField('Photo'),
            URLField('Site'),
        )
        form_data = dict(
            title='DSI',
            photo=zoom.utils.Bunch(value=b'fake photo'),
            site='http://localhost',
            url='/clients/dsi'
        )
        self.assertFalse(fields.validate(form_data))

        # The record has been rejected because
        # of the title length.  The image field has not had
        # the chance to save anything so there is nothing to
        # show.  With this simple field it's not smart enough
        # to save the image anywhere so it goes back to being
        # empty.
        self.assertEqual('minimum length 10', fields['title'].msg)
        self.assertEqual(None, fields['photo'].value)
        # self.assertEqual(b'fake photo', fields['photo'].value)
        photo = fields['photo']
        target = (
            '<input '
                'class="image-field" '
                'id="photo" '
                'name="photo" '
                'type="file" '
            '/>'
        )
        self.assertEqual(target, fields['photo'].widget())

        # The form is now re-submitted with a valid title
        # and it should pass validation, and the re-specified
        # image will be saved as well.
        form_data['title'] = 'Dynamic Solutions'
        form_data['photo'] = zoom.utils.Bunch(value=b'fake photo')
        self.assertTrue(fields.validate(form_data))
        self.assertEqual({'photo': b'fake photo'}, fields['photo'].evaluate())