Ticket #15620: clerablefileinput_initial.patch
File clerablefileinput_initial.patch, 3.3 KB (added by , 14 years ago) |
---|
-
django/forms/widgets.py
### Eclipse Workspace Patch 1.0 #P django-trunk
316 316 Given the name of the clear checkbox input, return the HTML id for it. 317 317 """ 318 318 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))) 319 323 320 324 def render(self, name, value, attrs=None): 321 325 substitutions = { … … 329 333 330 334 if value and hasattr(value, "url"): 331 335 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) 335 337 if not self.is_required: 336 338 checkbox_name = self.clear_checkbox_name(name) 337 339 checkbox_id = self.clear_checkbox_id(checkbox_name) -
tests/regressiontests/forms/tests/widgets.py
9 9 from django.forms.widgets import RadioFieldRenderer 10 10 from django.utils import copycompat as copy 11 11 from django.utils import formats 12 from django.utils.html import escape 12 13 from django.utils.safestring import mark_safe 13 14 from django.utils.translation import activate, deactivate 14 15 from django.utils.unittest import TestCase 15 16 16 17 17 18 18 class FormsWidgetTestCase(TestCase): 19 19 # Each Widget class corresponds to an HTML form widget. A Widget knows how to 20 20 # render itself, given a field name and some data. Widgets don't perform … … 1107 1107 self.assertTrue(u'something<div onclick="alert('oops')">.jpg' in output) 1108 1108 self.assertTrue(u'my<div>file' in output) 1109 1109 self.assertFalse(u'my<div>file' in output) 1110 1110 1111 1111 def test_clear_input_renders_only_if_not_required(self): 1112 1112 """ 1113 1113 A ClearableFileInput with is_required=False does not render a clear … … 1130 1130 self.assertEqual(widget.render('myfile', None), 1131 1131 u'<input type="file" name="myfile" />') 1132 1132 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 1133 1147 def test_clear_input_checked_returns_false(self): 1134 1148 """ 1135 1149 ClearableFileInput.value_from_datadict returns False if the clear