def test_cleared_but_file_given(self):
        """
        If we check the clear checkbox, but also submit a file, the
        file overrides.

        """
        field = ClearableFileField()
        result = field.clean([self.upload, '1'])
        self.assertEqual(result, self.upload)
    def test_not_cleared(self):
        """
        If the clear checkbox is not checked, the ``FileField`` data
        is returned normally.

        """
        field = ClearableFileField()
        result = field.clean([self.upload, '0'])
        self.assertEqual(result, self.upload)
Exemple #3
0
    def test_cleared_but_file_given(self):
        """
        If we check the clear checkbox, but also submit a file, the
        file overrides.

        """
        field = ClearableFileField()
        result = field.clean([self.upload, '1'])
        self.assertEqual(result, self.upload)
Exemple #4
0
    def test_not_cleared(self):
        """
        If the clear checkbox is not checked, the ``FileField`` data
        is returned normally.

        """
        field = ClearableFileField()
        result = field.clean([self.upload, '0'])
        self.assertEqual(result, self.upload)
    def test_cleared(self):
        """
        If the clear checkbox is checked and the file input empty, the
        field returns a value that is able to get a normal model
        ``FileField`` to clear itself.

        This is actually a bit tricky/hacky in the implementation, see
        the docstring of ``form_utils.fields.FakeEmptyFieldFile`` for
        details. Here we just test the results.

        """
        doc = Document.objects.create(myfile='something.txt')
        field = ClearableFileField(required=False)
        result = field.clean(['', '1'])
        doc._meta.get_field('myfile').save_form_data(doc, result)
        doc.save()
        doc = Document.objects.get(pk=doc.pk)
        self.assertEqual(doc.myfile, '')
Exemple #6
0
    def test_custom_template(self):
        """
        We can pass in a custom template and it will be passed on to
        the widget.

        """
        tpl = 'Clear: %(checkbox)s %(input)s'
        field = ClearableFileField(template=tpl)
        self.assertEqual(field.widget.template, tpl)
Exemple #7
0
    def test_cleared(self):
        """
        If the clear checkbox is checked and the file input empty, the
        field returns a value that is able to get a normal model
        ``FileField`` to clear itself.

        This is actually a bit tricky/hacky in the implementation, see
        the docstring of ``form_utils.fields.FakeEmptyFieldFile`` for
        details. Here we just test the results.

        """
        doc = Document.objects.create(myfile='something.txt')
        field = ClearableFileField(required=False)
        result = field.clean(['', '1'])
        doc._meta.get_field('myfile').save_form_data(doc, result)
        doc.save()
        doc = Document.objects.get(pk=doc.pk)
        self.assertEqual(doc.myfile, '')
Exemple #8
0
    def test_custom_file_field_required(self):
        """
        If we pass in our own ``file_field`` its required value is
        used for the composite field.

        """
        file_field = forms.ImageField(required=False)
        field = ClearableFileField(file_field=file_field)
        self.assertFalse(field.required)
Exemple #9
0
    def test_custom_file_field(self):
        """
        We can pass in our own ``file_field`` rather than using the
        default ``forms.FileField``.

        """
        file_field = forms.ImageField()
        field = ClearableFileField(file_field=file_field)
        self.assertTrue(field.fields[0] is file_field)
Exemple #10
0
    def test_custom_file_field_widget_used(self):
        """
        If we pass in our own ``file_field`` its widget is used for
        the internal file field.

        """
        widget = ImageWidget()
        file_field = forms.ImageField(widget=widget)
        field = ClearableFileField(file_field=file_field)
        self.assertTrue(field.fields[0].widget is widget)
Exemple #11
0
 class TestForm(forms.Form):
     f = ClearableFileField()
Exemple #12
0
 def formfield_for_dbfield(self, db_field, **kwargs):
     field = super(ClearableFileFieldsAdmin, self).formfield_for_dbfield(
         db_field, **kwargs)
     if isinstance(field, forms.FileField):
         field = ClearableFileField(field)
     return field