Added debughelpers. Flask will now tell you if you forget enctype
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Armin Ronacher · committed by Armin Ronacher · parent a69b437 · Browse files · 3 files·+65 added−0 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
3 files+65 added−0 removed
- other:+65 added−0 removed
Provenance
- Commit
2e022cb- Tree
fede35d- Author
Armin Ronacher <armin.ronacher@active-4.com>- Committer
Armin Ronacher <armin.ronacher@active-4.com>- Parent
a69b437
3 files changed+65 added−0 removed
added:flask/debughelpers.py+52 added
1+# -*- coding: utf-8 -*-2+"""3+ flask.debughelpers4+ ~~~~~~~~~~~~~~~~~~5+6+ Various helpers to make the development experience better.7+8+ :copyright: (c) 2011 by Armin Ronacher.9+ :license: BSD, see LICENSE for more details.10+"""11+12+13+class DebugFilesKeyError(KeyError, AssertionError):14+ """Raised from request.files during debugging. The idea is that it can15+ provide a better error message than just a generic KeyError/BadRequest.16+ """17+18+ def __init__(self, request, key):19+ form_matches = request.form.getlist(key)20+ buf = ['You tried to access the file "%s" in the request.files '21+ 'dictionary but it does not exist. The mimetype for the request '22+ 'is "%s" instead of "multipart/form-data" which means that no '23+ 'files were transmitted. To fix this error you most likely have '24+ 'to provide enctype="multipart/form-data" in your form.' %25+ (key, request.mimetype)]26+ if form_matches:27+ buf.append('\n\nThe browser instead most likely submitted the '28+ 'filenames in the form. This was submitted: %s' %29+ ', '.join('"%s"' % x for x in form_matches))30+ self.msg = ''.join(buf)31+32+ def __str__(self):33+ return self.msg34+35+36+def make_enctype_error_multidict(request):37+ """Since Flask 0.8 we're monkeypatching the files object in case a38+ request is detected that does not use multipart form data but the files39+ object is accessed.40+ """41+ oldcls = request.files.__class__42+ class newcls(oldcls):43+ def __getitem__(self, key):44+ try:45+ return oldcls.__getitem__(self, key)46+ except KeyError, e:47+ if key not in request.form:48+ raise49+ raise DebugFilesKeyError(request, key)50+ newcls.__name__ = oldcls.__name__51+ newcls.__module__ = oldcls.__module__52+ request.files.__class__ = newclsmodified:flask/wrappers.py+11 added
10 unmodified lines11 12 from werkzeug.wrappers import Request as RequestBase, Response as ResponseBase13 from werkzeug.utils import cached_property14 15+from .debughelpers import make_enctype_error_multidict16 from .helpers import json, _assert_have_json17 from .globals import _request_ctx_stack18 19 79 unmodified lines99 if request_charset is not None:100 return json.loads(self.data, encoding=request_charset)101 return json.loads(self.data)102 103+ def _load_form_data(self):104+ RequestBase._load_form_data(self)105 106+ # in debug mode we're replacing the files multidict with an ad-hoc107+ # subclass that raises a different error for key errors.108+ ctx = _request_ctx_stack.top109+ if ctx is not None and ctx.app.debug and \110+ self.mimetype != 'multipart/form-data':111+ make_enctype_error_multidict(self)112+113+114 class Response(ResponseBase):115 """The response object that is used by default in Flask. Works like the116 response object from Werkzeug but is set to have an HTML mimetype by117 default. Quite often you don't have to create this object yourself because6 unmodified linesmodified:CHANGES+2 added
14 unmodified lines15 - View functions can now opt out of getting the automatic16 OPTIONS implementation.17 - HTTP exceptions and Bad Request Key Errors can now be trapped so that they18 show up normally in the traceback.19+- Flask in debug mode is now detecting some common problems and tries to20+ warn you about them.21 22 Version 0.7.323 -------------24 238 unmodified lines