Python django.forms.ClearableFileInput() Examples

The following are 9 code examples for showing how to use django.forms.ClearableFileInput(). These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example.

You may check out the related API usage on the sidebar.

You may also want to check out all available functions/classes of the module django.forms , or try the search function .

Example 1
Project: coursys   Author: sfu-fas   File: other.py    License: GNU General Public License v3.0 6 votes vote down vote up
def render(self, name, value, attrs=None, renderer=None):
        name = str(name)
        substitutions = {
            'initial_text': self.initial_text,
            'input_text': self.input_text,
            'clear_template': '',
            'clear_checkbox_label': self.clear_checkbox_label,
        }
        template = '%(input)s'
        substitutions['input'] = super(forms.ClearableFileInput, self).render(name, value, attrs, renderer=renderer)

        if value and hasattr(value, "url"):
            template = self.template_with_initial
            substitutions['initial'] = ('<a href="%s">%s</a>'
                                        % (escape(value.file_sub.get_file_url()),
                                           escape(value.file_sub.display_filename())))
            if not self.is_required:
                checkbox_name = self.clear_checkbox_name(name)
                checkbox_id = self.clear_checkbox_id(checkbox_name)
                substitutions['clear_checkbox_name'] = escape(checkbox_name)
                substitutions['clear_checkbox_id'] = escape(checkbox_id)
                substitutions['clear'] = forms.CheckboxInput().render(checkbox_name, False, attrs={'id': checkbox_id})
                substitutions['clear_template'] = self.template_with_clear % substitutions

        return mark_safe(template % substitutions) 
Example 2
Project: django-djangui   Author: Chris7   File: factory.py    License: GNU General Public License v3.0 6 votes vote down vote up
def get_field(param, initial=None):
        field = param.form_field
        choices = json.loads(param.choices)
        field_kwargs = {'label': param.script_param.title(),
                        'required': param.required,
                        'help_text': param.param_help,
                        }
        if choices:
            field = 'ChoiceField'
            base_choices = [(None, '----')] if not param.required else []
            field_kwargs['choices'] = base_choices+[(str(i), str(i).title()) for i in choices]
        if field == 'FileField':
            if param.is_output:
                field = 'CharField'
                initial = None
            else:
                if initial is not None:
                    initial = utils.get_storage_object(initial) if not hasattr(initial, 'path') else initial
                    field_kwargs['widget'] = forms.ClearableFileInput()
        if initial is not None:
            field_kwargs['initial'] = initial
        field = getattr(forms, field)
        return field(**field_kwargs) 
Example 3
Project: django-s3file   Author: codingjoe   File: test_forms.py    License: MIT License 6 votes vote down vote up
def test_accept(self):
        widget = ClearableFileInput()
        assert 'accept' not in widget.render(name='file', value='test.jpg')
        assert ["starts-with", "$Content-Type", ""] in widget.get_conditions(None)

        widget = ClearableFileInput(attrs={'accept': 'image/*'})
        assert 'accept="image/*"' in widget.render(name='file', value='test.jpg')
        assert [
                   "starts-with", "$Content-Type", "image/"
               ] in widget.get_conditions('image/*')

        widget = ClearableFileInput(attrs={'accept': 'image/jpeg'})
        assert 'accept="image/jpeg"' in widget.render(name='file', value='test.jpg')
        assert {"Content-Type": 'image/jpeg'} in widget.get_conditions('image/jpeg')

        widget = ClearableFileInput(attrs={'accept': 'application/pdf,image/*'})
        assert 'accept="application/pdf,image/*"' in widget.render(
            name='file',
            value='test.jpg',
        )
        assert ["starts-with", "$Content-Type", ""] in widget.get_conditions(
            'application/pdf,image/*')
        assert {"Content-Type": 'application/pdf'} not in widget.get_conditions(
            'application/pdf,image/*') 
Example 4
Project: django-s3file   Author: codingjoe   File: test_forms.py    License: MIT License 5 votes vote down vote up
def test_build_attr(self):
        assert set(ClearableFileInput().build_attrs({}).keys()) == {
            'class',
            'data-url',
            'data-fields-x-amz-algorithm',
            'data-fields-x-amz-date',
            'data-fields-x-amz-signature',
            'data-fields-x-amz-credential',
            'data-fields-policy',
            'data-fields-key',
        }
        assert ClearableFileInput().build_attrs({})['class'] == 's3file'
        assert ClearableFileInput().build_attrs(
            {'class': 'my-class'})['class'] == 'my-class s3file' 
Example 5
Project: django-s3file   Author: codingjoe   File: test_forms.py    License: MIT License 5 votes vote down vote up
def test_get_conditions(self, freeze):
        conditions = ClearableFileInput().get_conditions(None)
        assert all(condition in conditions for condition in [
            {"bucket": 'test-bucket'},
            {"success_action_status": "201"},
            ['starts-with', '$key', 'custom/location/tmp'],
            ["starts-with", "$Content-Type", ""]
        ]), conditions 
Example 6
Project: django-s3file   Author: codingjoe   File: test_forms.py    License: MIT License 5 votes vote down vote up
def test_media(self):
        assert ClearableFileInput().media._js == ['s3file/js/s3file.js'] 
Example 7
Project: django-s3file   Author: codingjoe   File: test_forms.py    License: MIT License 5 votes vote down vote up
def test_upload_folder(self):
        assert ClearableFileInput().upload_folder.startswith(
            'custom/location/tmp/s3file/'
        )
        assert len(ClearableFileInput().upload_folder) == 49 
Example 8
def is_file(field):
    return isinstance(field.field.widget, forms.ClearableFileInput) 
Example 9
Project: djangocms-forms   Author: mishbahr   File: forms.py    License: BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def prepare_file(self, field):
        field_attrs = field.build_field_attrs()
        widget_attrs = field.build_widget_attrs()

        field_attrs.update({
            'widget': forms.ClearableFileInput(attrs=widget_attrs)
        })

        if field.choice_values:
            regex = re.compile('[\s]*\n[\s]*')
            choices = regex.split(field.choice_values)
            field_attrs.update({
                'allowed_file_types': [i.lstrip('.').lower() for i in choices]
            })
        return FormBuilderFileField(**field_attrs)