Code

Ticket #5387: form-is-multipart-test-docs.diff

File form-is-multipart-test-docs.diff, 2.9 KB (added by murkt, 7 years ago)

Patch with tests and documentation for rev 6268.

Line 
1Index: django/newforms/forms.py
2===================================================================
3--- django/newforms/forms.py    (revision 6268)
4+++ django/newforms/forms.py    (working copy)
5@@ -212,6 +212,18 @@
6         """
7         return self.cleaned_data
8 
9+    def is_multipart(self):
10+        """
11+        Returns True if the form needs to be multipart-encrypted, i.e. it has
12+        FileInput. Otherwise, False.
13+        """
14+        # python 2.5 syntax
15+        # return all(field.widget.needs_multipart_form for field in self.fields.values())
16+        for field in self.fields.values():
17+            if field.widget.needs_multipart_form:
18+                return True
19+        return False
20+
21 class Form(BaseForm):
22     "A collection of Fields, plus their associated data."
23     # This is a separate class from BaseForm in order to abstract the way
24Index: django/newforms/widgets.py
25===================================================================
26--- django/newforms/widgets.py  (revision 6268)
27+++ django/newforms/widgets.py  (working copy)
28@@ -24,6 +24,7 @@
29 
30 class Widget(object):
31     is_hidden = False          # Determines whether this corresponds to an <input type="hidden">.
32+    needs_multipart_form = False # Determines does this widget need multipart-encrypted form
33 
34     def __init__(self, attrs=None):
35         if attrs is not None:
36@@ -120,6 +121,7 @@
37 
38 class FileInput(Input):
39     input_type = 'file'
40+    needs_multipart_form = True
41 
42     def render(self, name, value, attrs=None):
43         return super(FileInput, self).render(name, None, attrs=attrs)
44Index: docs/newforms.txt
45===================================================================
46--- docs/newforms.txt   (revision 6268)
47+++ docs/newforms.txt   (working copy)
48@@ -776,6 +776,13 @@
49     # Unbound form with a image field
50     >>> f = ContactFormWithMugshot()
51 
52+If you're writing some generic views and templates, you can check
53+that your form needs proper ``enctype`` with the help from ``is_multipart()``::
54+
55+    >>> f = ContactFormWithMugshot()
56+    >>> f.is_multipart()
57+    True
58+
59 Subclassing forms
60 -----------------
61 
62Index: tests/regressiontests/forms/tests.py
63===================================================================
64--- tests/regressiontests/forms/tests.py        (revision 6268)
65+++ tests/regressiontests/forms/tests.py        (working copy)
66@@ -3856,6 +3856,25 @@
67 <div class="errorlist"><div class="error">This field is required.</div></div>
68 <p>Comment: <input type="text" name="comment" /></p>
69 
70+#################################
71+# Test multipart-encrypted form #
72+#################################
73+
74+>>> class FormWithoutFile(Form):
75+...     username = CharField()
76+>>> class FormWithFile(Form):
77+...     username = CharField()
78+...     file = FileField()
79+>>> class FormWithImage(Form):
80+...     image = ImageField()
81+
82+>>> FormWithoutFile().is_multipart()
83+False
84+>>> FormWithFile().is_multipart()
85+True
86+>>> FormWithImage().is_multipart()
87+True
88+
89 """
90 
91 __test__ = {