Skip to content

Commit

Permalink
Issue zestedesavoir#1337 : Protection anti-None
Browse files Browse the repository at this point in the history
  • Loading branch information
SpaceFox committed Aug 18, 2014
1 parent fd7434a commit 27de284
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions zds/middlewares/profile.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@
re.compile( ".*" ), # catch strange entries
]


def can_profile(request):
return (settings.DEBUG or (request.user is not None and request.user.is_superuser)) and 'prof' in request.GET


class ProfileMiddleware(object):
"""
Displays hotshot profiling for any view.
Expand All @@ -31,13 +36,14 @@ class ProfileMiddleware(object):
WARNING: It uses hotshot profiler which is not thread safe.
"""

def process_request(self, request):
if (settings.DEBUG or request.user.is_superuser) and 'prof' in request.GET:
if can_profile(request):
self.tmpfile = tempfile.mktemp()
self.prof = hotshot.Profile(self.tmpfile)

def process_view(self, request, callback, callback_args, callback_kwargs):
if (settings.DEBUG or request.user.is_superuser) and 'prof' in request.GET:
if can_profile(request):
return self.prof.runcall(callback, request, *callback_args, **callback_kwargs)

def get_group(self, file):
Expand Down Expand Up @@ -88,7 +94,7 @@ def summary_for_files(self, stats_str):

def process_response(self, request, response):
try:
if (settings.DEBUG or request.user.is_superuser) and 'prof' in request.GET:
if can_profile(request):
self.prof.close()

out = StringIO.StringIO()
Expand Down

0 comments on commit 27de284

Please sign in to comment.