From b3ca695ecfb68327f814954b0571107b536a08ef Mon Sep 17 00:00:00 2001 From: David Lord Date: Tue, 10 Jan 2017 02:05:33 -0800 Subject: [PATCH] don't assume Form meta arg is not None closes #278 --- docs/api.rst | 3 ++- docs/changelog.rst | 10 ++++++++++ docs/form.rst | 27 ++++++++++++++++++++++++++- flask_wtf/file.py | 9 +++++++-- 4 files changed, 45 insertions(+), 4 deletions(-) diff --git a/docs/api.rst b/docs/api.rst index c2ea72c4..693a6854 100644 --- a/docs/api.rst +++ b/docs/api.rst @@ -19,7 +19,8 @@ Forms and Fields .. module:: flask_wtf.file -.. autoclass:: FileField(...) +.. autoclass:: FileField + :members: has_file .. autoclass:: FileAllowed diff --git a/docs/changelog.rst b/docs/changelog.rst index b54159fd..63c50578 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -1,6 +1,16 @@ Flask-WTF Changelog =================== +Version 0.14.2 +-------------- + +Released 2017-01-10 + +- Fix bug where ``FlaskForm`` assumed ``meta`` argument was not ``None`` if it + was passed. (`#278`_) + +.. _#278: https://github.com/lepture/flask-wtf/issues/278 + Version 0.14.1 -------------- diff --git a/docs/form.rst b/docs/form.rst index 16e37064..527954bd 100644 --- a/docs/form.rst +++ b/docs/form.rst @@ -30,6 +30,30 @@ File Uploads .. module:: flask_wtf.file +The :class:`FileField` provided by Flask-WTF differs from the WTForms-provided +field. It will check that the file is a non-empty instance of +:class:`~werkzeug.datastructures.FileStorage`, otherwise ``data`` will be +``None``. :: + + from flask_wtf import FlaskForm + from flask_wtf.file import FileField, FileRequired + from werkzeug.utils import secure_filename + + class PhotoForm(FlaskForm): + photo = FileField(validators=[FileRequired()]) + + @app.route('/upload', methods=['GET', 'POST']) + def upload(): + if form.validate_on_submit(): + f = form.photo.data + filename = secure_filename(f.filename) + f.save(os.path.join( + app.instance_path, 'photos', filename + )) + return redirect(url_for('index')) + + return render_template('upload.html', form=form) + Remember to set the ``enctype`` of the HTML form to ``multipart/form-data``, otherwise ``request.files`` will be empty. @@ -55,7 +79,8 @@ Validation ~~~~~~~~~~ Flask-WTF supports validating file uploads with -:class:`FileRequired` and :class:`FileAllowed`. +:class:`FileRequired` and :class:`FileAllowed`. They can be used with both +Flask-WTF's and WTForms's ``FileField`` classes. :class:`FileAllowed` works well with Flask-Uploads. :: diff --git a/flask_wtf/file.py b/flask_wtf/file.py index 56099be2..e5851f52 100644 --- a/flask_wtf/file.py +++ b/flask_wtf/file.py @@ -9,7 +9,7 @@ class FileField(_FileField): - """Werkzeug-aware subclass of **wtforms.FileField**""" + """Werkzeug-aware subclass of :class:`wtforms.fields.FileField`.""" def process_formdata(self, valuelist): valuelist = (x for x in valuelist if isinstance(x, FileStorage) and x) @@ -22,7 +22,12 @@ def process_formdata(self, valuelist): def has_file(self): """Return ``True`` if ``self.data`` is a - :class:`~werkzeug.datastructures.FileStorage` object.""" + :class:`~werkzeug.datastructures.FileStorage` object. + + .. deprecated:: 0.14.1 + ``data`` is no longer set if the input is not a non-empty + ``FileStorage``. Check ``form.data is not None`` instead. + """ warnings.warn(FlaskWTFDeprecationWarning( '"has_file" is deprecated and will be removed in 1.0. The data is '