Skip to content
Myr

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

3 files+65 added−0 removed

added:flask/debughelpers.py+52 added
1# -*- coding: utf-8 -*-2"""3 flask.debughelpers4 ~~~~~~~~~~~~~~~~~~56 Various helpers to make the development experience better.78 :copyright: (c) 2011 by Armin Ronacher.9 :license: BSD, see LICENSE for more details.10"""111213class 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 """1718 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)3132 def __str__(self):33 return self.msg343536def 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__ = newcls
modified:flask/wrappers.py+11 added
10 unmodified lines
1112from werkzeug.wrappers import Request as RequestBase, Response as ResponseBase13from werkzeug.utils import cached_property1415from .debughelpers import make_enctype_error_multidict16from .helpers import json, _assert_have_json17from .globals import _request_ctx_stack1819
79 unmodified lines
99 if request_charset is not None:100 return json.loads(self.data, encoding=request_charset)101 return json.loads(self.data)102103 def _load_form_data(self):104 RequestBase._load_form_data(self)105106 # 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)112113114class 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 because
6 unmodified lines
modified:CHANGES+2 added
14 unmodified lines
15- 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.2122Version 0.7.323-------------24
238 unmodified lines