Ticket #21201: clearable-file-input.patch

File clearable-file-input.patch, 2.6 KB (added by Vlastimil Zíma, 11 years ago)
  • django/forms/widgets.py

    diff --git a/django/forms/widgets.py b/django/forms/widgets.py
    index f832011..1dc0354 100644
    a b from itertools import chain  
    99import warnings
    1010
    1111from django.conf import settings
     12from django.core.files.uploadedfile import UploadedFile
    1213from django.forms.utils import flatatt, to_current_timezone
    1314from django.utils.datastructures import MultiValueDict, MergeDict
    1415from django.utils.html import conditional_escape, format_html
    class ClearableFileInput(FileInput):  
    324325    input_text = ugettext_lazy('Change')
    325326    clear_checkbox_label = ugettext_lazy('Clear')
    326327
    327     template_with_initial = '%(initial_text)s: %(initial)s %(clear_template)s<br />%(input_text)s: %(input)s'
     328    template_with_initial = '%(initial_text)s: <a href="%(initial_url)s">%(initial)s</a> %(clear_template)s<br />%(input_text)s: %(input)s'
    328329
    329330    template_with_clear = '%(clear)s <label for="%(clear_checkbox_id)s">%(clear_checkbox_label)s</label>'
    330331
    331     url_markup_template = '<a href="{0}">{1}</a>'
    332 
    333332    def clear_checkbox_name(self, name):
    334333        """
    335334        Given the name of the file input, return the name of the clear checkbox
    class ClearableFileInput(FileInput):  
    343342        """
    344343        return name + '_id'
    345344
     345    def is_initial(self, value):
     346        """
     347        Returns whether value is considered to be initial value.
     348        """
     349        return bool(value and hasattr(value, 'url'))
     350
     351    def value_to_substitutions(self, value):
     352        """
     353        Returns value-related substitutions.
     354        """
     355        return  {
     356            'initial': conditional_escape(value),
     357            'initial_url': conditional_escape(value.url)
     358        }
     359
    346360    def render(self, name, value, attrs=None):
    347361        substitutions = {
    348362            'initial_text': self.initial_text,
    class ClearableFileInput(FileInput):  
    353367        template = '%(input)s'
    354368        substitutions['input'] = super(ClearableFileInput, self).render(name, value, attrs)
    355369
    356         if value and hasattr(value, "url"):
     370        if self.is_initial(value):
    357371            template = self.template_with_initial
    358             substitutions['initial'] = format_html(self.url_markup_template,
    359                                                    value.url,
    360                                                    force_text(value))
     372            substitutions.update(self.value_to_substitutions(value))
    361373            if not self.is_required:
    362374                checkbox_name = self.clear_checkbox_name(name)
    363375                checkbox_id = self.clear_checkbox_id(checkbox_name)
Back to Top