Commits

Thomas Waldmann committed a11656b

modify views: use flatland also for the file upload form field

Comments (0)

Files changed (3)

MoinMoin/items/__init__.py

 
 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
         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()
             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:
         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):
             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:
             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()
             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()
             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()

MoinMoin/templates/modify_applet.html

 {{ 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 />

MoinMoin/util/forms.py

                               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
+
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.