Ticket #15620: clerablefileinput_initial.patch

File clerablefileinput_initial.patch, 3.3 KB (added by Alexey Boriskin, 14 years ago)
  • django/forms/widgets.py

    ### Eclipse Workspace Patch 1.0
    #P django-trunk
     
    316316        Given the name of the clear checkbox input, return the HTML id for it.
    317317        """
    318318        return name + '_id'
     319   
     320    def get_initial_value(self, value):
     321        return u'<a href="%s">%s</a>'  % (escape(value.url),
     322                                          escape(force_unicode(value)))
    319323
    320324    def render(self, name, value, attrs=None):
    321325        substitutions = {
     
    329333
    330334        if value and hasattr(value, "url"):
    331335            template = self.template_with_initial
    332             substitutions['initial'] = (u'<a href="%s">%s</a>'
    333                                         % (escape(value.url),
    334                                            escape(force_unicode(value))))
     336            substitutions['initial'] = self.get_initial_value(value)
    335337            if not self.is_required:
    336338                checkbox_name = self.clear_checkbox_name(name)
    337339                checkbox_id = self.clear_checkbox_id(checkbox_name)
  • tests/regressiontests/forms/tests/widgets.py

     
    99from django.forms.widgets import RadioFieldRenderer
    1010from django.utils import copycompat as copy
    1111from django.utils import formats
     12from django.utils.html import escape
    1213from django.utils.safestring import mark_safe
    1314from django.utils.translation import activate, deactivate
    1415from django.utils.unittest import TestCase
    1516
    1617
    17 
    1818class FormsWidgetTestCase(TestCase):
    1919    # Each Widget class corresponds to an HTML form widget. A Widget knows how to
    2020    # render itself, given a field name and some data. Widgets don't perform
     
    11071107        self.assertTrue(u'something&lt;div onclick=&quot;alert(&#39;oops&#39;)&quot;&gt;.jpg' in output)
    11081108        self.assertTrue(u'my&lt;div&gt;file' in output)
    11091109        self.assertFalse(u'my<div>file' in output)
    1110 
     1110       
    11111111    def test_clear_input_renders_only_if_not_required(self):
    11121112        """
    11131113        A ClearableFileInput with is_required=False does not render a clear
     
    11301130        self.assertEqual(widget.render('myfile', None),
    11311131                         u'<input type="file" name="myfile" />')
    11321132
     1133    def test_initial_value(self):
     1134        widget = ClearableFileInput()
     1135        widget.is_required = True
     1136        self.assertEqual(widget.get_initial_value(FakeFieldFile()), u'<a href="something">something</a>')
     1137
     1138    def test_initial_value_override(self):
     1139        class ThumbnailClearableFileInput(ClearableFileInput):
     1140            def get_initial_value(self, value):
     1141                return u'<img src="%s" >'  % (escape(value.url))
     1142        widget = ThumbnailClearableFileInput()
     1143        widget.is_required = True
     1144        self.assertEqual(widget.render('myfile', FakeFieldFile()),
     1145                         u'Currently: <img src="something" > <br />Change: <input type="file" name="myfile" />')
     1146
    11331147    def test_clear_input_checked_returns_false(self):
    11341148        """
    11351149        ClearableFileInput.value_from_datadict returns False if the clear
Back to Top