Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 80 lines (66 sloc) 3.279 kb
2e022cb @mitsuhiko Added debughelpers. Flask will now tell you if you forget enctype
authored
1 # -*- coding: utf-8 -*-
2 """
3 flask.debughelpers
4 ~~~~~~~~~~~~~~~~~~
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 can
15 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 '
17a836d @mitsuhiko Better wording
authored
23 'file contents were transmitted. To fix this error you should '
24 'provide enctype="multipart/form-data" in your form.' %
2e022cb @mitsuhiko Added debughelpers. Flask will now tell you if you forget enctype
authored
25 (key, request.mimetype)]
26 if form_matches:
23c1dd8 @mitsuhiko Wording fail
authored
27 buf.append('\n\nThe browser instead transmitted some file names. '
28 'This was submitted: %s' % ', '.join('"%s"' % x
17a836d @mitsuhiko Better wording
authored
29 for x in form_matches))
f3db68c @mitsuhiko Added testcase for the debug behavior and explicit encoding
authored
30 self.msg = ''.join(buf).encode('utf-8')
2e022cb @mitsuhiko Added debughelpers. Flask will now tell you if you forget enctype
authored
31
32 def __str__(self):
33 return self.msg
34
35
6847329 @mitsuhiko Flask will now give you an error in debug mode if a post request caus…
authored
36 class FormDataRoutingRedirect(AssertionError):
37 """This exception is raised by Flask in debug mode if it detects a
38 redirect caused by the routing system when the request method is not
39 GET, HEAD or OPTIONS. Reasoning: form data will be dropped.
40 """
41
42 def __init__(self, request):
43 exc = request.routing_exception
44 buf = ['A request was sent to this URL (%s) but a redirect was '
45 'issued automatically by the routing system to "%s".'
46 % (request.url, exc.new_url)]
47
48 # In case just a slash was appended we can be extra helpful
49 if request.base_url + '/' == exc.new_url.split('?')[0]:
50 buf.append(' The URL was defined with a trailing slash so '
51 'Flask will automatically redirect to the URL '
52 'with the trailing slash if it was accessed '
53 'without one.')
54
55 buf.append(' Make sure to directly send your %s-request to this URL '
56 'since we can\'t make browsers or HTTP clients redirect '
87f50fd @mitsuhiko Don't lie to the user about POST redirects
authored
57 'with form data reliably or without user interaction.' %
58 request.method)
6847329 @mitsuhiko Flask will now give you an error in debug mode if a post request caus…
authored
59 buf.append('\n\nNote: this exception is only raised in debug mode')
60 AssertionError.__init__(self, ''.join(buf).encode('utf-8'))
61
62
ac99cd3 @mitsuhiko Better internal method name
authored
63 def attach_enctype_error_multidict(request):
2e022cb @mitsuhiko Added debughelpers. Flask will now tell you if you forget enctype
authored
64 """Since Flask 0.8 we're monkeypatching the files object in case a
65 request is detected that does not use multipart form data but the files
66 object is accessed.
67 """
68 oldcls = request.files.__class__
69 class newcls(oldcls):
70 def __getitem__(self, key):
71 try:
72 return oldcls.__getitem__(self, key)
73 except KeyError, e:
74 if key not in request.form:
75 raise
76 raise DebugFilesKeyError(request, key)
77 newcls.__name__ = oldcls.__name__
78 newcls.__module__ = oldcls.__module__
79 request.files.__class__ = newcls
Something went wrong with that request. Please try again.