changeset 244:a11656ba1eab

modify views: use flatland also for the file upload form field
author Thomas Waldmann <tw AT waldmann-edv DOT de>
date Sun, 29 May 2011 22:53:38 +0200
parents 69c0d9f6981d
children 52ca5dc1ab15
files MoinMoin/items/__init__.py MoinMoin/templates/modify_applet.html MoinMoin/util/forms.py
diffstat 3 files changed, 25 insertions(+), 11 deletions(-) [+]
line wrap: on
line diff
--- a/MoinMoin/items/__init__.py	Sun May 29 20:40:03 2011 +0200
+++ b/MoinMoin/items/__init__.py	Sun May 29 22:53:38 2011 +0200
@@ -24,6 +24,7 @@
 
 from flatland import Form, String, Integer, Boolean, Enum
 from flatland.validation import Validator, Present, IsEmail, ValueBetween, URLValidator, Converted
+from MoinMoin.util.forms import FileStorage
 
 from MoinMoin.security.textcha import TextCha, TextChaizedForm, TextChaValid
 from MoinMoin.signalling import item_modified
@@ -688,6 +689,7 @@
         class ModifyForm(CommentForm):
             rev = Integer.using(optional=False)
             meta_text = String.using(optional=False).with_properties(placeholder=L_("MetaData (JSON)"))
+            data_file = FileStorage.using(optional=True, label=L_('Upload file:'))
 
         if request.method == 'GET':
             form = ModifyForm.from_defaults()
@@ -695,7 +697,7 @@
             form['meta_text'] = self.meta_dict_to_text(self.meta)
             form['rev'] = self.rev.revno if self.rev.revno is not None else -1
         elif request.method == 'POST':
-            form = ModifyForm.from_flat(request.form)
+            form = ModifyForm.from_flat(request.form.items() + request.files.items())
             TextCha(form).amend_form()
             valid = form.validate()
             if valid:
@@ -1145,7 +1147,8 @@
         class ModifyForm(CommentForm):
             rev = Integer.using(optional=False)
             meta_text = String.using(optional=False).with_properties(placeholder=L_("MetaData (JSON)"))
-            data_text = String.using(optional=False).with_properties(placeholder=L_("Type your text here"))
+            data_text = String.using(optional=True).with_properties(placeholder=L_("Type your text here"))
+            data_file = FileStorage.using(optional=True, label=L_('Upload file:'))
 
         if request.method == 'GET':
             if template_name is None and isinstance(self.rev, DummyRev):
@@ -1160,7 +1163,7 @@
             form['meta_text'] = self.meta_dict_to_text(self.meta)
             form['rev'] = self.rev.revno if self.rev.revno is not None else -1
         elif request.method == 'POST':
-            form = ModifyForm.from_flat(request.form)
+            form = ModifyForm.from_flat(request.form.items() + request.files.items())
             TextCha(form).amend_form()
             valid = form.validate()
             if valid:
@@ -1343,6 +1346,7 @@
             rev = Integer.using(optional=False)
             # XXX as the "saving" POSTs come from TWikiDraw (not the form), editing meta_text doesn't work
             meta_text = String.using(optional=False).with_properties(placeholder=L_("MetaData (JSON)"))
+            data_file = FileStorage.using(optional=True, label=L_('Upload file:'))
 
         if request.method == 'GET':
             form = ModifyForm.from_defaults()
@@ -1435,6 +1439,7 @@
             rev = Integer.using(optional=False)
             # XXX as the "saving" POSTs come from AnyWikiDraw (not the form), editing meta_text doesn't work
             meta_text = String.using(optional=False).with_properties(placeholder=L_("MetaData (JSON)"))
+            data_file = FileStorage.using(optional=True, label=L_('Upload file:'))
 
         if request.method == 'GET':
             form = ModifyForm.from_defaults()
@@ -1523,6 +1528,7 @@
             rev = Integer.using(optional=False)
             # XXX as the "saving" POSTs come from SvgDraw (not the form), editing meta_text doesn't work
             meta_text = String.using(optional=False).with_properties(placeholder=L_("MetaData (JSON)"))
+            data_file = FileStorage.using(optional=True, label=L_('Upload file:'))
 
         if request.method == 'GET':
             form = ModifyForm.from_defaults()
--- a/MoinMoin/templates/modify_applet.html	Sun May 29 20:40:03 2011 +0200
+++ b/MoinMoin/templates/modify_applet.html	Sun May 29 22:53:38 2011 +0200
@@ -8,14 +8,7 @@
 {{ gen.input(form['rev'], type='hidden') }}
 {% block extra_form %}{% endblock %}
 {% block data_editor %}{% endblock %}
-    <dl>
-        <dt>
-            <label for="data_file">{{ _("Upload file:") }}</label>
-         </dt>
-         <dd>
-            <input type="file" id="data_file" name="data_file" />
-        </dd>
-    </dl>
+{{ forms.render_field(gen, form['data_file'], 'file') }}
 <pre>{{ help }}</pre>
 {{ gen.textarea(form['meta_text'], lang='en', dir='ltr', rows=rows_meta, cols=cols) }}
 <br />
--- a/MoinMoin/util/forms.py	Sun May 29 20:40:03 2011 +0200
+++ b/MoinMoin/util/forms.py	Sun May 29 22:53:38 2011 +0200
@@ -85,3 +85,18 @@
                               error_filter,
                               required_filter, placeholder_filter, autofocus_filter])
 
+
+# other flatland stuff
+
+from flatland import AdaptationError, Scalar
+import werkzeug
+
+
+class FileStorage(Scalar):
+    """Schema element for Werkzeug FileStorage instances."""
+
+    def adapt(self, value):
+        if not isinstance(value, (type(None), werkzeug.FileStorage)):
+            raise AdaptationError
+        return value
+